// ==UserScript==
// @name pixiv图片下载
// @namespace http://tampermonkey.net/
// @version 2024-02-22
// @description Pixiv图片下载,添加一个🥰按钮用来下载图片
// @author 52lcx
// @match https://www.pixiv.net/*
// @license MIT
// @require https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js
// @icon https://www.google.com/s2/favicons?sz=64&domain=pixiv.net
// @require https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.6.0/jszip.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/history.js/1.8/native.history.min.js
// @grant GM_xmlhttpRequest
// @grant GM_download
// @grant unsafeWindow
// ==/UserScript==
var old_url=""
var observer_url =new MutationObserver(function(){
var now_url=document.head.querySelector('link[rel="alternate"]').getAttribute("href")
console.log(location.href)
if(old_url===now_url){return}
else{old_url=now_url
console.log("我们不一样")
all_function();
}
})
observer_url.observe(document.head,{attributes: true, childList: true, subtree: true})
function all_function(){
var observer_insert =new MutationObserver(function(){
var zan=document.querySelector(".LpzCoKsT5QQ0VW1c")
if(zan){button();observer_insert.disconnect() }
setTimeout(function(){observer_insert.disconnect()},10000)
})
observer_insert.observe(document.body,{subtree:true,childList:true})
var observer_ads = new MutationObserver(function(mutationsList, observer) {
var delete_ads = document.querySelector(".sc-h4y4ap-0.hReJzp");
if (delete_ads) {
console.log("存在")
delete_ads.remove();
observer_ads.disconnect()
}
setTimeout(function(){observer_ads.disconnect()},10000)
});
observer_ads.observe(document.body, { subtree: true, childList: true });
setTimeout(function(){observer_ads.disconnect()},10000)
}
function button(){
let setting =document.querySelector(".sc-181ts2x-2.bTImJG")
var button = document.createElement("div");
button.innerHTML = '<div id="download" @click="download" style="cursor:pointer">🥰</div>';
setting.parentNode.insertBefore(button, setting);
var styleElement = document.createElement("style");
let style = `#download { font-size: 25px;
transform: translateY(5px); }`;
styleElement.innerHTML = style;
document.head.appendChild(styleElement);
let button_app=new Vue({
el:"#download",
data:{clickcount:0},
methods:{
download:function(){
this.$el.style.fontSize = "50px"
this.clickcount++
var all_image = document.querySelector(".sc-emr523-2.wEKy");
if(all_image){all_image.click()}
var long_image=document.querySelector(".sc-1mr081w-2.gyWxeX.gtm-manga-viewer-preview-modal-open")
if(document.querySelector(".sc-d98f2c-0.sc-1y32dbe-0.bvRXUX.new-header-logo").getAttribute("href")=="/manga")
{console.log("这是漫画")
localStorage.setItem("manga_name","title")
button_observe();
localStorage.setItem("button_click",'true')}
else if (long_image) {
console.log("这是多组插画")
setTimeout(function(){
download_long()
},10000)
}
else{
download_short()
}
setTimeout(function(){this.$el.style.fontSize="25px"},10000)
}
}
})}
// function select(){
// if(location.href.match(/\bartworks\/\d+$/).length){download_long()}
// else if(location.href.match(/\bartworks\/\d+#1$/).length){manga()}
//}
// //下载短插画
function download_short() {
console.log("正在下载短插画")
var text="未命名"
var url = document.querySelector(".sc-1qpw8k9-0.gTFqQV a").getAttribute("href");
var element1 = document.querySelector(".sc-1u8nu73-3.huVRfc");
var element2 = document.querySelector(".sc-f30yhg-1.hRmPFI div");
if (element1 && element2) {
var text1 = element1.innerText;
var text2 = element2.innerText;
text=text1+text2
} else {
console.log("至少一个元素未找到。");
}
GM_download({
url: url,
name: text+".png",
saveAs: false,
headers: { referer: "https://i.pximg.net/*" },
onerror: function (error) {
alert("下载失败,请重试!");
},
});
}
// //下载长插画
function download_long(){
console.log("正在下载长插画")
setTimeout(function(){
var textElement1 = document.querySelector(".sc-1u8nu73-3.huVRfc");
var textElement2 = document.querySelector(".sc-f30yhg-1.hRmPFI div");
var text = "未命名"
if (textElement1 && textElement2) {
text = textElement1.innerText + "---" + textElement2.innerText;
}
var zip=new JSZip()
var count=0
var imgs=document.querySelectorAll(".sc-1e1hy3c-2.dSqYyx a");
imgs.forEach(function(link,index){
var url=link.getAttribute("href")
GM_xmlhttpRequest({
method:"GET",
url:url,
headers:{referer:"https://i.pximg.net/*"},
responseType:"arraybuffer",
onerror:function(){"下载失败,请重试🥰"},
ontimeout:()=>{alert("下载超时,请重试")},
onload:function(response)
{if(response.status==200){
zip.file(index+1+ ".png",response.response,{binary:true})
count++
console.log(count)
if(count==imgs.length){
zip.generateAsync({type:"blob"}).then(function(content){
saveAs(content,text+".zip")
})
}
}
}
})
})},0)
}
//更新页面
function button_observe(){
var button_click = new MutationObserver(function() {
var button = document.querySelector(".sc-bdnxRM.jvCTkj.sc-gtsrHT.kJeMVE");
if (button) {
button.click();
button_click.disconnect(); }
setTimeout(function(){button_click.disconnect();},10000)
});
button_click.observe(document.body, { subtree: true, childList: true })}
//下载漫画
//sc-1oz5uvo-1 ivxzyL//漫画特有
function manga(title){
alert("下载漫画时请切换至竖版阅读哦哦🥰,如果不是请重试")
localStorage.setItem("button_click",'false')
var imgs=document.querySelectorAll(".gtm-expand-full-size-illust")
var zip= JSZip()
var count=0
console.log("开始下载漫画")
imgs.forEach(function(link ,index){
var url=link.getAttribute("href")
GM_xmlhttpRequest({
methods:"GET",
url:url,
headers:{referer:"https://i.pximg.net/*"},
responseType:"arraybuffer",
onerror:function(){"下载失败,请重试🥰"},
onload:function(response)
{if(response.status==200){
console.log("请求成功")
zip.file(index+1+ ".png",response.response,{binary:true})
count++
console.log(count)
if(count==imgs.length){
zip.generateAsync({type:"blob"}).then(function(content){
saveAs(content,title+".zip")
})
}
}
}
})
})
}
//监听按钮是否点击
var button_click = localStorage.getItem('button_click');
setTimeout(function(){
//
if (button_click === 'true' && location.href.match(/\bartworks\/\d+#1$/)) {
var title=document.querySelector(".sc-1u8nu73-3.huVRfc").innerText
manga(title);
}
},10000)