图片下载器

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

目前為 2021-06-01 提交的版本,檢視 最新版本

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