Redgifs source

Redirects redgifs link to its source video

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Redgifs source
// @description  Redirects redgifs link to its source video
// @license      MIT
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Redgifs source
// @author       John
// @match        https://*.redgifs.com/*
// @match        https://gfycat.com/*
// @include      /https?://.*gifv.*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @run-at     document-start
// ==/UserScript==

var interval = undefined;

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

function openInNewTab(url) {
    window.open(url, '_blank').focus();
}
function navigate(currentDoc, path) {
    if (interval != undefined) {
        clearInterval(interval);
    }
    if (navigated) {
        console.log("Already navigated");
        return;
    }
    navigated = true;
    console.log("orig path:", path);
    var player = getPlayer();
    if (window.location.href.indexOf("redgifs") > -1) {
        //path = path.replace("-mobile", "");
    }
    if (player && player.src.indexOf("expires") > -1) {
        // path = path.replace("thumbs4.redgifs", "thumbs2.redgifs");
        // path = path.replace("thumbs4.redgifs", "thumbs3.redgifs");
    }
    console.log("Navigating to:", path);
    if (path == "") {
        console.log("Cannot navigate!")
    } else {
    // currentDoc.location = path;
        window.location = path;
    }
    //openInNewTab(path);
}

function getPlayer() {
    try {
        var qualityButton = document.getElementsByClassName("gif-quality")[0];
        // console.log("quality button:", qualityButton);
        if (qualityButton.classList.contains("sd")) {
            qualityButton.click();
            window.location.reload();
            return;
        }
        var playerVideo;
        var playerVideoClass;
        if (window.location.href.indexOf("redgifs") > -1) {
            console.log("redgifs!", document);
            // playerVideoClass = "player-video";
            playerVideoClass = "videoWrapper";
            // console.log("redgifs:", document.getElementsByClassName(playerVideoClass)[0].getElementsByTagName("video"));
            playerVideo = document.getElementsByClassName(playerVideoClass)[0].getElementsByTagName("video")[0];
        } else if (window.location.href.indexOf("gfycat") > -1) {
            playerVideoClass = "video media";
            playerVideo = document.getElementsByClassName(playerVideoClass)[0].getElementsByTagName("source")[0];
        } else if (window.location.href.indexOf("gifv") > -1) {
            playerVideo = document.getElementsByTagName("video")[0].getElementsByTagName("source")[0];
        }
        console.log("Player video:", playerVideo);
        return playerVideo;
    } catch (error) {
        // console.log("Not ready yet?", error);
        return undefined;
    }
}

var navigated = false;
document.onkeyup = function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) { //Enter keycode
        console.log("Key press enter");
        var player = getPlayer();
        if (player != undefined) {
            tryNavigate();
        }
    }
};

function getMeta(metaName) {
    const metas = document.getElementsByTagName('meta');
    console.log("metas:", metas)

    for (let i = 0; i < metas.length; i++) {
        console.log(metas[i].getAttribute('property'))
        if (metas[i].getAttribute('property') === metaName) {
            return metas[i].getAttribute('content');
        }
    }
    return '';
}  

function tryNavigate() {
    var player = getPlayer();
    if (player != undefined) {
        var srcPath = player.src || player.currentSrc;
        // if (window.location.href.indexOf("redgifs") > -1) {
        //     srcPath = getMeta("og:video");
        // }
        navigate(document, srcPath);
        return true;
    }
    return false;
}

function start() {
    if (window.location.href.indexOf("redgifs") > -1 && 
        window.location.href.indexOf("watch") <= -1) {
        interval = setInterval(function(){
            var player = getPlayer();
            if (player != undefined) {
                console.log("player:", player);
                // var newHeight = document.body.clientHeight * 0.75;
                var newHeight = 700;
                console.log("New height:", newHeight);
                if (player.parentElement.parentElement.parentElement.clientHeight > newHeight) {
                    player.parentElement.parentElement.parentElement.style = `height: ${newHeight}px` 
                }
                clearInterval(interval);
            }
        }, 1000);
        return;
    }
    if (!tryNavigate()) {
        interval = setInterval(function(){
            if (!tryNavigate()) {
                console.log("Waiting for players");
            }
        }, 1000);
    }
}

window.addEventListener('load', function() {
    console.log("Window loaded: redgifs");
    setTimeout(function() {
        start();
    }, 1500);
}, false);