Facebook Anti-Refresh

Prevents Facebook from auto-refreshing the news feed

目前為 2025-05-13 提交的版本,檢視 最新版本

// ==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');
    });
    
})();