Amazon CPU Tamer

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

当前为 2020-11-17 提交的版本,查看 最新版本

  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.3
  16. // @grant none
  17. // @run-at document-start
  18. // ==/UserScript==
  19.  
  20. /*
  21. [update] 1.2.3
  22. Add a UK associate ID.
  23.  
  24. */
  25. (function(){
  26. const SCRIPTNAME = 'Amazon Cpu Tamer';
  27. console.log(SCRIPTNAME);
  28. const TAMEDINTERVAL = 60*1000;/* a bunch of intervals does cost so much even if the processes do nothing */
  29. const QUICKRESPONSE = 1*1000;/* some of the interactions need rather quick response such as video guides */
  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.uk'):
  46. addTag('knoa-21');
  47. break;
  48. case('www.amazon.co.jp'):
  49. addTag('knoa-22');
  50. break;
  51. }
  52. function addTag(tag){
  53. const url = new URL(location.href);
  54. if(url.searchParams.get('tag') !== null) return;/* do not overwrite */
  55. console.log(SCRIPTNAME, 'associate tag:', tag);
  56. document.documentElement.addEventListener('mousedown', function(e){
  57. for(let target = e.target; target; target = target.parentNode){
  58. if(target.href && target.href.startsWith(location.origin)){
  59. const separator = (target.href.includes('?')) ? '&' : '?';
  60. target.href = target.href + separator + 'tag=' + tag;
  61. }
  62. }
  63. });
  64. const separator = (url.search === '') ? '?' : '&';
  65. history.replaceState(null, document.title, location.href + separator + 'tag=' + tag);
  66. }
  67. })();