图片下载器

可以在绝大多数网站提取并批量下载图片。尤其是类似于千库网、包图网这种,不能右键保存图片或者保存的图片文件格式无法识别,均可以用脚本提取,然后用脚本提供的下载按钮,就可以下载到正确格式的图片文件。其他的淘宝、天猫电商图片批量下载,youtube、B站封面下载,等等都可以的。点击右键-tampermonkey-图片下载器,按这个顺序使用。(目前只适合tampermonkey,其他没试过行不行)

当前为 2021-02-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 图片下载器
  3. // @namespace http://tampermonkey.net/
  4. // @description 可以在绝大多数网站提取并批量下载图片。尤其是类似于千库网、包图网这种,不能右键保存图片或者保存的图片文件格式无法识别,均可以用脚本提取,然后用脚本提供的下载按钮,就可以下载到正确格式的图片文件。其他的淘宝、天猫电商图片批量下载,youtube、B站封面下载,等等都可以的。点击右键-tampermonkey-图片下载器,按这个顺序使用。(目前只适合tampermonkey,其他没试过行不行)
  5. // @version 1.19
  6. // @author 桃源隐叟
  7. // @include *
  8. // @grant GM_openInTab
  9. // @grant GM_download
  10. // @run-at context-menu
  11. // @match *
  12. // @match https://www.bilibili.com/
  13. // @match https://588ku.com/
  14. // @homepageURL https://github.com/taoyuancun123/modifyText/blob/master/modifyText.js
  15. // @supportURL https://greasyfork.org/zh-CN/scripts/419894/feedback
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. try {
  22. document.querySelector(".tyc-image-container").remove();
  23. } catch {
  24.  
  25. }
  26. let imgUrls = [];
  27. let bodyStr = document.body.innerHTML;
  28.  
  29. try {
  30. let imgEles = document.getElementsByTagName("img")
  31.  
  32. for (let i = 0; i < imgEles.length; i++) {
  33. ////console.log(imgEles[i].src);
  34. if (!imgUrls.includes(imgEles[i].src)) {
  35. imgUrls.push(imgEles[i].src);
  36. }
  37.  
  38.  
  39. }
  40.  
  41.  
  42. } catch {
  43. //alert("error");
  44. }
  45.  
  46.  
  47. try {
  48. let imgRegs = bodyStr.match(/(?<=background-image:\s*url\()(\S+)(?=\))/g);
  49.  
  50. for (let i = 0; i < imgRegs.length; i++) {
  51. ////console.log(imgRegs[i]);
  52. if (!imgUrls.includes(imgRegs[i].replace(/&quot;/g, ""))) {
  53. imgUrls.push(imgRegs[i].replace(/&quot;/g, ""));
  54. }
  55.  
  56. }
  57. } catch {
  58. //alert("error");
  59. }
  60.  
  61.  
  62. let imgContainer = `
  63. <div class="tyc-image-container" style="position:fixed;
  64. top:0px;right:0px;width:50vw;z-index:2147483645;
  65. background-color: #dedede;
  66. border: 1px solid #aaa;
  67. overflow:scroll;height:100%;
  68. ">
  69. <div class="control-section" style="width:46vw;z-index:2147483646;
  70. position:fixed;right:30px;
  71. top:0px;display:block;
  72. height:40px;line-height:40px;
  73. background:#eee;border:1px solid #aaa;border-radius:10px;">
  74. <input class="select-all" type="checkbox" name="select-all" value="select-all">全选
  75. <button class="btn-download" style="border:1px solid #aaa;border-radius:5px;
  76. height:32px;line-height:32px;
  77. margin:0px;padding:0 5px;
  78. ">download</button>
  79. <span style="margin-left:10px;">共${imgUrls.length}张图片</span>
  80. <button class="btn-close" style="font-size:20px;position:absolute;
  81. right:30px;top:4px;
  82. height:32px;line-height:32px;
  83. margin:0px;
  84. border-radius:10px;border:1px solid #aaa;
  85. width:30px;">X</button>
  86. </div>
  87. <div class="tyc-image-wrapper" style="margin-top:42px;display:flex;justify-content:center;
  88. align-items:center;flex-wrap:wrap;">
  89. </div>
  90. </div>
  91. `
  92.  
  93. let showBigImage = `
  94. <div class="show-big-image" style="position:fixed;left:30%;top:30%;z-index:2147483647;">
  95. </div>
  96. `
  97.  
  98. document.body.insertAdjacentHTML("afterbegin", imgContainer);
  99.  
  100. document.querySelector(".btn-close").onclick = (e) => {
  101. document.querySelector(".tyc-image-container").remove();
  102. }
  103.  
  104. document.querySelector(".btn-download").onclick = (e) => {
  105. if (document.querySelector(".select-all").checked) {
  106. imgUrls.forEach((img, index) => {
  107. GM_download(img, `pic-${index}`);
  108. });
  109. } else {
  110. alert("请勾选全选,下载全部,或者手动在图片上右键另存为指定图片");
  111. }
  112. }
  113.  
  114.  
  115. imgUrls.forEach((img, index) => {
  116. let insertImg = `<div class="tyc-img-item-container-${index}" style="text-align:center;font-size:0px;
  117. margin:5px;border:1px solid #99d;border-radius:3px;
  118. ">
  119. <img class="tyc-image-preview" src="${img}"/ style="width:auto;height:200px;"></div>`
  120. document.querySelector(".tyc-image-wrapper").insertAdjacentHTML("beforeend", insertImg);
  121. let naturalW = document.querySelector(`.tyc-img-item-container-${index} .tyc-image-preview`).naturalWidth;
  122. let naturalH = document.querySelector(`.tyc-img-item-container-${index} .tyc-image-preview`).naturalHeight;
  123.  
  124. let imgInfoContainer = `
  125. <div style="font-size:0px;background-color:rgba(100,100,100,0.6);height:30px;position:relative;">
  126.  
  127.  
  128. </div>
  129. `;
  130.  
  131. let thisImgContainer = document.querySelector(`.tyc-img-item-container-${index}`);
  132. let imgContainerWidth = thisImgContainer.getBoundingClientRect().width;
  133. let imgInfo = `
  134. <span style="font-size:16px;position:absolute;left:calc(50% - 80px);top:7px;">${naturalW}X${naturalH}</span>
  135. `;
  136.  
  137. let downAndFullBtn = `
  138. <span style="position:absolute;right:calc(50% - 30px);top:2px;border:1px solid #333;
  139. width:26px;height:26px;border-radius:20px;" class="preview-img">
  140. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrows-fullscreen" viewBox="0 0 16 16" style="position:absolute;top:5px;right:5px;">
  141. <path fill-rule="evenodd" d="M5.828 10.172a.5.5 0 0 0-.707 0l-4.096 4.096V11.5a.5.5 0 0 0-1 0v3.975a.5.5 0 0 0 .5.5H4.5a.5.5 0 0 0 0-1H1.732l4.096-4.096a.5.5 0 0 0 0-.707zm4.344 0a.5.5 0 0 1 .707 0l4.096 4.096V11.5a.5.5 0 1 1 1 0v3.975a.5.5 0 0 1-.5.5H11.5a.5.5 0 0 1 0-1h2.768l-4.096-4.096a.5.5 0 0 1 0-.707zm0-4.344a.5.5 0 0 0 .707 0l4.096-4.096V4.5a.5.5 0 1 0 1 0V.525a.5.5 0 0 0-.5-.5H11.5a.5.5 0 0 0 0 1h2.768l-4.096 4.096a.5.5 0 0 0 0 .707zm-4.344 0a.5.5 0 0 1-.707 0L1.025 1.732V4.5a.5.5 0 0 1-1 0V.525a.5.5 0 0 1 .5-.5H4.5a.5.5 0 0 1 0 1H1.732l4.096 4.096a.5.5 0 0 1 0 .707z"/>
  142. </svg>
  143. </span>
  144. <span style="position:absolute;right:calc(50% - 60px);top:2px;border:1px solid #333;
  145. width:26px;height:26px;border-radius:20px;
  146. " class="download-direct">
  147. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-download" viewBox="0 0 16 16" style="position:absolute;top:5px;right:5px;">
  148. <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
  149. <path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
  150. </svg>
  151. </span>
  152.  
  153. `;
  154.  
  155. let downloadBtn=`
  156. <span style="position:absolute;right:calc(50% - 15px);top:2px;border:1px solid #333;
  157. width:26px;height:26px;border-radius:20px;
  158. " class="download-direct">
  159. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-download" viewBox="0 0 16 16" style="position:absolute;top:5px;right:5px;">
  160. <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
  161. <path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
  162. </svg>
  163. </span>
  164. `
  165.  
  166.  
  167. thisImgContainer.insertAdjacentHTML("beforeend", imgInfoContainer);
  168.  
  169. let thisImgInfoContainer = thisImgContainer.querySelector("div");
  170.  
  171. let rectWidth = parseInt(thisImgContainer.getBoundingClientRect().width);
  172.  
  173. if (rectWidth > 120) {
  174. thisImgInfoContainer.insertAdjacentHTML("beforeend", imgInfo);
  175. thisImgInfoContainer.insertAdjacentHTML("beforeend", downAndFullBtn);
  176. } else if (rectWidth <= 120 && rectWidth >= 50) {
  177. thisImgInfoContainer.insertAdjacentHTML("beforeend", downAndFullBtn);
  178. thisImgInfoContainer.getElementsByClassName("preview-img")[0].style.right = "50%";
  179. thisImgInfoContainer.getElementsByClassName("download-direct")[0].style.right = "calc(50% - 30px)";
  180.  
  181.  
  182. }else{
  183. thisImgInfoContainer.insertAdjacentHTML("beforeend", downloadBtn);
  184. }
  185.  
  186.  
  187. ////console.log(img);
  188. });
  189.  
  190. document.body.onclick = (e) => {
  191. //console.log(e);
  192. if ((e.target.nodeName == "IMG" && e.target.className === "tyc-image-preview") || e.target.classList[1] == "bi-arrows-fullscreen" || e.path.find(isPreview) != undefined) {
  193. let imgContainer = e.path.find(
  194.  
  195. (ele) => {
  196. try {
  197. //console.log(ele);
  198. return ele.className.includes("tyc-img-item-container");
  199. }
  200. catch {
  201.  
  202. }
  203.  
  204. }
  205. )
  206. let path = imgContainer.getElementsByTagName("img")[0].src;
  207.  
  208. try {
  209. let container = document.querySelector(".show-big-image");
  210. if (container.getElementsByTagName("img")[0].src === path) {
  211. container.remove();
  212. return;
  213. } else {
  214. container.remove();
  215. }
  216. }
  217. catch {
  218.  
  219. }
  220. document.body.insertAdjacentHTML("beforeend", showBigImage);
  221.  
  222. let showItem = `<img src="${path}"/>`
  223.  
  224. document.querySelector(".show-big-image").insertAdjacentHTML("beforeend", showItem);
  225.  
  226. let tempImg = document.querySelector(".show-big-image img");
  227.  
  228. let dWidth = (window.innerWidth - tempImg.width) / 2;
  229. let dHeight = (window.innerHeight - tempImg.height) / 2;
  230.  
  231. document.querySelector(".show-big-image").style.left = dWidth + "px";
  232. document.querySelector(".show-big-image").style.top = dHeight + "px";
  233. } else if (e.target.parentElement.className === "show-big-image") {
  234. try {
  235. document.querySelector(".show-big-image").remove();
  236. }
  237. catch
  238. {
  239.  
  240. }
  241.  
  242. } else if (e.target.classList[1] == "bi-download" || e.path.find(isDownload) != undefined) {
  243. let imgContainer = e.path.find(
  244.  
  245. (ele) => {
  246. try {
  247. //console.log(ele);
  248. return ele.className.includes("tyc-img-item-container");
  249. }
  250. catch {
  251.  
  252. }
  253.  
  254. }
  255. )
  256. let path = imgContainer.getElementsByTagName("img")[0].src;
  257. var filename;
  258. if (path.indexOf("/") > 0)//如果包含有"/"号 从最后一个"/"号+1的位置开始截取字符串
  259. {
  260. filename = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
  261. }
  262. else {
  263. filename = path;
  264. }
  265. //console.log("download start" + path + " " + filename);
  266. GM_download(path, "pic");
  267. }
  268. }
  269.  
  270. function isDownload(ele) {
  271. return ele.className == "download-direct";
  272. }
  273.  
  274. function isPreview(ele) {
  275. return ele.className == "preview-img";
  276. }
  277. })();