Lemmynsfw Style/UX improvement

Screen-fitting images, automatic video playback

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        Lemmynsfw Style/UX improvement
// @namespace   tv.azzurite
// @match       https://lemmynsfw.com/*
// @match       https://media.redgifs.com/*
// @match       https://files.redgifs.com/*
// @grant       none
// @version     1.0
// @author      Azzurite
// @description Screen-fitting images, automatic video playback
// @license     GPLv3
// ==/UserScript==

const UNMUTE_VIDEOS = true;

if (UNMUTE_VIDEOS && location.host.includes(`redgifs.com`) && location.href.includes(`-silent.mp4`)) {
    location.href = location.href.replaceAll(`-silent.mp4`, `.mp4`);
}
const style = document.createElement(`style`);
style.innerHTML = `
.post-listing .img-expanded {
  max-width: 100vw !important;
  height: auto !important;
  width: 100vw !important;
  max-height: 100vh !important;
  object-fit: contain;
  margin: auto;
}
 .post-listings  .offset-sm-3 {
  margin-left: 0 !important;
  }
  .container-lg {
  margin-right: 0 !important;
  margin-left: 0 !important;
  max-width: 100% !important;
  }

  body {
    overflow-x: hidden;
  }
  main {
    width: 100% !important;
  }
  .row {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }
  .row > * {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }
  .container, .container-fluid, .container-lg {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }
`;
document.head.appendChild(style);

const observer = new IntersectionObserver((entries) => {
    entries.forEach((entry) => {
        if (entry.isIntersecting && entry.intersectionRatio > 0.5) {
            entry.target.volume = 1;
            entry.target.play();
        }
        if (!entry.isIntersecting) {
            entry.target.pause();
            entry.target.currentTime = 0;
        }
        if (entry.intersectionRatio < 0.5) {
            entry.target.volume = 0;
        }
    });
}, { threshold: [0, 0.5] });


function init(vid) {
    if (UNMUTE_VIDEOS) {
        vid.muted = false;
        vid.volume = 1;
    }
    vid.loop = true;
    vid.pause();
    observer.observe(vid);
}
for (const vid of document.getElementsByTagName(`video`)) {
    init(vid);
}
const mut = new MutationObserver((records) => {
    for (const record of records) {
        for (const removed of record.removedNodes) {
            if (removed.tagName === `VIDEO`) {
                observer.unobserve(removed);
            }
        }
        for (const added of record.addedNodes) {
            if (added.tagName === `VIDEO`) {
                init(added);
            }
            if (added instanceof HTMLElement) {
                for (const child of added.getElementsByTagName(`video`) || []) {
                    init(child);
                }
            }
        }
    }
});
mut.observe(document.body, { subtree:true, childList: true });