images predownload

predownload the images in web

当前为 2024-12-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name images predownload
  3. // @license MIT
  4. // @namespace https://github.com/xgame-0/tamper-monkey-script
  5. // @version 2024-12-23
  6. // @description predownload the images in web
  7. // @author xgame-0
  8. // @match *://hmjidi.com/*
  9. // @match *://aman3.com/*
  10. // @match *://aman3.org/*
  11. // @match *://aman5.com/*
  12. // @match *://aman5.org/*
  13. // @match *://aman7.com/*
  14. // @match *://aman7.org/*
  15. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  16. // @grant none
  17. // ==/UserScript==
  18.  
  19. 'use strict';
  20.  
  21. let CONFIGS = [{
  22. // host: ['aman5.com', 'aman5.org', 'aman7.com', 'aman7.org'],
  23. regExp: /aman[0-9]\.(com|org)/,
  24. attr: 'data-original',
  25. }, {
  26. host: ['hmjidi.com'],
  27. attr: 'data-src',
  28. }]
  29.  
  30. function checkConfigHost(cfgHost) {
  31. if (!Array.isArray(cfgHost)) {
  32. let host = window.location.host;
  33. return cfgHost && host.indexOf(cfgHost) >= 0
  34. }
  35.  
  36. for (let i in cfgHost) {
  37. if (checkConfigHost(cfgHost[i])) {
  38. return true;
  39. }
  40. }
  41. return false
  42. }
  43.  
  44. function checkConfigRegExp(re) {
  45. if (!Array.isArray(re)) {
  46. let host = window.location.host;
  47. return re && re.test(host)
  48. }
  49.  
  50. for (let i in re) {
  51. if (checkConfigRegExp(re[i])) {
  52. return true;
  53. }
  54. }
  55. return false
  56. }
  57.  
  58. function getConfig() {
  59. 'use strict';
  60.  
  61. let res = [];
  62. for (let i in CONFIGS) {
  63. let cfg = CONFIGS[i];
  64. if (cfg.host && checkConfigHost(cfg.host)) {
  65. res.push(cfg)
  66. } else if (cfg.regExp && checkConfigRegExp(cfg.regExp)) {
  67. res.push(cfg)
  68. }
  69. }
  70. return res;
  71. }
  72.  
  73. function predownloadImage(attrName) {
  74. 'use strict';
  75.  
  76. let l = document.querySelectorAll('img');
  77. // console.log('images:', l);
  78. l = l || [];
  79. l.forEach((e) => {
  80. console.log('image:', e);
  81. let attrValue = e.getAttribute(attrName);
  82. console.log('attr:', attrValue);
  83. if (!attrValue) {
  84. return;
  85. }
  86. window.setTimeout(function () {
  87. let img = new Image();
  88. img.src = attrValue;
  89. });
  90. })
  91. }
  92.  
  93. function main(ev) {
  94. console.log('event:', ev);
  95. let cfgs = getConfig() || [];
  96. console.log('host:', window.location.host, ', cfgs:', cfgs)
  97.  
  98. cfgs.every(cfg => {
  99. console.log('cfg:', cfg);
  100. predownloadImage(cfg.attr);
  101. })
  102. };
  103.  
  104. // console.log('state:', document.readyState);
  105. if (['complete', 'loaded', 'interactive'].indexOf(document.readyState) >= 0) {
  106. // console.log('state:', document.readyState);
  107. main();
  108. }
  109. else {
  110. window.addEventListener('DOMContentLoaded', main);
  111. window.addEventListener('load', main);
  112. }
  113. // setTimeout(main, 0);