🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2025/12/29 17:29:01
🔄 下次更新时间:2025/12/29 18:29:01
手动刷新缓存

Reddit Watcher

Consolidated mutation observer handler for use by other reddit userscripts. By itself doesn't do much, but other scripts and use hooks to avoid redundant observers on the same thing.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         Reddit Watcher
// @namespace    https://lawrenzo.com/p/reddit-watcher
// @description  Consolidated mutation observer handler for use by other reddit userscripts. By itself doesn't do much, but other scripts and use hooks to avoid redundant observers on the same thing.
// @version      1.3.2
// @author       Lawrence Sim
// @license      WTFPL (http://www.wtfpl.net)
// @grant        unsafeWindow
// @match        *://*.reddit.com/*
// ==/UserScript==
(function() {
    if(unsafeWindow && !unsafeWindow.redditWatcher) {
        var debug = false;
        function watcherAbstract(funcs, opts, name) {
            if(typeof funcs === "function") funcs = {get: funcs};
            return {
                name: name || "unnamed",
                defaultOpts: opts || {childList:true},
                hooks: {
                    update: [],
                    change: []
                },
                onUpdate: function(task) {
                    this.hooks.update.push(task);
                },
                update: function(mutated) {
                    if(debug) console.log(this.name + " updated");
                    this.hooks.update.forEach(task => task(this.watching, mutated));
                },
                onChange: function(task) {
                    this.hooks.change.push(task);
                },
                changed: function() {
                    if(debug) console.log(this.name + " changed");
                    this.hooks.change.forEach(task => task(this.watching));
                },
                un: function(func) {
                    let index = this.hooks.update.indexOf(func);
                    while(~index) {
                        this.hooks.update = this.hooks.splice(index, 1);
                        index = this.hooks.update.indexOf(func);
                    }
                    index = this.hooks.change.indexOf(func);
                    while(~index) {
                        this.hooks.change = this.change.splice(index, 1);
                        index = this.hooks.change.indexOf(func);
                    }
                },
                watching: null,
                watcher: null,
                get: funcs.get,
                reset: function() {
                    if(debug) console.log(this.name + " reset");
                    if(!this.watcher) {
                        this.watcher = new MutationObserver(this.update.bind(this));
                    } else {
                        this.watcher.disconnect();
                    }
                    this.update();
                    this.observe(this.watching, this.defaultOpts);
                },
                observe: function(elem, opts) {
                    if(this.watcher) this.watcher.observe(elem, opts);
                },
                wasChanged: funcs.changed,
                check: function() {
                    if(debug) console.log("checking " + this.name);
                    if(!this.watching || !document.body.contains(this.watching)) {
                        let requery = this.get();
                        if(requery && requery !== this.watching) {
                            this.watching = requery;
                            this.changed();
                            this.reset();
                            return true;
                        }
                    }
                    if(this.wasChanged && this.wasChanged(this.watching)) {
                        this.changed();
                        return true;
                    }
                }
            };
        }
        var lastFeed = null,
            lastFirstPost = null;
        var feedWatcher = watcherAbstract(
            {
                get: function() {
                    let listing = document.querySelector(".ListingLayout-outerContainer"),
                        posts = listing.querySelectorAll("div[data-testid='post-container']"),
                        feed = posts && posts.length > 1 && posts[0].parentNode;
                    while(feed && !feed.nextSibling) {
                        if(feed == listing) return null;
                        feed = feed.parentNode || null;
                    }
                    return feed && feed.parentNode;
                },
                changed: function(feed) {
                    if(lastFeed && lastFeed != feed) {
                        lastFeed = feed;
                        return false;
                    }
                    if(!lastFeed) lastFeed = feed;
                    let firstPost = lastFeed && lastFeed.querySelector("div[data-testid='post-container']");
                    if(firstPost !== lastFirstPost) {
                        lastFirstPost = firstPost;
                        return true;
                    }
                }
            },
            {childList:true},
            "feed"
        );
        var listingWatcher = watcherAbstract(
            {get: () => document.querySelector(".ListingLayout-outerContainer")},
            {childList:true, subtree:true},
            "listing"
        );
        var bodyWatcher = watcherAbstract(
            {get: () => document.body},
            {childList:true},
            "body"
        );
        listingWatcher.onChange(() => feedWatcher.check());
        listingWatcher.onUpdate(() => feedWatcher.check());
        listingWatcher.check();
        var wasInFeed = !!listingWatcher.watching;
        bodyWatcher.onUpdate(body => {
            if(body.querySelectorAll(".ListingLayout-outerContainer div[data-testid='post-container']").length > 1) {
                wasInFeed ? listingWatcher.check() : listingWatcher.reset();
                wasInFeed = true;
            } else {
                if(wasInFeed) listingWatcher.watcher && listingWatcher.watcher.disconnect();
                wasInFeed = false;
            }
        });
        bodyWatcher.check();
        unsafeWindow.redditWatcher = {
            body:    bodyWatcher,
            listing: listingWatcher,
            feed:    feedWatcher,
            update:  function() { this.body.update(); }
        };
    }
})();