Imgsrc.ru Downloader

Mass downloads all images from a collection on one page

  1. // ==UserScript==
  2. // @name Imgsrc.ru Downloader
  3. // @namespace https://imgsrc.ru
  4. // @version 1.0.0
  5. // @description Mass downloads all images from a collection on one page
  6. // @author You
  7. // @match https://imgsrc.ru/main/tape.php*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=imgsrc.ru
  9. // @grant GM_xmlhttpRequest
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. if (typeof GM_download !== 'function') {
  14. if (typeof GM_xmlhttpRequest !== 'function') {
  15. throw new Error('GM_xmlhttpRequest is undefined. Please set @grant GM_xmlhttpRequest at metadata block.');
  16. }
  17.  
  18. function GM_download (url, name) {
  19. if (url == null) return;
  20.  
  21. var data = {
  22. method: 'GET',
  23. responseType: 'arraybuffer',
  24.  
  25. onload: function (res) {
  26. var blob = new Blob([res.response], {type: 'application/octet-stream'});
  27. var url = URL.createObjectURL(blob); // blob url
  28.  
  29. var a = document.createElement('a');
  30. a.setAttribute('href', url);
  31. a.setAttribute('download', data.name != null ? data.name : 'filename');
  32. document.documentElement.appendChild(a);
  33.  
  34. // call download
  35. // a.click() or CLICK the download link can't modify filename in Firefox (why?)
  36. // Solution from FileSaver.js, https://github.com/eligrey/FileSaver.js/
  37. var e = new MouseEvent('click');
  38. a.dispatchEvent(e);
  39.  
  40. document.documentElement.removeChild(a);
  41.  
  42. setTimeout(function(){
  43. // reduce memory usage
  44. URL.revokeObjectURL(url);
  45. if ('close' in blob) blob.close(); // File Blob.close() API, not supported by all the browser right now
  46. blob = undefined;
  47. }, 1000);
  48.  
  49. if (typeof data.onafterload === 'function') data.onafterload(); // call onload function
  50. }
  51.  
  52. // error object of onerror function is not supported right now
  53. };
  54.  
  55. if (typeof url === 'string') {
  56. data.url = url;
  57. data.name = name;
  58. }
  59. else {
  60. if (url instanceof Object === false) return;
  61.  
  62. // as documentation, you can only use [url, name, headers, saveAs, onload, onerror] function, but we won't check them
  63. // Notice: saveAs is not supported
  64. if (url.url == null) return;
  65.  
  66. for (var i in url) {
  67. if (i === 'onload') data.onafterload = url.onload; // onload function support
  68. else data[i] = url[i];
  69. }
  70. }
  71.  
  72. // it returns this GM_xhr, thought not mentioned in documentation
  73. return GM_xmlhttpRequest(data);
  74. }
  75. }
  76.  
  77. (function() {
  78. 'use strict';
  79.  
  80. const imageUrls = []
  81. const imageElements = document.querySelectorAll('.fts')
  82.  
  83. const imageDiv = document.querySelector('.h100')
  84.  
  85. for (let i = 0; i < imageElements.length; i++) {
  86. const url = 'https://' + imageElements[i].getAttribute('src');
  87. imageUrls.push(url)
  88. }
  89.  
  90. const buttonDiv = document.createElement("div")
  91. buttonDiv.style.display = 'flex'
  92. buttonDiv.style['justify-content'] = 'center'
  93.  
  94.  
  95. const btn = document.createElement("button")
  96. btn.innerText = 'Download all images on this page'
  97. btn.id = 'download'
  98. btn.style.color = '#fff'
  99. btn.style.background = '#000'
  100. btn.style.border = 'none'
  101. btn.style.padding = '20px'
  102. btn.style['border-radius'] = '15px'
  103.  
  104. buttonDiv.append(btn)
  105.  
  106.  
  107. imageDiv.prepend(buttonDiv)
  108.  
  109. btn.addEventListener("mouseenter", () => {
  110. btn.style.background = '#4e4e4e'
  111. btn.style.cursor = 'pointer'
  112. })
  113. btn.addEventListener("mouseleave", () => {
  114. btn.style.background = '#000'
  115. })
  116.  
  117. btn.addEventListener("click", () => {
  118. for (let i = 0; i < imageUrls.length; i++) {
  119. GM_download({
  120. url: imageUrls[i],
  121. name: imageUrls[i].split('/')[imageUrls[i].split('/').length - 1],
  122. saveAs: false
  123. });
  124. }
  125. })
  126.  
  127.  
  128. })();