Amazon CPU Tamer

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

目前為 2020-12-22 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Amazon CPU Tamer
  3. // @name:ja Amazon CPU Tamer
  4. // @name:zh-CN Amazon CPU Tamer
  5. // @namespace knoa.jp
  6. // @description It reduces CPU usage on Amazon shopping pages. Enjoy your snappy shopping.
  7. // @description:ja AmazonのショッピングページでのCPU使用率を削減します。お買いものをサクサク楽しみましょう。
  8. // @description:zh-CN 减少Amazon购物页面上的CPU利用率。顺利地享受买东西吧。
  9. // @include https://www.amazon.com/*
  10. // @include https://www.amazon.co.jp/*
  11. // @include https://www.amazon.co.uk/*
  12. // @include https://www.amazon.es/*
  13. // @include https://www.amazon.fr/*
  14. // @include https://www.amazon.de/*
  15. // @include https://www.amazon.it/*
  16. // @include https://www.amazon.*
  17. // @include https://*.amazon-*.com/*
  18. // @include https://*.*-amazon.com/*
  19. // @exclude */cart/*
  20. // @exclude */buy/*
  21. // @version 1.4.2
  22. // @grant none
  23. // @run-at document-start
  24. // @antifeature referral-link
  25. // @contributionURL https://paypal.me/kantankikaku
  26. // ==/UserScript==
  27.  
  28. /*
  29. [update]
  30. Added @antifeature referral-link for adopting greasyfork's rule. And minor internal update.
  31.  
  32. [memo]
  33. top:
  34. interval インタラクション要素にも使われるので、1インスタンスにまとめた上で前面タブのみやむなく125msごとに実行。
  35. もっとゆるい頻度にしつつ、click や keydown 時のみ頻度を上げる手はあるが、前面タブで1-2%なら許されるだろう。
  36. ゆるい interval による把握できている問題は詳細画像の切り替え表示だけなので、自分でやっちゃう手もあるが。
  37. timeout インタラクションのみで定常なしなので、そのまま実行してもよい。
  38. iframe(ad):
  39. interval 初期化時のみなのでそのまま実行してもよい。
  40. timeout 定常 100ms が iframe ごとに1つずつなので、1秒ごとに頻度を落とす。
  41. iframe(cloudfront.net ad)
  42. ローカルソースのiframeから事後生成されるのでTampermonkeyが機能しない。
  43. 広告除去することはできるが、このスクリプトの役目ではない。
  44. */
  45. (function(){
  46. const SCRIPTID = 'AmazonCpuTamer';
  47. console.log(SCRIPTID, location.href);
  48. const BUNDLEDINTERVAL = 125;/* the bundled interval */
  49. const BACKGROUNDINTERVAL = 60*1000;/* take even longer interval on hidden tab */
  50. const IFRAMETIMEOUT = 1*1000;/* amazon uses timeouts instead of intervals on iframes */
  51. /*
  52. [interval]
  53. tame quick intervals
  54. */
  55. if(window === top){
  56. /* integrate each of intervals */
  57. const bundle = {};/* {0: {f, interval, lastExecution}} */
  58. let index = 0;/* use it instead of interval id */
  59. let lastExecution = 0;
  60. /* bundle intervals */
  61. const originalSetInterval = window.setInterval.bind(window);
  62. window.setInterval = function(f, interval, ...args){
  63. //console.log(SCRIPTID, 'original interval:', interval, location.href);
  64. bundle[index] = {
  65. f: f.bind(null, ...args),
  66. interval: interval,
  67. lastExecution: 0,
  68. };
  69. return index++;
  70. };
  71. window.clearInterval = function(id){
  72. //console.log(SCRIPTID, 'clearInterval:', id, location.href);
  73. delete bundle[id];
  74. };
  75. /* execute bundled intervals */
  76. /* a bunch of intervals does cost so much even if the processes do nothing */
  77. originalSetInterval(function(){
  78. const now = Date.now();
  79. if(document.hidden && now < lastExecution + BACKGROUNDINTERVAL) return;
  80. Object.keys(bundle).forEach(id => {
  81. const item = bundle[id];
  82. if(item === undefined) return;/* it could be occur on tiny deletion chance */
  83. if(now < item.lastExecution + item.interval) return;/* not yet */
  84. item.f();
  85. item.lastExecution = now;
  86. });
  87. lastExecution = now;
  88. }, BUNDLEDINTERVAL);
  89. }
  90. /*
  91. [timeout]
  92. tame quick timeouts on iframe ads
  93. */
  94. if(window !== top){
  95. const originalSetTimeout = window.setTimeout.bind(window);
  96. window.setTimeout = function(f, timeout, ...args){
  97. if(document.hidden) return;
  98. if(timeout < IFRAMETIMEOUT){
  99. //console.log(SCRIPTID, 'timeout:', timeout, 'to', IFRAMETIMEOUT, location.href);
  100. timeout = IFRAMETIMEOUT;
  101. }
  102. return originalSetTimeout(f, timeout, ...args);
  103. };
  104. }
  105. /*
  106. [associate]
  107. add an associate tag
  108. */
  109. if(window === top){
  110. const IDS = {
  111. 'www.amazon.com': 'knoa-20',
  112. 'www.amazon.co.jp': 'knoa-22',
  113. 'www.amazon.co.uk': 'knoa01-21',
  114. 'www.amazon.es': 'knoa0c-21',
  115. 'www.amazon.fr': 'knoa09-21',
  116. 'www.amazon.de': 'knoa03-21',
  117. 'www.amazon.it': 'knoa0a-21',
  118. };
  119. if(IDS[location.host]){
  120. addTag(IDS[location.host]);
  121. }
  122. function addTag(tag){
  123. const url = new URL(location.href);
  124. if(url.searchParams.get('tag') !== null) return;/* do not overwrite */
  125. console.log(SCRIPTID, 'associate tag:', tag);
  126. document.documentElement.addEventListener('mousedown', function(e){
  127. for(let target = e.target; target; target = target.parentNode){
  128. if(target.href && target.href.startsWith(location.origin)){
  129. const separator = (target.href.includes('?')) ? '&' : '?';
  130. target.href = target.href + separator + 'tag=' + tag;
  131. }
  132. }
  133. });
  134. const separator = (url.search === '') ? '?' : '&';
  135. history.replaceState(null, document.title, location.href + separator + 'tag=' + tag);
  136. }
  137. }
  138. })();