Amazon CPU Tamer

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

当前为 2020-11-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.*
  13. // @exclude */cart/*
  14. // @exclude */buy/*
  15. // @version 1.2.1
  16. // @grant none
  17. // @run-at document-start
  18. // ==/UserScript==
  19.  
  20. /*
  21. [update] 1.2.1
  22. Fix for interaction stability.
  23.  
  24. */
  25. (function(){
  26. const SCRIPTNAME = 'Amazon Cpu Tamer';
  27. console.log(SCRIPTNAME);
  28. const TAMEDINTERVAL = 60*1000;/* bunch of intervals does cost so much even if the process does nothing */
  29. const QUICKRESPONSE = 1*1000;/* some interaction needs rather quick response such as video guids */
  30. /* tame quick intervals */
  31. window.originalSetInterval = window.setInterval;
  32. window.setInterval = function(f, interval, ...args){
  33. if(interval < TAMEDINTERVAL){
  34. console.log(SCRIPTNAME, 'interval:', interval, 'to', TAMEDINTERVAL);
  35. interval = TAMEDINTERVAL;
  36. window.setTimeout(f, QUICKRESPONSE, args);
  37. }
  38. return window.originalSetInterval(f, interval, args);
  39. };
  40. /* add an associate tag */
  41. switch(location.host){
  42. case('www.amazon.com'):
  43. addTag('knoa-20');
  44. break;
  45. case('www.amazon.co.jp'):
  46. addTag('knoa-22');
  47. break;
  48. }
  49. function addTag(tag){
  50. const url = new URL(location.href);
  51. if(url.searchParams.get('tag') !== null) return;/* do not overwrite */
  52. console.log(SCRIPTNAME, 'associate tag:', tag);
  53. document.documentElement.addEventListener('mousedown', function(e){
  54. for(let target = e.target; target; target = target.parentNode){
  55. if(target.href && target.href.startsWith(location.origin)){
  56. const separator = (target.href.includes('?')) ? '&' : '?';
  57. target.href = target.href + separator + 'tag=' + tag;
  58. }
  59. }
  60. });
  61. const separator = (url.search === '') ? '?' : '&';
  62. history.replaceState(null, document.title, location.href + separator + 'tag=' + tag);
  63. }
  64. })();