HackTimer V2

Uses a Web Worker to ensure high-precision timing for setInterval and setTimeout, minimizing throttling.

目前為 2025-10-21 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/552983/1681317/HackTimer%20V2.js

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @license      MIT
// @name         HackTimer V2
// @namespace    HackTimer
// @version      1.1.2 (Gemini version)
// @description  Uses a Web Worker to ensure high-precision timing for setInterval and setTimeout, minimizing throttling.
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function () {
    const logPrefix = 'HackTimer V2: ';
    let workerScriptUrl = null;

    // --- 1. Generate Worker Script Blob URL ---
    try {
        const workerCode = `
            var fakeIdToId = {};
            onmessage = function (event) {
                var data = event.data;
                var name = data.name;
                var fakeId = data.fakeId;
                // Ensure time is treated as a number, defaulting to 0
                var time = data.time || 0; 

                switch (name) {
                    case 'setInterval':
                        fakeIdToId[fakeId] = setInterval(function () {
                            postMessage({ fakeId: fakeId });
                        }, time);
                        break;
                    case 'clearInterval':
                        if (fakeIdToId[fakeId]) {
                            clearInterval(fakeIdToId[fakeId]);
                            delete fakeIdToId[fakeId];
                        }
                        break;
                    case 'setTimeout':
                        fakeIdToId[fakeId] = setTimeout(function () {
                            postMessage({ fakeId: fakeId });
                            // Timeouts clear themselves after firing
                            delete fakeIdToId[fakeId]; 
                        }, time);
                        break;
                    case 'clearTimeout':
                        if (fakeIdToId[fakeId]) {
                            clearTimeout(fakeIdToId[fakeId]);
                            delete fakeIdToId[fakeId];
                        }
                        break;
                }
            };
        `;
        const blob = new Blob([workerCode], { type: 'application/javascript' });
        workerScriptUrl = window.URL.createObjectURL(blob);
    } catch (error) {
        console.warn(logPrefix + 'Blob creation failed. Web Worker replacement disabled.', error);
        return; // Cannot proceed without Blob support
    }

    if (typeof Worker === 'undefined') {
        console.warn(logPrefix + 'Web Workers not supported. Replacement disabled.');
        if (workerScriptUrl) window.URL.revokeObjectURL(workerScriptUrl);
        return;
    }

    let worker;
    const fakeIdToCallback = new Map();
    let lastFakeId = 1;

    // --- 2. ID Generation ---
    function getFakeId() {
        // Simple incrementing ID, wrapping safely (using a large limit)
        lastFakeId = (lastFakeId % 0xFFFFFFF) + 1; 
        while (fakeIdToCallback.has(lastFakeId)) {
            lastFakeId++;
        }
        return lastFakeId;
    }

    // --- 3. Callback Execution Logic ---
    function executeCallback(fakeId) {
        const request = fakeIdToCallback.get(fakeId);
        if (!request) return;

        const { callback, parameters, isTimeout } = request;
        
        if (isTimeout) {
            fakeIdToCallback.delete(fakeId);
        }

        let callbackFunc = callback;
        
        // Handle string callbacks (legacy support)
        if (typeof callback === 'string') {
            try {
                // eslint-disable-next-line no-new-func
                callbackFunc = new Function(callback);
            } catch (error) {
                console.error(logPrefix + 'Error parsing string callback:', error);
                return;
            }
        }

        if (typeof callbackFunc === 'function') {
            try {
                // Execute in the global scope
                callbackFunc.apply(window, parameters);
            } catch (error) {
                console.error(logPrefix + 'Error executing callback:', error);
            }
        }
    }

    // --- 4. Worker Initialization and Overrides ---
    try {
        worker = new Worker(workerScriptUrl);
        
        // Store originals (useful if users mix native and hacked timers)
        const originalClearInterval = window.clearInterval;
        const originalClearTimeout = window.clearTimeout;

        // Override setInterval
        window.setInterval = function (callback, time, ...parameters) {
            const fakeId = getFakeId();
            fakeIdToCallback.set(fakeId, {
                callback: callback,
                parameters: parameters,
                isTimeout: false
            });
            worker.postMessage({
                name: 'setInterval',
                fakeId: fakeId,
                time: Math.max(0, time || 0)
            });
            return fakeId;
        };

        // Override clearInterval
        window.clearInterval = function (fakeId) {
            if (fakeIdToCallback.has(fakeId)) {
                fakeIdToCallback.delete(fakeId);
                worker.postMessage({
                    name: 'clearInterval',
                    fakeId: fakeId
                });
            } else if (typeof fakeId === 'number' && fakeId > 0) {
                // Allow clearing native timers if the ID wasn't tracked by us
                originalClearInterval(fakeId);
            }
        };

        // Override setTimeout
        window.setTimeout = function (callback, time, ...parameters) {
            const fakeId = getFakeId();
            fakeIdToCallback.set(fakeId, {
                callback: callback,
                parameters: parameters,
                isTimeout: true
            });
            worker.postMessage({
                name: 'setTimeout',
                fakeId: fakeId,
                time: Math.max(0, time || 0)
            });
            return fakeId;
        };

        // Override clearTimeout
        window.clearTimeout = function (fakeId) {
            if (fakeIdToCallback.has(fakeId)) {
                fakeIdToCallback.delete(fakeId);
                worker.postMessage({
                    name: 'clearTimeout',
                    fakeId: fakeId
                });
            } else if (typeof fakeId === 'number' && fakeId > 0) {
                // Allow clearing native timers
                originalClearTimeout(fakeId);
            }
        };

        // Worker Message Handlers
        worker.onmessage = function (event) {
            const { fakeId } = event.data;
            executeCallback(fakeId);
        };

        worker.onerror = function (event) {
            console.error(logPrefix + 'Worker runtime error:', event);
        };
        
        // Clean up the temporary Blob URL
        window.URL.revokeObjectURL(workerScriptUrl);
        
    } catch (error) {
        console.error(logPrefix + 'Initialisation failed. Using standard timers.', error);
        if (workerScriptUrl) window.URL.revokeObjectURL(workerScriptUrl);
    }
})();