Redgifs Tweaks

tweaks for redgifs page(NOT iframe/embed gifs)

As of 2024-05-27. See the latest version.

// ==UserScript==
// @name               Redgifs Tweaks
// @namespace          https://greasyfork.org/users/821661
// @match              https://www.redgifs.com/*
// @exclude-match      https://www.redgifs.com/ifr/*
// @grant              GM_registerMenuCommand
// @grant              GM_addElement
// @grant              GM_setValue
// @grant              GM_getValue
// @grant              GM_download
// @run-at             document-start
// @version            0.2
// @author             hdyzen
// @description        tweaks for redgifs page(NOT iframe/embed gifs)
// @license            MIT
// @noframes
// ==/UserScript==
'use strict';

const menu = {
    midClickIfr: { state: false, label: 'Middle click in video open ifr link' },
    downloadBtn: { state: true, label: 'Download button' },
    reloadBtn: { state: false, label: 'Reload video button' },
    removeBoosted: { state: true, label: 'Remove boosted/promoted gifs' },
};

// Get options | Default Options
let menuSaved = GM_getValue('menu', menu);

// Array to storage url to download
const urlsArr = [];

// Create/reload options in menu
function menuOpt(obj) {
    menuSaved = GM_getValue('menu', menu);
    Object.entries(obj).forEach(([key]) => {
        if (menuSaved[key]) {
            menu[key].state = menuSaved[key].state;
        }

        GM_registerMenuCommand(`${menu[key].label}: ${menu[key].state ? 'ON' : 'OFF'}`, () => optChange(key, menu[key].state), { id: key, autoClose: false });
    });
}
menuOpt(menu);

// Toggle option in menu
function optChange(key, state) {
    menu[key].state = !state;
    GM_setValue('menu', menu); // Save options
    menuOpt(menu); // Reload options
    createActionButton();
}

// Middle click open embed link
function openIfr() {
    let canOpen = false;
    document.addEventListener('mousedown', e => {
        const gifOverlay = e.target.classList.contains('Video-OverLayer');
        if (menu.midClickIfr.state === true && e.button === 1 && gifOverlay) {
            canOpen = true;
            e.preventDefault();
        }
    });
    document.addEventListener('mouseup', e => {
        const gifPlayer = e.target.closest('.Player.Player_isActive');
        if (canOpen === true && gifPlayer) {
            window.open(`https://www.redgifs.com/ifr/${gifPlayer.id.slice(4)}`, '_blank');
            canOpen = false;
        }
    });
}
openIfr();

