Amazon CPU Tamer

It reduces CPU usage on Amazon shopping pages. Enjoy your snappy shopping.

Verze ze dne 06. 12. 2020. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        Amazon CPU Tamer
// @name:ja     Amazon CPU Tamer
// @name:zh-CN  Amazon CPU Tamer
// @namespace   knoa.jp
// @contributionURL https://paypal.me/kantankikaku
// @description It reduces CPU usage on Amazon shopping pages. Enjoy your snappy shopping.
// @description:ja AmazonのショッピングページでのCPU使用率を削減します。お買いものをサクサク楽しみましょう。
// @description:zh-CN 减少Amazon购物页面上的CPU利用率。顺利地享受买东西吧。
// @include     https://www.amazon.com/*
// @include     https://www.amazon.co.jp/*
// @include     https://www.amazon.co.uk/*
// @include     https://www.amazon.*
// @include     https://*.amazon-*.com/*
// @include     https://*.*-amazon.com/*
// @exclude     */cart/*
// @exclude     */buy/*
// @version     1.3.0
// @grant       none
// @run-at      document-start
// ==/UserScript==

/*
[update]
Much less CPU usage especialy on internal ads.

[memo]
.com/.co.jp
top:    interval さまざま多数, timeout インタラクションのみで定常なし
iframe: interval なし,         timeout 定常 100ms が iframe ごとに1つずつ
*/
(function(){
  const SCRIPTID = 'AmazonCpuTamer';
  console.log(SCRIPTID, location.href);
  const TAMEDINTERVAL = 60*1000;/* a bunch of intervals does cost so much even if the processes do nothing */
  const QUICKRESPONSE =  1*1000;/* some of the interactions need rather quick response such as video guides */
  const IFRAMETIMEOUT =  1*1000;/* amazon uses timeouts instead of intervals on iframes */
  /* tame quick intervals */
  const originalSetInterval = window.setInterval.bind(window);
  window.setInterval = function(f, interval, ...args){
    if(interval < TAMEDINTERVAL){
      console.log(SCRIPTID, 'interval:', interval, 'to', TAMEDINTERVAL, location.href);
      interval = TAMEDINTERVAL;
      window.setTimeout(f, QUICKRESPONSE, ...args);
    }
    return originalSetInterval(f, interval, ...args);
  };
  /* tame quick timeouts on iframe ads */
  if(window !== top){
    const originalSetTimeout = window.setTimeout.bind(window);
    window.setTimeout = function(f, timeout, ...args){
      if(document.hidden) return;
      if(timeout < IFRAMETIMEOUT){
        console.log(SCRIPTID, 'timeout:', timeout, 'to', IFRAMETIMEOUT, location.href);
        timeout = IFRAMETIMEOUT;
      }
      return originalSetTimeout(f, timeout, ...args);
    };
  }
  /* add an associate tag */
  switch(location.host){
    case('www.amazon.com'):
      addTag('knoa-20');
      break;
    case('www.amazon.co.uk'):
      addTag('knoa-21');
      break;
    case('www.amazon.co.jp'):
      addTag('knoa-22');
      break;
  }
  function addTag(tag){
    const url = new URL(location.href);
    if(url.searchParams.get('tag') !== null) return;/* do not overwrite */
    console.log(SCRIPTID, 'associate tag:', tag);
    document.documentElement.addEventListener('mousedown', function(e){
      for(let target = e.target; target; target = target.parentNode){
        if(target.href && target.href.startsWith(location.origin)){
          const separator = (target.href.includes('?')) ? '&' : '?';
          target.href = target.href + separator + 'tag=' + tag;
        }
      }
    });
    const separator = (url.search === '') ? '?' : '&';
    history.replaceState(null, document.title, location.href + separator + 'tag=' + tag);
  }
})();