// ==UserScript== // @name 图片下载器 // @namespace http://tampermonkey.net/ // @description 可以在绝大多数网站提取并批量下载图片。尤其是类似于千库网、包图网这种,不能右键保存图片或者保存的图片文件格式无法识别,均可以用脚本提取,然后用脚本提供的下载按钮,就可以下载到正确格式的图片文件。其他的淘宝、天猫电商图片批量下载,youtube、B站封面下载,等等都可以的。点击右键-tampermonkey-图片下载器,按这个顺序使用。(目前只适合油猴-tampermonkey,其他没试过行不行) // @version 1.29 // @author 桃源隐叟 // @include * // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // @grant GM_download // @run-at context-menu // @match * // @match https://www.bilibili.com/ // @match https://588ku.com/ // @homepageURL https://github.com/taoyuancun123/modifyText/blob/master/modifyText.js // @supportURL https://greasyfork.org/zh-CN/scripts/419894/feedback // @downloadURL none // ==/UserScript== (function () { 'use strict'; try { document.querySelector(".tyc-image-container").remove(); } catch { } var imgUrls = []; var bodyStr = document.body.innerHTML; var imgSelected = []; var imgWaitDownload = []; var widthFilter = { min: 0, max: 3000 }; var heightFilter = { min: 0, max: 3000 }; var tempImgUrls=[]; try { let imgEles = document.getElementsByTagName("img") for (let i = 0; i < imgEles.length; i++) { ////console.log(imgEles[i].src); if (!imgUrls.includes(imgEles[i].src)) { imgUrls.push(imgEles[i].src); } } } catch { //alert("error"); } try { let imgRegs = bodyStr.match(/(?<=background-image:\s*url\()(\S+)(?=\))/g); for (let i = 0; i < imgRegs.length; i++) { ////console.log(imgRegs[i]); if (!imgUrls.includes(imgRegs[i].replace(/"/g, ""))) { imgUrls.push(imgRegs[i].replace(/"/g, "")); } } } catch { //alert("error"); } let imgContainer = `
全选 已选(0/${imgUrls.length})张图片

Width: -
Height: -

