Reddit Redgifs VideoJS in Post

reddit display redgifs video with videoJS within iframe

// ==UserScript==
// @name        Reddit Redgifs VideoJS in Post
// @namespace   https://greasyfork.org/pt-BR/users/821661
// @match       https://www.reddit.com/*
// @match       https://new.reddit.com/*
// @match       https://www.redgifs.com/ifr/*?isreddit
// @grant       GM_addElement
// @grant       GM_addStyle
// @version     1.2
// @author      hdyzen
// @description reddit display redgifs video with videoJS within iframe
// @license     MIT
// ==/UserScript==

(function () {
    'use strict';
    // Reddit iframe
    function detectLinks() {
        const links = document.querySelectorAll('._3Qkp11fjcAw9I9wtLo8frE ._13svhQIUZqD9PVzFcLwOKT[href*="redgifs.com"]:not([href$=".jpg"], [linked])');
        links.forEach(link => {
            if (link) {
                link.setAttribute('linked', '');
                link.parentElement.appendChild(createIframe(link.href));
            }
        });
    }
    function createIframe(href) {
        const i = document.createElement('iframe');
        i.classList.add('iframe-redgifs');
        i.src = href.replace('/watch/', '/ifr/') + '?isreddit';
        return i;
    }

    // Redgifs iframe
    function detectVideo() {
        const video = document.querySelector('.videoLink video');
        if (video) {
            clearInterval(intervalR);
            createVideo(video.src);
            GM_addElement(document.body, 'script', {
                src: 'https://vjs.zencdn.net/8.6.1/video.min.js',
            });
        }
    }
    function createVideo(src) {
        const e = document.createElement('video');
        e.src = src;
        e.classList.add('video-js');
        e.setAttribute('controls', '');
        e.setAttribute('data-setup', '{}');
        document.body.innerHTML = e.outerHTML;
    }

    // Domain
    const domain = location.hostname;
    if (domain === 'www.redgifs.com') GM_addStyle(`body > *:not(.video-js){display:none!important;}body{background:#000!important;}.video-js{height: 100%;width: 100%; position: fixed !important; inset: 0;}`);
    if (domain === 'www.reddit.com' || domain === 'new.reddit.com') GM_addStyle(`.iframe-redgifs{min-height: 512px;max-height:100vh;max-width:100%;}._10wC0aXnrUKfdJ4Ssz-o14{flex-direction: column !important;}._17nmfaMf1Rq20sVfEmle0O:has(a[href*="redgifs.com"]){display:none!important;}:is(._32pB7ODBwG3OSx1u_17g58, .yn9v_hQEhjlRNZI0xspbA):has(.iframe-redgifs){padding-bottom: 0 !important;}`);

    // Css
    GM_addElement(document.head, 'link', {
        href: 'https://vjs.zencdn.net/8.6.1/video-js.css',
        rel: 'stylesheet',
    });

    // Intervals
    let intervalE = setInterval(detectLinks, 500);
    let intervalR = setInterval(detectVideo, 250);
})();