Reddit Fix

Fix of the infinite scroll, see full image actually shows the full image, remove background effects, copy video adress

目前為 2022-03-13 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Reddit Fix
// @namespace    http://tampermonkey.net/
// @version      1.6.3.1
// @description  Fix of the infinite scroll, see full image actually shows the full image, remove background effects, copy video adress
// @author       Bum
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @match        https://www.reddit.com/*
// @grant        GM_addStyle
// ==/UserScript==

var holdTopicsInMemory = "false";
var commentEffects = "false";
var originalBehavior = "false";
var topicLimitInDom = 70;
var lastScrollTop = 0;
var scrollTopWhenREmoved = -1;

var lastTopicRemovedTime =  new Date().getTime();

if (localStorage.getItem("holdTopicsInMemory") != null) {
    holdTopicsInMemory = localStorage.getItem("holdTopicsInMemory");
}

if (localStorage.getItem("commentEffects") != null) {
    commentEffects = localStorage.getItem("commentEffects");
}

if (localStorage.getItem("originalBehavior") != null) {
    originalBehavior = localStorage.getItem("originalBehavior");
}

var topicsInMemory = [];
var isAPop = false;
var maxOffset = 0;

(function() {
    'use strict';
    function GM_addStyle(css) {
        const style = document.getElementById("GM_addStyle") || (function() {
            const style = document.createElement('style');
            style.type = 'text/css';
            style.id = "GM_addStyle";
            document.head.appendChild(style);
            return style;
        })();
        const sheet = style.sheet;
        sheet.insertRule(css, (sheet.rules || sheet.cssRules || []).length);
    }

    if (commentEffects == "false")
    {
        GM_addStyle ( 'img[src*="flame"]{display:none;}' );
        GM_addStyle ( 'div[aria-role="presentation"]{box-shadow:none !important;background : transparent !important;}' );
    }
    GM_addStyle ( '.wwHbgRV0ZXGp5CHHlpo5u{display:block !important;}' );
    GM_addStyle ( '._1Q2mF3u7v9hBVu_4bkC7R4{display:block !important;}' );
    GM_addStyle ( '._3hUbl08LBz2mbXjy0iYhOS,._3b8u2OJXaSDdBWoRB7zUoK {height: 50px !important;width: 100% !important; bottom: 0 !important;}' );


    //POPUP
    GM_addStyle('.fixmodal {position:fixed;background-color:rgba(0, 0, 0, 0.5);height:100%;width:100%;top:0;left:0;display:none; z-index: 1000;}');
    GM_addStyle('#fixPopup {padding:5px;text-align:center;}');
    GM_addStyle('.fixmodalWrap {margin: 50px auto;; position:relative;width: fit-content;} ');
var maxWidthPop = window.screen.height - 200;
GM_addStyle('#fixPopup img {max-height:'+maxWidthPop+'px;}');

    window.addEventListener('scroll', throttle(callback, 300));

    function throttle(fn, wait) {
        var time = Date.now();
        return function() {
            if ((time + wait - Date.now()) < 0) {
                fn();
                time = Date.now();
            }
        }
    }

    function getMenuItem(id, display)
    {
        return '<a class="M2Hk_S2yvXpsNPfZMBMur customRedditFixMenu" id = "'+id+'" ><div class="_1lwNBHmCQJObvqs1fXKSYR" style="margin-right: 0px;">'+display+'</div></a>';
    }

    function getCheckBoxItem(checked, id, display){

        if (checked == 'true')
            return '<a data-redditstyle="true" class="_1YWXCINvcuU7nk0ED-bta8" class = "fixRedditCustomMenuB"><div class="vzhy90YD0qH7ZDJi7xMGw" style = "margin-left:-30px;">'+display+'</div><button id = "'+id+'" aria-checked="true" class="_2e2g485kpErHhJQUiyvvC2 _179edq2yfZswDIo3NdeebM _1L5kUnhRYhUJ4TkMbOTKkI" role="switch" type="button"><div class="_2FKpII1jz0h6xCAw1kQAvS"></div></button></a>';
        else
            return '<a data-redditstyle="true" class="_1YWXCINvcuU7nk0ED-bta8" class = "fixRedditCustomMenuB"><div class="vzhy90YD0qH7ZDJi7xMGw" style = "margin-left:-30px;">'+display+'</div><button id = "'+id+'" aria-checked="false" class="_2e2g485kpErHhJQUiyvvC2 _179edq2yfZswDIo3NdeebM" role="switch" type="button"><div class="_2FKpII1jz0h6xCAw1kQAvS"></div></button></a>';
    }

    function RemoveMenu(){
        $("#redditFixReloadAll").remove();
        $("#redditFixReload25").remove();
    }
    function AddMenu(){
        var menu = $("._2pUO1Sfe7WlIHvq6goN3Pz");
        if (menu.find(".customRedditFixMenu").length > 0 )
            return;
        menu.append(getMenuItem('redditFixReloadAll', 'Reload All'));
        $("#redditFixReloadAll").click(function(){
            for (var i = topicsInMemory.length - 1; i >= 0; --i) {
                var el = topicsInMemory.pop(i);
                $(".rpBJOHq2PR60pnwJlUyP0").prepend(el);
            }
            maxOffset = $(document).height();
        });
        menu.append(getMenuItem('redditFixReload25', 'Reload 25'));
        $("#redditFixReload25").click(function(){
            var reloadTill = topicsInMemory.length;
            if (reloadTill > 25)
                reloadTill = 25;
            for (var i = 0; i < 25; ++i) {
                var el = topicsInMemory.pop(i);
                $(".rpBJOHq2PR60pnwJlUyP0").prepend(el);
            }
            maxOffset = $(document).height();
        });
     }

    $("#email-collection-tooltip-id").click(function(){
        function _appendMenu() {
            var menuMain = $('div[role="menu"]');
            var isReady = menuMain.length > 0;
            if (!isReady) {
                setTimeout(_appendMenu, 100);
                return;
            }
            if (menuMain.find("#fixRedditCustomMenuB").length == 0){
                menuMain.find('a:last').before(getCheckBoxItem(commentEffects, "redditFixCheckBoxBackground", "Comment effects"));
                $("#redditFixCheckBoxBackground").click(function(){
                    var btnBackgroundsChecked = $(this);
                    if (btnBackgroundsChecked.hasClass("_1L5kUnhRYhUJ4TkMbOTKkI")){
                        btnBackgroundsChecked.removeClass("_1L5kUnhRYhUJ4TkMbOTKkI");
                        localStorage.setItem("commentEffects", false);
                        commentEffects = "false";
                    }
                    else{
                        btnBackgroundsChecked.addClass("_1L5kUnhRYhUJ4TkMbOTKkI");
                        localStorage.setItem("commentEffects", true);
                        commentEffects = "true";
                    }
                });
                menuMain.find('a:last').before(getCheckBoxItem(holdTopicsInMemory, "fixRedditKeepTopicsInMemory", "Save topics in ram"));
                $("#fixRedditKeepTopicsInMemory").click(function(){
                    var btnTopicsChecked = $(this);
                    if (btnTopicsChecked.hasClass("_1L5kUnhRYhUJ4TkMbOTKkI")){
                        btnTopicsChecked.removeClass("_1L5kUnhRYhUJ4TkMbOTKkI");
                        localStorage.setItem("holdTopicsInMemory", false);
                        holdTopicsInMemory = "false";
                        RemoveMenu();
                    }
                    else{
                        btnTopicsChecked.addClass("_1L5kUnhRYhUJ4TkMbOTKkI");
                        localStorage.setItem("holdTopicsInMemory", true);
                        holdTopicsInMemory = "true";
                        AddMenu();
                    }
                });
                menuMain.find('a:last').before(getCheckBoxItem(originalBehavior, "fixRedditoriginalBehavior", "Original behavior"));
                $("#fixRedditoriginalBehavior").click(function(){
                    var btnTopicsChecked = $(this);
                    if (btnTopicsChecked.hasClass("_1L5kUnhRYhUJ4TkMbOTKkI")){
                        btnTopicsChecked.removeClass("_1L5kUnhRYhUJ4TkMbOTKkI");
                        localStorage.setItem("originalBehavior", false);
                        originalBehavior = "false";
                    }
                    else{
                        btnTopicsChecked.addClass("_1L5kUnhRYhUJ4TkMbOTKkI");
                        localStorage.setItem("originalBehavior", true);
                        originalBehavior = "true";
                    }
                });
                menuMain.find("a:last").before('<div class="_7cxLZzQcuE-aYbch5G9oN"></div>');
            }

        }
        _appendMenu();
    });

    function attachObserver(){
        if ($(".observerIsAttached").length > 0)
            return;
        $(".rpBJOHq2PR60pnwJlUyP0").addClass("observerIsAttached");
        var config = { attributes: false, childList: true, subtree: true };
        var targetNodeRoot = $(".rpBJOHq2PR60pnwJlUyP0").first().get(0);
        var configRoot = { attributes: false, childList: true, subtree: true };

        var callbackRoot = function(mutationsList, observer) {
            for(var mutation of mutationsList) {
                mutation.addedNodes.forEach(function(node) {
                    var currentTime = new Date().getTime();
                    var time = currentTime - lastTopicRemovedTime;
                    if ($(node).find(".scrollerItem")){
                        if ($(".scrollerItem").length > topicLimitInDom){
                            let newHeight = $(window).scrollTop();
                            $(".scrollerItem").slice(0,49).each(function(){
                                var currElHeight = 0;
                                if ($(this).parent().parent().attr("style") != ""){
                                    var el = $(this);
                                    if (holdTopicsInMemory)
                                        topicsInMemory.push(el.parent().parent());
                                    currElHeight = el.parent().parent().height();

                                    if(el.parent().parent().find(".promotedlink")){
                                    }
                                    else{
                                        if (currElHeight == 0){
                                            currElHeight = currElHeight + 500;
                                        }
                                    }
                                    el.parent().parent().remove();
                                    newHeight = newHeight - currElHeight;
                                    currElHeight = 0;
                                }
                                lastTopicRemovedTime = new Date().getTime();
                                scrollTopWhenREmoved = lastScrollTop+10000;
                            });
                            $(window).scrollTop(newHeight);
                        }
                    }
                });
            }
        };


        var observerroot = new MutationObserver(callbackRoot);
        observerroot.observe(targetNodeRoot, config);
    }

    function copyToClipboard(element) {
        var $temp = $("<input>");
        $("body").append($temp);
        $temp.val($(element).text()).select();
        document.execCommand("copy");
        $temp.remove();
    }

    function attachTopicObserver(){
        var config = { attributes: false, childList: true, subtree: true };
        var targetNodeTopics = $("#2x-container").first().get(0);
        var configRoot = { attributes: false, childList: true, subtree: true };

        var callbackTopic = function(mutationsList, observer) {
            for(var mutation of mutationsList) {
                mutation.addedNodes.forEach(function(node) {
                    if (($('div[data-test-id="post-content"]:not(.ObserverAttached)').length > 0)){
                        var content = $('div[data-test-id="post-content"]');
                        content.addClass("ObserverAttached");
                        content.find("._3-miAEojrCvx_4FQ8x3P-s").append('<a class="_10K5i7NW6qcm-UoCtpB3aK YszYBnnIoNY8pZ6UwCivd _3yh2bniLq7bYr4BaiXowdO _2sAFaB0tx4Hd5KxVkdUcAx _28vEaVlLWeas1CDiLuTCap" id ="fixDirectLink"><span class="_2-cXnP74241WI7fpcpfPmg"> Get Direct Link</span></a>');
                        $("#fixDirectLink").css("cursor","pointer");
                        var urlThisWindow = $(content).find("._3jOxDPIQ0KaOWpzvSQo-1s").attr("href");
                        if (urlThisWindow.substring(urlThisWindow.length-1) == "/")
                        {
                            urlThisWindow = urlThisWindow.substring(0, urlThisWindow.length-1);
                        }
                        urlThisWindow = urlThisWindow+".json";
                        $("#fixDirectLink").click(function(){
                            $.getJSON( urlThisWindow, function( data ) {
                                try {
                                    var urlFallback = data[0].data.children[0].data.secure_media.reddit_video.fallback_url;
                                    copyToClipboard($("<div>"+urlFallback+"</div>"));
                                    var popupCopy = '<div class="_3q-XSJ2vokDQrvdG6mR__k fixObserverpopup"><div class="trdUvQxqQHHqQKOUBcgnr EjdBJNEwygtHMKiHd3Bnv" style="opacity: 1; x: 1px; y: 0px; transform: translateY(0px) scale(1, 1); --Toaster-indicatorColor:#24A0ED;"><div class="_21oJwLzDt5kLN6scufKBab"><span class="_7JH6kQpO-bx66b9ajIZrz">Link copied to clipboard: ' + urlFallback + '</span></div><div class="_2-rGW3UtrT-pD45pofU3tx"></div></div></div>';
                                    $("#POPUP_CONTAINER").append(popupCopy);
                                    setTimeout(function(){ $(".fixObserverpopup").remove(); }, 3000);

                                }
                                catch(err) {
                                    var popupCopyfailed = '<div class="_3q-XSJ2vokDQrvdG6mR__k fixObserverpopup"><div class="trdUvQxqQHHqQKOUBcgnr EjdBJNEwygtHMKiHd3Bnv" style="opacity: 1; x: 1px; y: 0px; transform: translateY(0px) scale(1, 1); --Toaster-indicatorColor:#24A0ED;"><div class="_21oJwLzDt5kLN6scufKBab"><span class="_7JH6kQpO-bx66b9ajIZrz">No link found</span></div><div class="_2-rGW3UtrT-pD45pofU3tx"></div></div></div>';
                                    $("#POPUP_CONTAINER").append(popupCopyfailed);
                                    setTimeout(function(){ $(".fixObserverpopup").remove(); }, 3000);
                                }
                            });
                        });
                    }
                });
            }
        };
        if (targetNodeTopics != undefined){
            var observerTopic = new MutationObserver(callbackTopic);
            observerTopic.observe(targetNodeTopics, config);
        }
    }

    function fadeOutThePicture(){
        $(".fixmodal").fadeOut("fast");
        document.removeEventListener("click", fadeOutThePicture);
    }
    attachTopicObserver();
    $(document).on("mousedown", "._3b8u2OJXaSDdBWoRB7zUoK,._3hUbl08LBz2mbXjy0iYhOS", function(e) {

        console.log("changed");
        var imgSrc = $(this).parent().find("img").attr("src");
        $(".fixmodal").fadeIn("fast");
        $("#fixPopup img").remove();
        $('<img src="'+imgSrc+'" alt="image3" />').appendTo("#fixPopup");

        setTimeout(() => {document.addEventListener("click", fadeOutThePicture);}, 100);
        e.stopPropagation();
        e.preventDefault();
        e.cancelBubble = true;
        e.stopImmediatePropagation();
        return false
    });

    function callback() {
        if (originalBehavior == "true")
            return;
        if (holdTopicsInMemory == "true")
            AddMenu();
        var st = $(document).scrollTop();
        if (st > maxOffset)
        {
            if (st > lastScrollTop){
                attachObserver();
            }
            lastScrollTop = st;
            maxOffset = 0;
        }
    }
    $("body").append('<div class="fixmodal"> <div class="fixmodalWrap"><div id="fixPopup"></div> </div></div>');


})();