图片下载器

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

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

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