图片下载器

某些网站图片,右键不能保存,使用此脚本,提取后可直接右键保存,如千库网、B战封面、淘宝天猫等网站。点击右键后,即可看到图片下载选项。

当前为 2021-01-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 图片下载器
  3. // @name:en Image downloader
  4. // @namespace http://tampermonkey.net/
  5. // @description 某些网站图片,右键不能保存,使用此脚本,提取后可直接右键保存,如千库网、B战封面、淘宝天猫等网站。点击右键后,即可看到图片下载选项。
  6. // @description:en It can extract the images of most websites, such as qianku.com, baotu.com, huaban.com, and the video cover of YouTube / BiliBili. Right click to see the image extraction options
  7. // @version 1.5
  8. // @author 桃源隐叟
  9. // @include *
  10. // @grant GM_openInTab
  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.  
  20. (function () {
  21. 'use strict';
  22.  
  23. try{
  24. document.querySelector(".tyc-image-container").remove();
  25. }catch{
  26.  
  27. }
  28. let imgUrls = [];
  29. let bodyStr = document.body.innerHTML;
  30.  
  31. try {
  32. let imgEles = document.getElementsByTagName("img")
  33.  
  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.  
  41. }
  42.  
  43.  
  44. } catch {
  45. //alert("error");
  46. }
  47.  
  48.  
  49. try {
  50. let imgRegs = bodyStr.match(/(?<=background-image:\s*url\()(\S+)(?=\))/g);
  51.  
  52. for (let i = 0; i < imgRegs.length; i++) {
  53. //console.log(imgRegs[i]);
  54. if (!imgUrls.includes(imgRegs[i].replace(/&quot;/g, ""))) {
  55. imgUrls.push(imgRegs[i].replace(/&quot;/g, ""));
  56. }
  57.  
  58. }
  59. } catch {
  60. //alert("error");
  61. }
  62.  
  63.  
  64. let imgContainer = `
  65. <div class="tyc-image-container" style="position:fixed;
  66. top:0px;right:0px;width:50vw;display:flex;justify-content:center;
  67. align-items:center;flex-wrap:wrap;z-index:2147483646;
  68. background-color: #dedede;
  69. border: 1px solid #aaa;
  70. overflow:scroll;height:100%;
  71. ">
  72. <div class="control-section" style="width:50%;position:fixed;right:0;top:0px;">
  73. <input class="select-all" type="checkbox" name="select-all" value="select-all">全选
  74. <button class="btn-download" style="height:30px;border:1px solid #aaa;border-radius:5px;padding:5px;">download</button>
  75. <button class="btn-close" style="font-size:30px;position:fixed;top:0px;right:30px;border-radius:10px;border:1px solid #aaa;width:50px;">X</button>
  76. </div>
  77. </div>
  78. `
  79.  
  80. let showBigImage=`
  81. <div class="show-big-image" style="position:fixed;left:30%;top:30%;z-index:2147483647;">
  82. </div>
  83. `
  84.  
  85. document.body.insertAdjacentHTML("afterbegin", imgContainer);
  86.  
  87. document.querySelector(".btn-close").onclick=(e)=>{
  88. document.querySelector(".tyc-image-container").remove();
  89. }
  90.  
  91. document.querySelector(".btn-download").onclick=(e)=>{
  92. if(document.querySelector(".select-all").checked){
  93. imgUrls.forEach((img,index) => {
  94. GM_download(img, `pic-${index}`);
  95. });
  96. }else{
  97. alert("请勾选全选,下载全部,或者手动在图片上右键另存为指定图片");
  98. }
  99. }
  100.  
  101.  
  102. imgUrls.forEach((img,index) => {
  103. let insertImg = `<div class="tyc-img-item-container-${index}" style="text-align:center;font-size:12px;margin:5px;">
  104. <img class="tyc-image-preview" src="${img}"/ style="width:auto;height:200px;"></div>`
  105. document.querySelector(".tyc-image-container").insertAdjacentHTML("beforeend", insertImg);
  106. let naturalW=document.querySelector(`.tyc-img-item-container-${index} .tyc-image-preview`).naturalWidth;
  107. let naturalH=document.querySelector(`.tyc-img-item-container-${index} .tyc-image-preview`).naturalHeight;
  108.  
  109. let imgInfo=`<p style="line-height:14px;height:14px;font-size:12px;">${naturalW}X${naturalH}</p>`;
  110. document.querySelector(`.tyc-img-item-container-${index}`).insertAdjacentHTML("beforeend", imgInfo);
  111. //console.log(img);
  112. });
  113.  
  114. document.body.onclick=(e)=>{
  115. if(e.target.nodeName=="IMG" && e.target.className==="tyc-image-preview"){
  116. try{
  117. document.querySelector(".show-big-image").remove();
  118. }
  119. catch{
  120.  
  121. }
  122. document.body.insertAdjacentHTML("beforeend",showBigImage);
  123. let showItem=`<img src="${e.target.src}"/>`
  124.  
  125. document.querySelector(".show-big-image").insertAdjacentHTML("beforeend",showItem);
  126.  
  127. let tempImg=document.querySelector(".show-big-image img");
  128.  
  129. let dWidth=(window.innerWidth-tempImg.width)/2;
  130. let dHeight=(window.innerHeight-tempImg.height)/2;
  131.  
  132. document.querySelector(".show-big-image").style.left=dWidth+"px";
  133. document.querySelector(".show-big-image").style.top=dHeight+"px";
  134. }else if(e.target.parentElement.className==="show-big-image"){
  135. document.querySelector(".show-big-image").remove();
  136. }
  137. }
  138.  
  139.  
  140. })();