图片下载器

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

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

  1. // ==UserScript==
  2. // @name 图片下载器
  3. // @namespace http://tampermonkey.net/
  4. // @description 可以在绝大多数网站提取并批量下载图片。尤其是类似于千库网、包图网这种,不能右键保存图片或者保存的图片文件格式无法识别,均可以用脚本提取,然后用脚本提供的下载按钮,就可以下载到正确格式的图片文件。其他的淘宝、天猫电商图片批量下载,youtube、B站封面下载,等等都可以的。点击右键-tampermonkey-图片下载器,按这个顺序使用。(目前只适合tampermonkey,其他没试过行不行)
  5. // @version 1.18
  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.  
  156. thisImgContainer.insertAdjacentHTML("beforeend", imgInfoContainer);
  157.  
  158. let thisImgInfoContainer = thisImgContainer.querySelector("div");
  159.  
  160. let rectWidth = parseInt(thisImgContainer.getBoundingClientRect().width);
  161.  
  162. if (rectWidth > 120) {
  163. thisImgInfoContainer.insertAdjacentHTML("beforeend", imgInfo);
  164. thisImgInfoContainer.insertAdjacentHTML("beforeend", downAndFullBtn);
  165. } else if (rectWidth <= 120 && rectWidth >= 50) {
  166. thisImgInfoContainer.insertAdjacentHTML("beforeend", downAndFullBtn);
  167. thisImgInfoContainer.getElementsByClassName("preview-img")[0].style.right = "50%";
  168. thisImgInfoContainer.getElementsByClassName("download-direct")[0].style.right = "calc(50% - 30px)";
  169.  
  170.  
  171. }
  172.  
  173.  
  174. ////console.log(img);
  175. });
  176.  
  177. document.body.onclick = (e) => {
  178. //console.log(e);
  179. if ((e.target.nodeName == "IMG" && e.target.className === "tyc-image-preview") || e.target.classList[1] == "bi-arrows-fullscreen" || e.path.find(isPreview) != undefined) {
  180. let imgContainer = e.path.find(
  181.  
  182. (ele) => {
  183. try {
  184. //console.log(ele);
  185. return ele.className.includes("tyc-img-item-container");
  186. }
  187. catch {
  188.  
  189. }
  190.  
  191. }
  192. )
  193. let path = imgContainer.getElementsByTagName("img")[0].src;
  194.  
  195. try {
  196. let container = document.querySelector(".show-big-image");
  197. if (container.getElementsByTagName("img")[0].src === path) {
  198. container.remove();
  199. return;
  200. } else {
  201. container.remove();
  202. }
  203. }
  204. catch {
  205.  
  206. }
  207. document.body.insertAdjacentHTML("beforeend", showBigImage);
  208.  
  209. let showItem = `<img src="${path}"/>`
  210.  
  211. document.querySelector(".show-big-image").insertAdjacentHTML("beforeend", showItem);
  212.  
  213. let tempImg = document.querySelector(".show-big-image img");
  214.  
  215. let dWidth = (window.innerWidth - tempImg.width) / 2;
  216. let dHeight = (window.innerHeight - tempImg.height) / 2;
  217.  
  218. document.querySelector(".show-big-image").style.left = dWidth + "px";
  219. document.querySelector(".show-big-image").style.top = dHeight + "px";
  220. } else if (e.target.parentElement.className === "show-big-image") {
  221. try {
  222. document.querySelector(".show-big-image").remove();
  223. }
  224. catch
  225. {
  226.  
  227. }
  228.  
  229. } else if (e.target.classList[1] == "bi-download" || e.path.find(isDownload) != undefined) {
  230. let imgContainer = e.path.find(
  231.  
  232. (ele) => {
  233. try {
  234. //console.log(ele);
  235. return ele.className.includes("tyc-img-item-container");
  236. }
  237. catch {
  238.  
  239. }
  240.  
  241. }
  242. )
  243. let path = imgContainer.getElementsByTagName("img")[0].src;
  244. var filename;
  245. if (path.indexOf("/") > 0)//如果包含有"/"号 从最后一个"/"号+1的位置开始截取字符串
  246. {
  247. filename = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
  248. }
  249. else {
  250. filename = path;
  251. }
  252. //console.log("download start" + path + " " + filename);
  253. GM_download(path, "pic");
  254. }
  255. }
  256.  
  257. function isDownload(ele) {
  258. return ele.className == "download-direct";
  259. }
  260.  
  261. function isPreview(ele) {
  262. return ele.className == "preview-img";
  263. }
  264. })();