Facebook Anti-Refresh

Prevents Facebook from auto-refreshing the news feed

安装此脚本
作者推荐脚本

您可能也喜欢Facebook Auto Unmute

安装此脚本
  1. // ==UserScript==
  2. // @name Facebook Anti-Refresh
  3. // @namespace CustomScripts
  4. // @description Prevents Facebook from auto-refreshing the news feed
  5. // @author areen-c
  6. // @match *://*.facebook.com/*
  7. // @version 1.2
  8. // @license MIT
  9. // @homepage https://github.com/areen-c
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=facebook.com
  11. // @run-at document-start
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. console.log('[FB Anti-Refresh] Starting...');
  19.  
  20. try {
  21. Object.defineProperty(document, 'hidden', {
  22. configurable: true,
  23. get: () => false
  24. });
  25.  
  26. Object.defineProperty(document, 'visibilityState', {
  27. configurable: true,
  28. get: () => 'visible'
  29. });
  30.  
  31. const originalHasFocus = document.hasFocus;
  32. document.hasFocus = () => true;
  33.  
  34. console.log('[FB Anti-Refresh] Visibility API overridden');
  35. } catch (e) {
  36. console.warn('[FB Anti-Refresh] Could not override visibility API:', e);
  37. }
  38.  
  39. const originalAddEventListener = EventTarget.prototype.addEventListener;
  40. EventTarget.prototype.addEventListener = function(type, listener, options) {
  41. if (type === 'visibilitychange' ||
  42. type === 'webkitvisibilitychange' ||
  43. type === 'mozvisibilitychange') {
  44. return;
  45. }
  46. return originalAddEventListener.call(this, type, listener, options);
  47. };
  48.  
  49. try {
  50. Object.defineProperty(window, 'onblur', {
  51. configurable: true,
  52. get: () => null,
  53. set: () => {}
  54. });
  55.  
  56. Object.defineProperty(window, 'onfocus', {
  57. configurable: true,
  58. get: () => null,
  59. set: () => {}
  60. });
  61. } catch (e) {
  62. console.warn('[FB Anti-Refresh] Could not override window focus events:', e);
  63. }
  64.  
  65. const lastActivity = { time: Date.now() };
  66.  
  67. ['click', 'scroll', 'keypress'].forEach(event => {
  68. document.addEventListener(event, () => {
  69. lastActivity.time = Date.now();
  70. }, { passive: true, capture: true });
  71. });
  72.  
  73. const originalFetch = window.fetch;
  74. window.fetch = function(...args) {
  75. const [url] = args;
  76.  
  77. if (typeof url === 'string' && url.includes('facebook.com')) {
  78. const refreshEndpoints = [
  79. '/ajax/home/generic.php',
  80. '/ajax/pagelet/generic.php/HomeStream',
  81. '/ajax/ticker_stream.php'
  82. ];
  83.  
  84. const isRefreshRequest = refreshEndpoints.some(endpoint =>
  85. url.includes(endpoint)
  86. );
  87.  
  88. if (isRefreshRequest) {
  89. const timeSinceActivity = Date.now() - lastActivity.time;
  90.  
  91. if (timeSinceActivity > 60000) {
  92. console.log('[FB Anti-Refresh] Blocked refresh request');
  93. return Promise.resolve(new Response('{}', {
  94. status: 200,
  95. headers: { 'Content-Type': 'application/json' }
  96. }));
  97. }
  98. }
  99. }
  100.  
  101. return originalFetch.apply(this, args);
  102. };
  103.  
  104. const removeMetaRefresh = () => {
  105. const metaTags = document.querySelectorAll('meta[http-equiv="refresh"]');
  106. metaTags.forEach(tag => {
  107. tag.remove();
  108. console.log('[FB Anti-Refresh] Removed meta refresh tag');
  109. });
  110. };
  111.  
  112. if (document.readyState === 'loading') {
  113. document.addEventListener('DOMContentLoaded', removeMetaRefresh);
  114. } else {
  115. removeMetaRefresh();
  116. }
  117.  
  118. const originalPushState = history.pushState;
  119. history.pushState = function(...args) {
  120. const timeSinceActivity = Date.now() - lastActivity.time;
  121. if (timeSinceActivity > 120000) {
  122. console.log('[FB Anti-Refresh] Blocked history.pushState due to inactivity');
  123. return;
  124. }
  125. return originalPushState.apply(this, args);
  126. };
  127.  
  128. console.log('[FB Anti-Refresh] Protection active');
  129.  
  130. })();