` let showBigImage = `
` document.body.insertAdjacentHTML("afterbegin", imgContainer); if (window.location.href.includes("hathitrust.org")) { //imgUrls=[]; let imgs = document.querySelectorAll(".image img"); if (imgs.length > 0) { let canvas = document.createElement("canvas"); imgUrls = []; for (let pi = 0; pi < imgs.length; pi++) { canvas.width = imgs[pi].width; canvas.height = imgs[pi].height; canvas.getContext("2d").drawImage(imgs[pi], 0, 0); imgUrls.push(canvas.toDataURL("image/png")); } document.querySelector(".select-all").style = "position:relative;width:15px;height:15px;" } else { } } document.body.onclick = (e) => { //console.log(e); if ((e.target.nodeName == "IMG" && e.target.className === "tyc-image-preview")) { let imgContainer = e.path.find( (ele) => { try { //console.log(ele); return ele.className.includes("tyc-img-item-container"); } catch { } } ) let path = imgContainer.getElementsByTagName("img")[0].src; try { let container = document.querySelector(".show-big-image"); if (container.getElementsByTagName("img")[0].src === path) { container.remove(); return; } else { container.remove(); } } catch { } document.body.insertAdjacentHTML("beforeend", showBigImage); let showItem = `` document.querySelector(".show-big-image").insertAdjacentHTML("beforeend", showItem); let tempImg = document.querySelector(".show-big-image img"); let dWidth = (window.innerWidth - tempImg.width) / 2; let dHeight = (window.innerHeight - tempImg.height) / 2; document.querySelector(".show-big-image").style.left = dWidth + "px"; document.querySelector(".show-big-image").style.top = dHeight + "px"; } else if (e.target.parentElement.className === "show-big-image") { try { document.querySelector(".show-big-image").remove(); } catch { } } else if (e.target.classList[1] == "bi-download" || e.path.find(isDownload) != undefined) { let imgContainer = e.path.find( (ele) => { try { //console.log(ele); return ele.className.includes("tyc-img-item-container"); } catch { } } ) let path = imgContainer.getElementsByTagName("img")[0].src; var filename; if (path.indexOf("/") > 0)//如果包含有"/"号 从最后一个"/"号+1的位置开始截取字符串 { filename = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf(".")); } else { filename = path; } //console.log("download start" + path + " " + filename); GM_download(path, "pic"); } else if (e.target.classList[1] == "bi-check" || e.path.find(isSelect) != undefined) { let checkSvg = e.path.find((ele) => ele.classList[1] === "bi-check"); let currentImg = parseInt(checkSvg.dataset.value); let container = e.path.find((ele) => ele.className === `tyc-img-item-container-${currentImg}`); if (imgSelected.includes(currentImg)) { imgSelected.splice(imgSelected.indexOf(currentImg), 1); checkSvg.style.color = "black"; container.style.border = "1px solid #99d"; } else { imgSelected.push(currentImg); checkSvg.style.color = "white"; container.style.border = "1px solid white"; } document.querySelector(".num-tip").innerText = `已选(${imgSelected.length}/${imgUrls.length})张图片`; transIndexToLink(tempImgUrls); } } document.querySelector(".btn-close").onclick = (e) => { document.querySelector(".tyc-image-container").remove(); } document.querySelector(".btn-download").onclick = (e) => { /* if (document.querySelector(".select-all").checked) { imgUrls.forEach((img, index) => { GM_download(img, `pic-${index}`); }); } else { alert("请勾选全选,下载全部,或者手动在图片上右键另存为指定图片"); } */ if (imgWaitDownload.length >= 1) { imgWaitDownload.forEach((img, index) => { console.log(index); console.log(img); GM_download(img, `pic-${index}`); }); } else { alert("请至少选中一张图片。"); } } document.body.onchange = (e) => { if (e.target.className.includes("width-check")) { GM_setValue('width-check',e.target.checked); } if (e.target.className.includes("height-check")) { GM_setValue('height-check',e.target.checked); } if(e.target.nodeName==="INPUT" && e.target.type==="text" && e.target.className.includes("value")){ GM_setValue(e.target.className,e.target.value); } (e.target.className.includes("width-check")||e.target.className.includes("height-check")|| (e.target.nodeName==="INPUT" && e.target.type==="text" && e.target.className.includes("value"))) &&(clean(),init()); } document.querySelector(".select-all").onchange = (e) => { if (document.querySelector(".select-all").checked) { imgWaitDownload = tempImgUrls; } else { transIndexToLink(tempImgUrls); } document.querySelector(".num-tip").innerText = `已选(${imgWaitDownload.length}/${imgUrls.length})张图片`; } init(); function init() { tempImgUrls=imgUrls; getSavedValue(); if (document.querySelector(".width-check").checked) { tempImgUrls=tempImgUrls.filter(filterByWidth); } if (document.querySelector(".height-check").checked) { tempImgUrls=tempImgUrls.filter(filterByHeight); } showImage(tempImgUrls); } function clean(){ imgWaitDownload=[]; imgSelected=[]; document.querySelector(".num-tip").innerText = `已选(${imgSelected.length}/${imgUrls.length})张图片`; document.querySelector(".tyc-image-wrapper").innerHTML=""; } function isDownload(ele) { return ele.className == "download-direct"; } function isSelect(ele) { return ele.className == "select-image"; } function transIndexToLink(tempImgUrls) { imgWaitDownload = []; imgSelected.forEach((imgIndex, index) => { imgWaitDownload.push(tempImgUrls[imgIndex]); }); } function showImage(filtedImgUrls) { filtedImgUrls.forEach((img, index) => { if(window.location.href.includes("huaban.com")){ if(img.includes("/webp")){ img=img.replace(/\/webp/g,"/png"); } } let insertImg = `
` document.querySelector(".tyc-image-wrapper").insertAdjacentHTML("beforeend", insertImg); let naturalW = document.querySelector(`.tyc-img-item-container-${index} .tyc-image-preview`).naturalWidth; let naturalH = document.querySelector(`.tyc-img-item-container-${index} .tyc-image-preview`).naturalHeight; let imgInfoContainer = `
`; let thisImgContainer = document.querySelector(`.tyc-img-item-container-${index}`); let imgContainerWidth = thisImgContainer.getBoundingClientRect().width; let imgInfo = ` ${naturalW}X${naturalH} `; /* */ let downAndFullBtn = ` `; let downloadBtn = ` ` thisImgContainer.insertAdjacentHTML("beforeend", imgInfoContainer); let thisImgInfoContainer = thisImgContainer.querySelector("div"); let rectWidth = parseInt(thisImgContainer.getBoundingClientRect().width); if (rectWidth > 120) { thisImgInfoContainer.insertAdjacentHTML("beforeend", imgInfo); thisImgInfoContainer.insertAdjacentHTML("beforeend", downAndFullBtn); } else if (rectWidth <= 120 && rectWidth >= 50) { thisImgInfoContainer.insertAdjacentHTML("beforeend", downAndFullBtn); thisImgInfoContainer.getElementsByClassName("select-image")[0].style.right = "50%"; thisImgInfoContainer.getElementsByClassName("download-direct")[0].style.right = "calc(50% - 30px)"; } else { thisImgInfoContainer.insertAdjacentHTML("beforeend", downloadBtn); } ////console.log(img); }); } function filterByWidth(src) { let tempImg=new Image(); tempImg.src=src; if(tempImg.width>=parseInt(document.querySelector(".width-value-min").value) && tempImg.width<=parseInt(document.querySelector(".width-value-max").value)) { return src; } } function filterByHeight(src) { let tempImg=new Image(); tempImg.src=src; if(tempImg.height>=parseInt(document.querySelector(".height-value-min").value) && tempImg.height<=parseInt(document.querySelector(".height-value-max").value)) { return src; } } function getSavedValue(){ GM_getValue("width-check")&&(document.querySelector(".width-check").checked=GM_getValue("width-check")); GM_getValue("height-check")&&(document.querySelector(".height-check").checked=GM_getValue("height-check")); GM_getValue("width-value-min")&&(document.querySelector(".width-value-min").value=GM_getValue("width-value-min")); GM_getValue("width-value-max")&&(document.querySelector(".width-value-max").value=GM_getValue("width-value-max")); GM_getValue("height-value-min")&&(document.querySelector(".height-value-min").value=GM_getValue("height-value-min")); GM_getValue("height-value-max")&&(document.querySelector(".height-value-max").value=GM_getValue("height-value-max")); } })();