Facebook Anti-Refresh

Prevents Facebook from auto-refreshing the news feed

安裝腳本?
作者推薦腳本

您可能也會喜歡 Facebook Auto Unmute

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

(function() {
    'use strict';
    
    Object.defineProperty(document, 'visibilityState', {
        get: function() {
            return 'visible';
        }
    });
    
    Object.defineProperty(document, 'hidden', {
        get: function() {
            return false;
        }
    });
    
    if (typeof document.mozHidden !== "undefined") {
        Object.defineProperty(document, 'mozHidden', {
            get: function() {
                return false;
            }
        });
    }
    
    if (typeof document.webkitHidden !== "undefined") {
        Object.defineProperty(document, 'webkitHidden', {
            get: function() {
                return false;
            }
        });
    }
    
    const originalAddEventListener = EventTarget.prototype.addEventListener;
    
    document.addEventListener = function(type, listener, options) {
        if (type === 'visibilitychange' || 
            type === 'webkitvisibilitychange' || 
            type === 'mozvisibilitychange') {
            return;
        }
        return originalAddEventListener.call(this, type, listener, options);
    };
    
    window.addEventListener = function(type, listener, options) {
        if (type === 'blur' || type === 'mouseleave') {
            return;
        }
        return originalAddEventListener.call(this, type, listener, options);
    };
    
    Object.defineProperty(window, 'onblur', {
        get: function() { return null; },
        set: function() { }
    });
    
    Object.defineProperty(window, 'onfocus', {
        get: function() { return null; },
        set: function() { }
    });
    
    Object.defineProperty(document, 'onvisibilitychange', {
        get: function() { return null; },
        set: function() { }
    });
    
    const originalHasFocus = document.hasFocus;
    document.hasFocus = function() {
        return true;
    };
    
    const originalFetch = window.fetch;
    window.fetch = function(...args) {
        const url = args[0];
        
        if (typeof url === 'string' && url.includes('facebook.com')) {
            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) {
                    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();
        }, true);
    });
})();