图片下载器

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

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

  1. // ==UserScript==
  2. // @name:zh-CN 图片下载器
  3. // @name:zh-TW 图片下载器
  4. // @name Image Downloader
  5. // @namespace http://tampermonkey.net/
  6. // @description You can extract and download pictures in batches on most websites.Right Click-tampermonkey-Image Downloader,use in this order.(currently only suitable for tampermonkey and chrome,others have not tried it)
  7. // @description:zh-CN 可以在绝大多数网站提取并批量下载图片。尤其是类似于千库网、包图网这种,不能右键保存图片或者保存的图片文件格式无法识别,均可以用脚本提取,然后用脚本提供的下载按钮,就可以下载到正确格式的图片文件。其他的淘宝、天猫电商图片批量下载,youtube、B站封面下载,等等都可以的。点击右键-tampermonkey-图片下载器,按这个顺序使用。(目前只适合chrome+tampermonkey,其他组合多少有问题)
  8. // @description:zh-TW 可以在绝大多数网站提取并批量下载图片。尤其是类似于千库网、包图网这种,不能右键保存图片或者保存的图片文件格式无法识别,均可以用脚本提取,然后用脚本提供的下载按钮,就可以下载到正确格式的图片文件。其他的淘宝、天猫电商图片批量下载,youtube、B站封面下载,等等都可以的。点击右键-tampermonkey-图片下载器,按这个顺序使用。(目前只适合chrome+tampermonkey,其他组合多少有问题)
  9. // @version 1.56
  10. // @author 桃源隐叟
  11. // @include *
  12. // @grant GM_openInTab
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // @grant GM_download
  16. // @run-at context-menu
  17. // @match *
  18. // @match https://www.bilibili.com/
  19. // @match https://588ku.com/
  20. // @homepageURL https://github.com/taoyuancun123/modifyText/blob/master/modifyText.js
  21. // @supportURL https://greasyfork.org/zh-CN/scripts/419894/feedback
  22. // ==/UserScript==
  23. (function () {
  24. 'use strict';
  25. try {
  26. document.querySelector(".tyc-image-container").remove();
  27. } catch {
  28. }
  29. var imgUrls = [];
  30. var bodyStr = document.body.innerHTML;
  31. var imgSelected = [];
  32. var imgWaitDownload = [];
  33. var widthFilter = { min: 0, max: 3000 };
  34. var heightFilter = { min: 0, max: 3000 };
  35. var tempImgUrls=[];
  36. try {
  37. let imgEles = document.getElementsByTagName("img")
  38. for (let i = 0; i < imgEles.length; i++) {
  39. ////console.log(imgEles[i].src);
  40. if (!imgUrls.includes(imgEles[i].src)) {
  41. imgUrls.push(imgEles[i].src);
  42. }
  43. }
  44. } catch {
  45. //alert("error");
  46. }
  47. try {
  48. let imgRegs = bodyStr.match(/(?<=background-image:\s*url\()(\S+)(?=\))/g);
  49. for (let i = 0; i < imgRegs.length; i++) {
  50. ////console.log(imgRegs[i]);
  51. if (!imgUrls.includes(imgRegs[i].replace(/&quot;/g, ""))) {
  52. imgUrls.push(imgRegs[i].replace(/&quot;/g, ""));
  53. }
  54. }
  55. } catch {
  56. //alert("error");
  57. }
  58. let imgContainer = `
  59. <div class="tyc-image-container" style="position:fixed;
  60. top:0px;right:0px;width:50vw;z-index:2147483645;
  61. background-color: #dedede;
  62. border: 1px solid #aaa;
  63. overflow:scroll;height:100%;
  64. ">
  65. <div class="control-section" style="width:46vw;z-index:2147483646;
  66. position:fixed;right:30px;
  67. top:0px;display:block;
  68. height:80px;line-height:40px;
  69. background:#eee;border:1px solid #aaa;border-radius:2px;">
  70. <input class="select-all" type="checkbox" name="select-all" value="select-all">全选
  71. <button class="btn-download" style="border:1px solid #aaa;border-radius:5px;
  72. height:32px;line-height:32px;
  73. margin:0px;padding:0 5px;
  74. ">download</button>
  75. <span style="margin-left:10px;" class="num-tip">已选(0/${imgUrls.length})张图片</span>
  76. <button class="btn-close" style="font-size:20px;position:absolute;
  77. right:30px;top:4px;
  78. height:32px;line-height:32px;
  79. margin:0px;
  80. border-radius:10px;border:1px solid #aaa;
  81. width:30px;">X</button>
  82. <p style="line-height:12px;">
  83. <div style="float:left;">
  84. <input type="checkbox" class="width-check img-check" name="width-check" value="width-check">Width:
  85. <input type="text" class="width-value-min" size="1" style="height:15px;width:50px;"
  86. min="0" max="9999" value="0">-
  87. <input type="text" class="width-value-max" size="1" style="height:15px;width:50px;"
  88. min="0" max="9999" value="3000">
  89. </div>
  90. <div style="float:left;margin-left:30px;">
  91. <input type="checkbox" class="height-check img-check" name="height-check" value="height-check">Height:
  92. <input type="text" class="height-value-min" size="1" style="height:15px;width:50px;"
  93. min="0" max="9999" value="0">-
  94. <input type="text" class="height-value-max" size="1" style="height:15px;width:50px;"
  95. min="0" max="9999" value="3000">
  96. </div>
  97. </p>
  98. </div>
  99. <div class="tyc-image-wrapper" style="margin-top:82px;display:flex;justify-content:center;
  100. align-items:center;flex-wrap:wrap;">
  101. </div>
  102. </div>
  103. `
  104. let showBigImage = `
  105. <div class="show-big-image" style="position:fixed;left:30%;top:30%;z-index:2147483647;">
  106. </div>
  107. `
  108. document.body.insertAdjacentHTML("afterbegin", imgContainer);
  109. if (window.location.href.includes("hathitrust.org")) {
  110. //imgUrls=[];
  111. let imgs = document.querySelectorAll(".image img");
  112. if (imgs.length > 0) {
  113. let canvas = document.createElement("canvas");
  114. imgUrls = [];
  115. for (let pi = 0; pi < imgs.length; pi++) {
  116. canvas.width = imgs[pi].width;
  117. canvas.height = imgs[pi].height;
  118. canvas.getContext("2d").drawImage(imgs[pi], 0, 0);
  119. imgUrls.push(canvas.toDataURL("image/png"));
  120. }
  121. document.querySelector(".select-all").style = "position:relative;width:15px;height:15px;"
  122. } else {
  123. }
  124. }
  125. document.body.onclick = (e) => {
  126. //console.log(e);
  127. if ((e.target.nodeName == "IMG" && e.target.className === "tyc-image-preview")) {
  128. let imgContainer = e.path.find(
  129. (ele) => {
  130. try {
  131. //console.log(ele);
  132. return ele.className.includes("tyc-img-item-container");
  133. }
  134. catch {
  135. }
  136. }
  137. )
  138. let path = imgContainer.getElementsByTagName("img")[0].src;
  139. try {
  140. let container = document.querySelector(".show-big-image");
  141. if (container.getElementsByTagName("img")[0].src === path) {
  142. container.remove();
  143. return;
  144. } else {
  145. container.remove();
  146. }
  147. }
  148. catch {
  149. }
  150. document.body.insertAdjacentHTML("beforeend", showBigImage);
  151. let showItem = `<img src="${path}"/>`
  152. document.querySelector(".show-big-image").insertAdjacentHTML("beforeend", showItem);
  153. let tempImg = document.querySelector(".show-big-image img");
  154. let dWidth = (window.innerWidth - tempImg.width) / 2;
  155. let dHeight = (window.innerHeight - tempImg.height) / 2;
  156. document.querySelector(".show-big-image").style.left = dWidth + "px";
  157. document.querySelector(".show-big-image").style.top = dHeight + "px";
  158. } else if (e.target.parentElement.className === "show-big-image") {
  159. try {
  160. document.querySelector(".show-big-image").remove();
  161. }
  162. catch
  163. {
  164. }
  165. } else if (e.target.classList[1] == "bi-download" || e.path.find(isDownload) != undefined) {
  166. let imgContainer = e.path.find(
  167. (ele) => {
  168. try {
  169. //console.log(ele);
  170. return ele.className.includes("tyc-img-item-container");
  171. }
  172. catch {
  173. }
  174. }
  175. )
  176. let path = imgContainer.getElementsByTagName("img")[0].src;
  177. var filename;
  178. if (path.indexOf("/") > 0)//如果包含有"/"号 从最后一个"/"号+1的位置开始截取字符串
  179. {
  180. filename = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
  181. }
  182. else {
  183. filename = path;
  184. }
  185. //console.log("download start" + path + " " + filename);
  186. GM_download(path, "pic");
  187. } else if (e.target.classList[1] == "bi-check" || e.path.find(isSelect) != undefined) {
  188. let checkSvg = e.path.find((ele) => ele.classList[1] === "bi-check");
  189. let currentImg = parseInt(checkSvg.dataset.value);
  190. let container = e.path.find((ele) => ele.className === `tyc-img-item-container-${currentImg}`);
  191. if (imgSelected.includes(currentImg)) {
  192. imgSelected.splice(imgSelected.indexOf(currentImg), 1);
  193. checkSvg.style.color = "black";
  194. container.style.border = "1px solid #99d";
  195. } else {
  196. imgSelected.push(currentImg);
  197. checkSvg.style.color = "white";
  198. container.style.border = "1px solid white";
  199. }
  200. document.querySelector(".num-tip").innerText = `已选(${imgSelected.length}/${imgUrls.length})张图片`;
  201. transIndexToLink(tempImgUrls);
  202. }
  203. }
  204. document.querySelector(".btn-close").onclick = (e) => {
  205. document.querySelector(".tyc-image-container").remove();
  206. }
  207. document.querySelector(".btn-download").onclick = (e) => {
  208. /* if (document.querySelector(".select-all").checked) {
  209. imgUrls.forEach((img, index) => {
  210. GM_download(img, `pic-${index}`);
  211. });
  212. } else {
  213. alert("请勾选全选,下载全部,或者手动在图片上右键另存为指定图片");
  214. } */
  215. if (imgWaitDownload.length >= 1) {
  216. imgWaitDownload.forEach(async (img, index) => {
  217. console.log(index);
  218. console.log(img);
  219. await GM_download(img, `pic-${index}`);
  220. });
  221. } else {
  222. alert("请至少选中一张图片。");
  223. }
  224. }
  225. document.body.onchange = (e) => {
  226. if (e.target.className.includes("width-check")) {
  227. GM_setValue('width-check',e.target.checked);
  228. }
  229. if (e.target.className.includes("height-check")) {
  230. GM_setValue('height-check',e.target.checked);
  231. }
  232. if(e.target.nodeName==="INPUT" && e.target.type==="text" && e.target.className.includes("value")){
  233. GM_setValue(e.target.className,e.target.value);
  234. }
  235. (e.target.className.includes("width-check")||e.target.className.includes("height-check")||
  236. (e.target.nodeName==="INPUT" && e.target.type==="text" && e.target.className.includes("value")))
  237. &&(clean(),init());
  238. }
  239. document.querySelector(".select-all").onchange = (e) => {
  240. if (document.querySelector(".select-all").checked) {
  241. imgWaitDownload = tempImgUrls;
  242. } else {
  243. transIndexToLink(tempImgUrls);
  244. }
  245. document.querySelector(".num-tip").innerText = `已选(${imgWaitDownload.length}/${imgUrls.length})张图片`;
  246. }
  247. init();
  248. function init() {
  249. tempImgUrls=imgUrls;
  250. getSavedValue();
  251. if (document.querySelector(".width-check").checked) {
  252. tempImgUrls=tempImgUrls.filter(filterByWidth);
  253. }
  254. if (document.querySelector(".height-check").checked) {
  255. tempImgUrls=tempImgUrls.filter(filterByHeight);
  256. }
  257. showImage(tempImgUrls);
  258. }
  259. function clean(){
  260. imgWaitDownload=[];
  261. imgSelected=[];
  262. document.querySelector(".num-tip").innerText = `已选(${imgSelected.length}/${imgUrls.length})张图片`;
  263. document.querySelector(".tyc-image-wrapper").innerHTML="";
  264. }
  265. function isDownload(ele) {
  266. return ele.className == "download-direct";
  267. }
  268. function isSelect(ele) {
  269. return ele.className == "select-image";
  270. }
  271. function transIndexToLink(tempImgUrls) {
  272. imgWaitDownload = [];
  273. imgSelected.forEach((imgIndex, index) => {
  274. imgWaitDownload.push(tempImgUrls[imgIndex]);
  275. });
  276. }
  277. function showImage(filtedImgUrls) {
  278. filtedImgUrls.forEach((img, index) => {
  279. if(window.location.href.includes("huaban.com")){
  280. if(img.includes("/webp")){
  281. img=img.replace(/\/webp/g,"/png");
  282. }
  283. }
  284. let insertImg = `<div class="tyc-img-item-container-${index}" style="text-align:center;font-size:0px;
  285. margin:5px;border:1px solid #99d;border-radius:3px;
  286. ">
  287. <img class="tyc-image-preview" src="${img}"/ style="width:auto;height:200px;"></div>`
  288. document.querySelector(".tyc-image-wrapper").insertAdjacentHTML("beforeend", insertImg);
  289. let naturalW = document.querySelector(`.tyc-img-item-container-${index} .tyc-image-preview`).naturalWidth;
  290. let naturalH = document.querySelector(`.tyc-img-item-container-${index} .tyc-image-preview`).naturalHeight;
  291. let imgInfoContainer = `
  292. <div style="font-size:0px;background-color:rgba(100,100,100,0.6);height:30px;position:relative;">
  293. </div>
  294. `;
  295. let thisImgContainer = document.querySelector(`.tyc-img-item-container-${index}`);
  296. let imgContainerWidth = thisImgContainer.getBoundingClientRect().width;
  297. let imgInfo = `
  298. <span style="font-size:16px;position:absolute;left:calc(50% - 80px);top:7px;">${naturalW}X${naturalH}</span>
  299. `;
  300. /*
  301. <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;">
  302. <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"/>
  303. </svg>*/
  304. let downAndFullBtn = `
  305. <span style="position:absolute;right:calc(50% - 30px);top:2px;border:1px solid #333;
  306. width:26px;height:26px;border-radius:20px;" class="select-image" data-value="${index}">
  307. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check" viewBox="0 0 16 16" style="position:absolute;top:-1px;right:-2px;width:30px;height:30px;" data-value="${index}">
  308. <path d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z"/>
  309. </svg>
  310. </span>
  311. <span style="position:absolute;right:calc(50% - 60px);top:2px;border:1px solid #333;
  312. width:26px;height:26px;border-radius:20px;
  313. " class="download-direct">
  314. <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;">
  315. <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"/>
  316. <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"/>
  317. </svg>
  318. </span>
  319. `;
  320. let downloadBtn = `
  321. <span style="position:absolute;right:calc(50% - 15px);top:2px;border:1px solid #333;
  322. width:26px;height:26px;border-radius:20px;
  323. " class="download-direct">
  324. <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;">
  325. <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"/>
  326. <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"/>
  327. </svg>
  328. </span>
  329. `
  330. thisImgContainer.insertAdjacentHTML("beforeend", imgInfoContainer);
  331. let thisImgInfoContainer = thisImgContainer.querySelector("div");
  332. let rectWidth = parseInt(thisImgContainer.getBoundingClientRect().width);
  333. if (rectWidth > 120) {
  334. thisImgInfoContainer.insertAdjacentHTML("beforeend", imgInfo);
  335. thisImgInfoContainer.insertAdjacentHTML("beforeend", downAndFullBtn);
  336. } else if (rectWidth <= 120 && rectWidth >= 50) {
  337. thisImgInfoContainer.insertAdjacentHTML("beforeend", downAndFullBtn);
  338. thisImgInfoContainer.getElementsByClassName("select-image")[0].style.right = "50%";
  339. thisImgInfoContainer.getElementsByClassName("download-direct")[0].style.right = "calc(50% - 30px)";
  340. } else {
  341. thisImgInfoContainer.insertAdjacentHTML("beforeend", downloadBtn);
  342. }
  343. ////console.log(img);
  344. });
  345. }
  346. function filterByWidth(src) {
  347. let tempImg=new Image();
  348. tempImg.src=src;
  349. if(tempImg.width>=parseInt(document.querySelector(".width-value-min").value)
  350. && tempImg.width<=parseInt(document.querySelector(".width-value-max").value))
  351. {
  352. return src;
  353. }
  354. }
  355. function filterByHeight(src) {
  356. let tempImg=new Image();
  357. tempImg.src=src;
  358. if(tempImg.height>=parseInt(document.querySelector(".height-value-min").value)
  359. && tempImg.height<=parseInt(document.querySelector(".height-value-max").value))
  360. {
  361. return src;
  362. }
  363. }
  364. function getSavedValue(){
  365. GM_getValue("width-check")&&(document.querySelector(".width-check").checked=GM_getValue("width-check"));
  366. GM_getValue("height-check")&&(document.querySelector(".height-check").checked=GM_getValue("height-check"));
  367. GM_getValue("width-value-min")&&(document.querySelector(".width-value-min").value=GM_getValue("width-value-min"));
  368. GM_getValue("width-value-max")&&(document.querySelector(".width-value-max").value=GM_getValue("width-value-max"));
  369. GM_getValue("height-value-min")&&(document.querySelector(".height-value-min").value=GM_getValue("height-value-min"));
  370. GM_getValue("height-value-max")&&(document.querySelector(".height-value-max").value=GM_getValue("height-value-max"));
  371. }
  372. })();