Disable setInterval Globally

禁止所有网站执行 setInterval 函数

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Disable setInterval Globally
// @namespace    http://tampermonkey.net/
// @version      1.0
// @license MIT
// @description  禁止所有网站执行 setInterval 函数
// @author       Your Name
// @match        *://*/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建一个脚本元素,将重写代码注入到页面上下文中
    const script = document.createElement('script');
    script.textContent = `
        (function() {
            // 检查是否已经重写过 setInterval,避免重复
            if (window.setInterval.originalSetInterval) {
                return;
            }

            // 保存原始的 setInterval 方法
            window.setInterval.originalSetInterval = window.setInterval;

            // 重写 setInterval 方法
            window.setInterval = function(callback, delay, ...args) {
                console.warn('setInterval 被禁止执行。');
                // 可以选择抛出错误,阻止执行
                // throw new Error('setInterval 被禁止执行。');

                // 或者简单地返回一个无效的 ID
                return null;
            };

            // 可选:冻结 setInterval 方法,防止再次被修改
            Object.defineProperty(window, 'setInterval', {
                writable: false,
                configurable: false
            });

            console.log('window.setInterval 方法已被重写以禁止其执行。');
        })();
    `;

    // 将脚本元素添加到文档中
    document.documentElement.appendChild(script);

    // 移除脚本元素以避免污染 DOM
    script.parentNode.removeChild(script);
})();