// ==UserScript==
// @name 孔夫子旧书网图片下载(自动去水印)-路人甲乙丙
// @description 何以生财,唯有实战。(问题反馈联系微信Byte4Me)
// @version 3.5
// @author 路人甲乙丙
// @namespace iblogc
// @match *://search.kongfz.com/*
// @match *://book.kongfz.com/*
// @match *://item.kongfz.com/*
// @match *://book.kongfz.com/C*
// @grant GM_addStyle
// @grant GM_download
// @grant GM_xmlhttpRequest
// @license Apache License, Version 2.0
// @homepage https://github.com/iblogc/TampermonkeyScript/blob/main/kongfzImageDownload.js
// ==/UserScript==
"use strict";!function(){function n(n){k=n,localStorage.setItem(h,n)}function o(){w=!0,localStorage.setItem(y,"true")}function e(n){return n.replace(/(_water|_n|_p|_b|_s)/g,"")}function t(n,o){var e=n.createElement("button");e.innerText="👉 下载图片",e.className="searchPageDownloadButton item-button",e.style.backgroundColor="#026052",e.style.color="white";var t=o.querySelector("div.add-cart-btn")||o.querySelector("div.add-cart-button");return t.parentNode.insertBefore(e,t.nextSibling),e}function r(n,o){var e=n.createElement("button");e.innerText="👉 下载图片",e.className="searchPageDownloadButton item-button",e.style.backgroundColor="#026052",e.style.color="white";var t=o.querySelector("div.add-cart-btn")||o.querySelector("div.add-cart-button");return t.parentNode.insertBefore(e,t.nextSibling),e}function a(n,o){var e=n.createElement("button");e.innerText="👉 下载图片",e.className="bookListPageDownloadButton",e.style.backgroundColor="#026052",e.style.color="white";var t=o.querySelector("a.con-btn-cart");return t.parentNode.insertBefore(e,t.nextSibling),e}function i(n,o){c(n,o)}function l(n){var o=n.querySelectorAll("ul#figure-info-box > li");return Array.from(o,function(n){return e(n.querySelector("img").getAttribute("_viewsrc"))})}function c(o,e){var t=l(o);e.style.cursor="not-allowed";var r=document.createElement("a");if(r.href="https://greasyfork.s3.us-east-2.amazonaws.com/lp9hdyffstt0wpz2ub39gw9p6srr",r.target="_blank",r.textContent="🐛问题反馈",r.style.color="#ffeb83",0===t.length)return e.innerText="🧐商品详情中没\n有图片可以下载\n",e.style.backgroundColor="#ccc",e.style.color="#999",r.style.color="#b55222",void e.appendChild(r);e.disabled=!0,e.innerText="Downloading...";var a=0,i=0,c=(o.querySelector('meta[name="keywords"]').getAttribute("content")||"").match(/([^,]+)/),u=c&&c.length>1?c[1]:"",s=(o.querySelector('meta[name="description"]').getAttribute("content")||"").match(/ISBN:([0-9]*)/),p=s&&s.length>1?s[1]:"";t.forEach(function(o,l){var c=(o.split(".").pop()||"").toLowerCase(),s=u.trim()+"-"+p.trim()+"-"+(l+1)+"."+(c||"jpg");console.log("Image download "+s+": "+o),GM_download({url:o,name:s,onprogress:function(n){e.innerText="Downloading...("+(l+1)+"/"+t.length+")"},onload:function(){a++,a===t.length&&(n(k+t.length),(k%100==0&&0!==k&&!w||k>1e3&&!w)&&d()),a+i===t.length&&(e.style.lineHeight="20px",e.innerText="📢总计:"+t.length+"\n🥳成功:"+a+"\n😭失败:"+i+"\n",e.appendChild(r),e.style.backgroundColor=i>0?"#ef8f87":"#6eb76c")},onerror:function(n){i++,console.log("Error downloading image:",n),a+i===t.length&&(e.style.lineHeight="20px",e.innerText="📢总计:"+t.length+"\n🥳成功:"+a+"\n😭失败:"+i+"\n",e.appendChild(r),i>0?(e.style.backgroundColor="#f5675b",e.style.color="#fff"):(e.style.backgroundColor="#06b500",e.style.color="#fff"))}})})}function d(){var n=document.createElement("div");n.classList.add("overlay"),document.body.appendChild(n);var e=document.createElement("div");e.classList.add("donation-popup"),e.innerHTML='\n <div class="donation-popup">\n <p class="donation-text">孔夫子旧书网图片下载(自动去水印)------“时间就是金钱”</p>\n <div class="donation-text">非常开心您选择此工具,考虑捐赠以鼓励我继续维护和改进此工具🙏</div>\n <div class="donation-images">\n <img src="https://greasyfork.s3.us-east-2.amazonaws.com/1ohv6vh4i7r7bdx3pe9zkmtfqdcz" alt="捐赠二维码1" />\n <img src="https://greasyfork.s3.us-east-2.amazonaws.com/5f4nlsf3mhtrps0x3dm2tpnj0k54" alt="捐赠二维码2" />\n </div>\n <div style="text-align:center"><a href="https://greasyfork.s3.us-east-2.amazonaws.com/lp9hdyffstt0wpz2ub39gw9p6srr" target="_blank">问题/建议反馈微信:Byte4Me</a></div>\n <br />\n <div class="donation-buttons">\n <button id="donateBtn">我已捐赠💖</button>\n <button id="notDonateBtn">暂不捐赠❌</button>\n </div>\n </div>\n ',document.body.appendChild(e);var t=e.querySelector("#donateBtn"),r=e.querySelector("#notDonateBtn");t.addEventListener("click",function(){alert("🙏感谢您的支持,有问题请联系微信:Byte4Me!"),e.remove(),n.remove(),o()}),r.addEventListener("click",function(){e.remove(),n.remove()})}function u(n,o){o.addEventListener("click",function(){GM_xmlhttpRequest({method:"GET",url:n,onload:function(n){c((new DOMParser).parseFromString(n.responseText,"text/html"),o)},onerror:function(n){console.log("Error:",n),o.innerText="⛔ 解析网页时出错"}})})}function s(n){u(n.querySelector(".item-info-box > .item-name > a.item-link").href,t(document,n))}function p(n){u(n.querySelector(".item-info > .title > a.link").href,r(document,n))}function f(n){u(n.querySelector("div.list-con-title > a").href,a(document,n))}function g(){document.querySelector(".product-item-box")&&(clearInterval(z),document.querySelectorAll(".product-item-box > .product-item-wrap").forEach(function(n){s(n)}))}function m(){document.querySelector("#listBox")&&(clearInterval(z),document.querySelectorAll("#listBox > .item").forEach(function(n){p(n)}))}function b(){document.querySelector("ul.itemList")&&(clearInterval(z),document.querySelectorAll("ul.itemList > li").forEach(function(n){f(n)}))}var x=window.location.href,h="downloadCount",y="donationPopupShown",v="firstExecutionv35",k=parseInt(localStorage.getItem(h))||0,w="true"===localStorage.getItem(y),S="true"===localStorage.getItem(v),z=void 0;if(S||(alert("孔夫子旧书网图片下载插件 v3.5 修复https://item.kongfz.com/index.php?*类页面不显示下载按钮 bug"),function(){S=!0,localStorage.setItem(v,"true")}()),x.includes("//search.kongfz.com/"))console.log("//search.kongfz.com/"),z=setInterval(g,1e3);else if(x.includes("//book.kongfz.com/C"))console.log("//book.kongfz.com/C"),z=setInterval(m,1e3);else if(x.includes("//book.kongfz.com/")){console.log("//book.kongfz.com/");var q=function(n){var o=document.createElement("button");return o.innerText="👉 下载图片("+n.length+")",o.id="downloadButton",o.style.backgroundColor="#026052",o.style.color="white",document.body.appendChild(o),o}(l(document));q.addEventListener("click",function(){return i(document,q)})}else x.includes("//item.kongfz.com/")&&(console.log("//item.kongfz.com/"),z=setInterval(b,1e3));GM_addStyle("\n #downloadButton {\n position: fixed;\n bottom: 20px;\n right: 20px;\n padding: 10px 20px;\n background-color: #333;\n color: #fff;\n border: none;\n border-radius: 5px;\n cursor: pointer;\n z-index: 9999;\n }\n #bugReportLink {\n position: fixed;\n bottom: 5px;\n right: 20px;\n padding: 10px 20px;\n background-color: #333;\n color: #fff;\n border: none;\n border-radius: 5px;\n cursor: pointer;\n z-index: 9999;\n }\n .searchPageDownloadButton {\n padding: 1px 5px;\n background-color: #333;\n color: #fff;\n border: none;\n border-radius: 3px;\n cursor: pointer;\n z-index: 9999;\n font-size: 12px;\n margin: 0px 10px;\n }\n .bookListPageDownloadButton {\n padding: 1px 5px;\n background-color: #333;\n color: #fff;\n border: none;\n border-radius: 3px;\n cursor: pointer;\n z-index: 9999;\n font-size: 12px;\n margin: 8px auto 0;\n display: block;\n }\n button.disabled {\n background-color: #ccc;\n color: #999;\n cursor: not-allowed;\n /* 其他样式 */\n }\n .overlay {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色 */\n z-index: 9998; /* 比弹窗层级低,但比页面其他元素高 */\n }\n .donation-popup {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n background-color: #f9f9f9;\n padding: 20px;\n border-radius: 10px;\n box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);\n z-index: 9999;\n }\n\n .donation-popup .donation-text {\n margin-bottom: 20px;\n text-align: center;\n font-size: 16px;\n }\n\n .donation-images {\n display: flex;\n justify-content: space-around;\n margin-bottom: 20px;\n }\n\n .donation-images img {\n height: 300px;\n border-radius: 5px;\n box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);\n }\n\n .donation-buttons {\n display: flex;\n justify-content: center;\n }\n\n .donation-buttons button {\n padding: 5px 10px;\n margin: 0 10px;\n border: none;\n border-radius: 5px;\n cursor: pointer;\n font-size: 16px;\n background-color: #026052;\n color: #fff;\n box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);\n transition: background-color 0.3s ease;\n }\n\n .donation-buttons button:hover {\n background-color: #014033;\n }\n\n ")}();