Modified_WorkerTimer

Needs to setInterval/setTimeout times working normally

2022-09-09 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/451050/1091555/Modified_WorkerTimer.js

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @license      MIT
// @name         Modified_WorkerTimer
// @version      1.2.1
// @description  Needs to setInterval/setTimeout times working normally
// @author       mohayonao
// @match        http://*/*
// @match        https://*/*
// @grant        none
// @run-at       document-start
// @namespace    https://gist.github.com/mohayonao/50547c5a51a7ca71c90d
// ==/UserScript==

window.WorkerTimer = (function() {
  const global = window;
  var TIMER_WORKER_SOURCE = [
    "var timerIds = {}, _ = {};",
    "_.setInterval = function(args) {",
    "  timerIds[args.timerId] = setInterval(function() { postMessage(args.timerId); }, args.delay);",
    "};",
    "_.clearInterval = function(args) {",
    "  clearInterval(timerIds[args.timerId]);",
    "};",
    "_.setTimeout = function(args) {",
    "  timerIds[args.timerId] = setTimeout(function() { postMessage(args.timerId); }, args.delay);",
    "};",
    "_.clearTimeout = function(args) {",
    "  clearTimeout(timerIds[args.timerId]);",
    "};",
    "onmessage = function(e) { _[e.data.type](e.data) };"
  ].join("");

  var _timerId = 0;
  var _callbacks = {};
  var _timer = new global.Worker(global.URL.createObjectURL(
    new global.Blob([ TIMER_WORKER_SOURCE ], { type: "text/javascript" })
  ));

  _timer.onmessage = function(e) {
    if (_callbacks[e.data]) {
      _callbacks[e.data].callback.apply(null, _callbacks[e.data].params);
    }
  };

  return {
    setInterval: function(callback, delay) {
      var params = Array.prototype.slice.call(arguments, 2);

      _timerId += 50;

      _timer.postMessage({ type: "setInterval", timerId: _timerId, delay: delay });
      _callbacks[_timerId] = { callback: callback, params: params };

      return _timerId;
    },
    setTimeout: function(callback, delay) {
      var params = Array.prototype.slice.call(arguments, 4);

      _timerId += 50;

      _timer.postMessage({ type: "setTimeout", timerId: _timerId, delay: delay });
      _callbacks[_timerId] = { callback: callback, params: params };

      return _timerId;
    },
    clearInterval: function(timerId) {
      _timer.postMessage({ type: "clearInterval", timerId: timerId });
      _callbacks[timerId] = null;
    },
    clearTimeout: function(timerId) {
      _timer.postMessage({ type: "clearTimeout", timerId: timerId });
      _callbacks[timerId] = null;
    }
  };
})();