Greasy Fork

Greasy Fork is available in English.

Facebook Anti-Refresh

Prevents Facebook from auto-refreshing the news feed

当前为 2025-05-13 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 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.0
// @license      MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=facebook.com
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let wasHidden = false;
    
    const originalHidden = Object.getOwnPropertyDescriptor(Document.prototype, 'hidden');
    const originalVisibilityState = Object.getOwnPropertyDescriptor(Document.prototype, 'visibilityState');
    
    Object.defineProperty(document, 'hidden', {
        get: function() {
            return false;
        }
    });
    
    Object.defineProperty(document, 'visibilityState', {
        get: function() {
            return 'visible';
        }
    });
    
    const originalAddEventListener = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function(type, listener, options) {
        if (type === 'visibilitychange') {
            return;
        }
        return originalAddEventListener.call(this, type, listener, options);
    };
    
    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        const url = args[0];
        
        if (typeof url === 'string' && url.includes('facebook.com')) {
            console.log('Intercepted request:', url);
            
            if (url.includes('/api/graphql') || 
                url.includes('/ajax/pagelet') || 
                url.includes('/ajax/bulk-route-definitions')) {
                
                const now = Date.now();
                if (window.lastInteraction && (now - window.lastInteraction) > 60000) {
                    console.log('Blocking potential refresh request');
                    return Promise.resolve(new Response('{}', {
                        status: 200,
                        headers: { 'Content-Type': 'application/json' }
                    }));
                }
            }
        }
        
        return originalFetch.apply(this, args);
    };
    
    window.lastInteraction = Date.now();
    ['click', 'scroll', 'keypress'].forEach(event => {
        document.addEventListener(event, () => {
            window.lastInteraction = Date.now();
        });
    });
    
    const originalScrollTo = window.scrollTo;
    const originalScroll = window.scroll;
    
    window.scrollTo = function(x, y) {
        if (y === 0 && window.pageYOffset > 100) {
            console.log('Prevented scroll to top');
            return;
        }
        return originalScrollTo.apply(this, arguments);
    };
    
    window.scroll = function(x, y) {
        if (y === 0 && window.pageYOffset > 100) {
            console.log('Prevented scroll to top');
            return;
        }
        return originalScroll.apply(this, arguments);
    };
    
    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            if (mutation.type === 'childList' && mutation.removedNodes.length > 5) {
                const feed = document.querySelector('[role="feed"]');
                if (feed && feed.children.length < 5) {
                    console.log('Detected potential feed refresh, attempting to prevent');
                }
            }
        }
    });
    
    window.addEventListener('load', () => {
        const targetNode = document.querySelector('[role="main"]') || document.body;
        observer.observe(targetNode, { childList: true, subtree: true });
        console.log('Facebook Anti-Refresh script loaded');
    });
    
})();