// Create action button
function createActionButton(append = document.querySelector('.SideBar > :last-child')) {
    if (menu.downloadBtn.state === true) append.querySelector('.SideBar-Item:last-of-type').insertAdjacentHTML('beforebegin', `<li class="SideBar-Item" ><div class="download-button" style="display: inline-block; cursor: pointer;" ><svg width="28px" height="28px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12.5535 16.5061C12.4114 16.6615 12.2106 16.75 12 16.75C11.7894 16.75 11.5886 16.6615 11.4465 16.5061L7.44648 12.1311C7.16698 11.8254 7.18822 11.351 7.49392 11.0715C7.79963 10.792 8.27402 10.8132 8.55352 11.1189L11.25 14.0682V3C11.25 2.58579 11.5858 2.25 12 2.25C12.4142 2.25 12.75 2.58579 12.75 3V14.0682L15.4465 11.1189C15.726 10.8132 16.2004 10.792 16.5061 11.0715C16.8118 11.351 16.833 11.8254 16.5535 12.1311L12.5535 16.5061Z" fill="#fff"></path><path d="M3.75 15C3.75 14.5858 3.41422 14.25 3 14.25C2.58579 14.25 2.25 14.5858 2.25 15V15.0549C2.24998 16.4225 2.24996 17.5248 2.36652 18.3918C2.48754 19.2919 2.74643 20.0497 3.34835 20.6516C3.95027 21.2536 4.70814 21.5125 5.60825 21.6335C6.47522 21.75 7.57754 21.75 8.94513 21.75H15.0549C16.4225 21.75 17.5248 21.75 18.3918 21.6335C19.2919 21.5125 20.0497 21.2536 20.6517 20.6516C21.2536 20.0497 21.5125 19.2919 21.6335 18.3918C21.75 17.5248 21.75 16.4225 21.75 15.0549V15C21.75 14.5858 21.4142 14.25 21 14.25C20.5858 14.25 20.25 14.5858 20.25 15C20.25 16.4354 20.2484 17.4365 20.1469 18.1919C20.0482 18.9257 19.8678 19.3142 19.591 19.591C19.3142 19.8678 18.9257 20.0482 18.1919 20.1469C17.4365 20.2484 16.4354 20.25 15 20.25H9C7.56459 20.25 6.56347 20.2484 5.80812 20.1469C5.07435 20.0482 4.68577 19.8678 4.40901 19.591C4.13225 19.3142 3.9518 18.9257 3.85315 18.1919C3.75159 17.4365 3.75 16.4354 3.75 15Z" fill="#fff"></path></svg></div></li>`);
    if (menu.reloadBtn.state === true) append.querySelector('.SideBar-Item:last-of-type').insertAdjacentHTML('beforebegin', `<li class="SideBar-Item" ><div class="reload-button" style="display: inline-block; cursor: pointer;" title="Reload video" ><svg width="28px" height="28px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4.06189 13C4.02104 12.6724 4 12.3387 4 12C4 7.58172 7.58172 4 12 4C14.5006 4 16.7332 5.14727 18.2002 6.94416M19.9381 11C19.979 11.3276 20 11.6613 20 12C20 16.4183 16.4183 20 12 20C9.61061 20 7.46589 18.9525 6 17.2916M9 17H6V17.2916M18.2002 4V6.94416M18.2002 6.94416V6.99993L15.2002 7M6 20V17.2916" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><script xmlns=""/></svg></div></li>`);
    append.addEventListener('click', e => {
        if (e.target.closest('.download-button')) {
            const gifId = append.parentNode.parentNode.id.slice(4);
            downloadVideo(urlsArr.find(url => url.toLowerCase().includes(gifId)));
        }

        if (e.target.closest('.reload-button')) {
            document.querySelector('video.isLoaded').load();
        }
    });
}

// Download video
function downloadVideo(url) {
    const nameVideo = url.match(/\/([a-zA-Z0-9.-]+)\?/);
    GM_download({
        url: url,
        name: nameVideo[1],
    });
}

// Get gif url from JSON.parse() patched
function getUrlGif(json, ...args) {
    const parsed = originalParse(json, ...args);

    if (json.includes('"gif"')) {
        urlsArr.push(parsed.gif.urls.hd || parsed.gif.urls.sd);
    } else if (json.includes('"gifs"')) {
        parsed.gifs.forEach(gif => urlsArr.push(gif.urls.hd || gif.urls.sd));
    }

    return parsed;
}

// Patch appendChild() to append (download, reload) button
const originalAppendChild = Node.prototype.appendChild;

Node.prototype.appendChild = function (newNode) {
    if ((menu.downloadBtn.state === true || menu.reloadBtn.state === true) && newNode.classList && newNode.classList.contains('SideBar')) createActionButton(newNode);

    return originalAppendChild.apply(this, arguments);
};

// Patch parse() to get gif url
const originalParse = JSON.parse;

JSON.parse = function (json, ...args) {
    const toReturn = menu.downloadBtn.state === true ? getUrlGif(json, ...args) : originalParse.apply(this, arguments);

    return toReturn;
};

// Patch open() to prevent boosted gifs
const originalXHROpen = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function open(method, url, async, user, password) {
    if (menu.removeBoosted.state === true && url.includes('/gifs/boost')) return;

    return originalXHROpen.apply(this, arguments);
};