Amazon CPU Tamer

减少Amazon购物页面上的CPU利用率。顺利地享受买东西吧。

当前为 2020-12-08 提交的版本,查看 最新版本

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