Greasy Fork

Greasy Fork is available in English.

Facebook Anti-Refresh

Prevents Facebook from auto-refreshing the news feed

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                 Facebook Anti-Refresh
// @namespace            CustomScripts
// @description          Prevents Facebook from auto-refreshing the news feed
// @author               areen-c
// @match                *://*.facebook.com/*
// @version              1.2
// @license              MIT
// @homepage             https://github.com/areen-c
// @icon                 https://www.google.com/s2/favicons?sz=64&domain=facebook.com
// @run-at               document-start
// @grant                none
// ==/UserScript==

(function() {
    'use strict';

    console.log('[FB Anti-Refresh] Starting...');

    try {
        Object.defineProperty(document, 'hidden', {
            configurable: true,
            get: () => false
        });

        Object.defineProperty(document, 'visibilityState', {
            configurable: true,
            get: () => 'visible'
        });

        const originalHasFocus = document.hasFocus;
        document.hasFocus = () => true;

        console.log('[FB Anti-Refresh] Visibility API overridden');
    } catch (e) {
        console.warn('[FB Anti-Refresh] Could not override visibility API:', e);
    }

    const originalAddEventListener = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function(type, listener, options) {
        if (type === 'visibilitychange' ||
            type === 'webkitvisibilitychange' ||
            type === 'mozvisibilitychange') {
            return;
        }
        return originalAddEventListener.call(this, type, listener, options);
    };

    try {
        Object.defineProperty(window, 'onblur', {
            configurable: true,
            get: () => null,
            set: () => {}
        });

        Object.defineProperty(window, 'onfocus', {
            configurable: true,
            get: () => null,
            set: () => {}
        });
    } catch (e) {
        console.warn('[FB Anti-Refresh] Could not override window focus events:', e);
    }

    const lastActivity = { time: Date.now() };

    ['click', 'scroll', 'keypress'].forEach(event => {
        document.addEventListener(event, () => {
            lastActivity.time = Date.now();
        }, { passive: true, capture: true });
    });

    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        const [url] = args;

        if (typeof url === 'string' && url.includes('facebook.com')) {
            const refreshEndpoints = [
                '/ajax/home/generic.php',
                '/ajax/pagelet/generic.php/HomeStream',
                '/ajax/ticker_stream.php'
            ];

            const isRefreshRequest = refreshEndpoints.some(endpoint =>
                url.includes(endpoint)
            );

            if (isRefreshRequest) {
                const timeSinceActivity = Date.now() - lastActivity.time;

                if (timeSinceActivity > 60000) {
                    console.log('[FB Anti-Refresh] Blocked refresh request');
                    return Promise.resolve(new Response('{}', {
                        status: 200,
                        headers: { 'Content-Type': 'application/json' }
                    }));
                }
            }
        }

        return originalFetch.apply(this, args);
    };

    const removeMetaRefresh = () => {
        const metaTags = document.querySelectorAll('meta[http-equiv="refresh"]');
        metaTags.forEach(tag => {
            tag.remove();
            console.log('[FB Anti-Refresh] Removed meta refresh tag');
        });
    };

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', removeMetaRefresh);
    } else {
        removeMetaRefresh();
    }

    const originalPushState = history.pushState;
    history.pushState = function(...args) {
        const timeSinceActivity = Date.now() - lastActivity.time;
        if (timeSinceActivity > 120000) {
            console.log('[FB Anti-Refresh] Blocked history.pushState due to inactivity');
            return;
        }
        return originalPushState.apply(this, args);
    };

    console.log('[FB Anti-Refresh] Protection active');

})();