Amazon CPU Tamer

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

Stan na 07-11-2020. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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.*
// @exclude     */cart/*
// @exclude     */buy/*
// @version     1.2.1
// @grant       none
// @run-at      document-start
// ==/UserScript==

/*
[update] 1.2.1
Fix for interaction stability.

*/
(function(){
  const SCRIPTNAME = 'Amazon Cpu Tamer';
  console.log(SCRIPTNAME);
  const TAMEDINTERVAL =  60*1000;/* bunch of intervals does cost so much even if the process does nothing */
  const QUICKRESPONSE =   1*1000;/* some interaction needs rather quick response such as video guids */
  /* tame quick intervals */
  window.originalSetInterval   = window.setInterval;
  window.setInterval = function(f, interval, ...args){
    if(interval < TAMEDINTERVAL){
      console.log(SCRIPTNAME, 'interval:', interval, 'to', TAMEDINTERVAL);
      interval = TAMEDINTERVAL;
      window.setTimeout(f, QUICKRESPONSE, args);
    }
    return window.originalSetInterval(f, interval, args);
  };
  /* add an associate tag */
  switch(location.host){
    case('www.amazon.com'):
      addTag('knoa-20');
      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(SCRIPTNAME, '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);
  }
})();