Amazon CPU Tamer

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

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

  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. // @exclude */cart/*
  17. // @exclude */buy/*
  18. // @version 1.3.1
  19. // @grant none
  20. // @run-at document-start
  21. // ==/UserScript==
  22.  
  23. /*
  24. [update]
  25. Minor fix.
  26.  
  27. [memo]
  28. top: interval さまざま多数, timeout インタラクションのみで定常なし
  29. iframe: interval なし, timeout 定常 100ms が iframe ごとに1つずつ
  30. */
  31. (function(){
  32. const SCRIPTID = 'AmazonCpuTamer';
  33. console.log(SCRIPTID, location.href);
  34. const TAMEDINTERVAL = 60*1000;/* a bunch of intervals does cost so much even if the processes do nothing */
  35. const QUICKRESPONSE = 1*1000;/* some of the interactions need rather quick response such as video guides */
  36. const IFRAMETIMEOUT = 1*1000;/* amazon uses timeouts instead of intervals on iframes */
  37. /* tame quick intervals */
  38. const originalSetInterval = window.setInterval.bind(window);
  39. window.setInterval = function(f, interval, ...args){
  40. if(interval < TAMEDINTERVAL){
  41. console.log(SCRIPTID, 'interval:', interval, 'to', TAMEDINTERVAL, location.href);
  42. interval = TAMEDINTERVAL;
  43. window.setTimeout(f, QUICKRESPONSE, ...args);
  44. }
  45. return originalSetInterval(f, interval, ...args);
  46. };
  47. /* tame quick timeouts on iframe ads */
  48. if(window !== top){
  49. const originalSetTimeout = window.setTimeout.bind(window);
  50. window.setTimeout = function(f, timeout, ...args){
  51. if(document.hidden) return;
  52. if(timeout < IFRAMETIMEOUT){
  53. //console.log(SCRIPTID, 'timeout:', timeout, 'to', IFRAMETIMEOUT, location.href);
  54. timeout = IFRAMETIMEOUT;
  55. }
  56. return originalSetTimeout(f, timeout, ...args);
  57. };
  58. }
  59. /* add an associate tag */
  60. switch(location.host){
  61. case('www.amazon.com'):
  62. addTag('knoa-20');
  63. break;
  64. case('www.amazon.co.uk'):
  65. addTag('knoa-21');
  66. break;
  67. case('www.amazon.co.jp'):
  68. addTag('knoa-22');
  69. break;
  70. }
  71. function addTag(tag){
  72. const url = new URL(location.href);
  73. if(url.searchParams.get('tag') !== null) return;/* do not overwrite */
  74. console.log(SCRIPTID, 'associate tag:', tag);
  75. document.documentElement.addEventListener('mousedown', function(e){
  76. for(let target = e.target; target; target = target.parentNode){
  77. if(target.href && target.href.startsWith(location.origin)){
  78. const separator = (target.href.includes('?')) ? '&' : '?';
  79. target.href = target.href + separator + 'tag=' + tag;
  80. }
  81. }
  82. });
  83. const separator = (url.search === '') ? '?' : '&';
  84. history.replaceState(null, document.title, location.href + separator + 'tag=' + tag);
  85. }
  86. })();