Reddit Fix

Fix attempt of the infinite scroll, removes background effects on comments

当前为 2020-06-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Reddit Fix
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Fix attempt of the infinite scroll, removes background effects on comments
// @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";

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 lastScrollTop = 0;
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;}' );
    } 

    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();
            console.log(maxOffset);
        });
        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();
            console.log(maxOffset);
        });
     }

    $("#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 callback() {
        if (originalBehavior == "true")
            return;
        if (holdTopicsInMemory == "true")
            AddMenu();
        var st = $(document).scrollTop();
        if (st > maxOffset)
        {
            if (st > lastScrollTop){
                console.log($(".scrollerItem").length);
                if ($(".scrollerItem").length > 50)
                {
                    $(".scrollerItem").slice(0,9).each(function(){
                        var el = $(this);
                        if (el.offset().top == 0){
                            if (holdTopicsInMemory)
                                topicsInMemory.push(el.parent().parent());
                            el.parent().parent().remove();
                            st = 0;
                        }
                    });
                }
            }
            lastScrollTop = st;
            maxOffset = 0;
        }
    }
})();