This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.sleazyfork.org/scripts/480132/1473184/Get_all_img_Library.js
// ==UserScript==
// @name Get_all_img_Library
// @namespace http://tampermonkey.net/
// @version 2024.10.28
// @description 添加了downloader和Add_css;
// @author You
// @grant none
// ==/UserScript==
function get_img_obo_sessionStorage(key,nums,geturl,getimg,putimg){
if(window.location.href.indexOf(key)<0){return}
var i = 2
if(sessionStorage.num){i = Number(sessionStorage.num)}
var oldimg = $(sessionStorage.img)
before_reflash(i,oldimg,putimg)
var state = sessionStorage.state
if(state!='start'){putimg($(sessionStorage.img),0);sessionStorage.clear();return}
if(i<=nums){
var href = geturl(i)
if(!href){putimg($(sessionStorage.img),0);sessionStorage.clear();return}
$.ajax({
url:href,
success:function(){
var img = getimg('')
if(sessionStorage.img==undefined){sessionStorage.img=""}
sessionStorage.img += $('<div></div>').append(img.clone()).html()
window.location.href = href
i+=1
sessionStorage.num = i
},
error:function(){
alert(href+' error')
putimg($(sessionStorage.img),0)
sessionStorage.clear()
},
})
}else{
putimg($(sessionStorage.img),0)
sessionStorage.clear()
}
}
function before_reflash(i,oldimg,putimg){
if(sessionStorage.reflash){putimg($(sessionStorage.img),0)}
console.log('before_reflash')
window.onbeforeunload = function(){
if(!oldimg){return}
if(sessionStorage.state){return}
sessionStorage.img = $('<div></div>').append(oldimg).html()
sessionStorage.reflash = 'reflash'
sessionStorage.num = i
}
}
function get_img_obo_ajax_href(key,nums,geturl,getimg,putimg){
var obo = function(i){
var href = geturl(i)
console.log(href)
if(!href){return}
$.ajax({
url:href,
success:function(data){
var img = getimg(data)
putimg(img,i)
showmass(i+'/'+nums)
i+=1
if(i>=nums){return}
obo(i)
},
error:function(){
console.log('error: '+href)
i+=1
if(i>=nums){return}
obo(i)
}
})
};obo(0)
}
function new_bottom_bu(text){
var bu = $('<button></button>').text(text).css({
position:'fixed',
bottom:0,
width:'100vw',
height:'10vh',
'font-size':'5vh'
})
return bu
}
function obo_sessionStorage_start_bu(){
var bu = new_bottom_bu("start")
bu.click(function(){
if(sessionStorage.state!='start'){
sessionStorage.clear()
sessionStorage.setItem('state','start')
location.reload()
}else{
sessionStorage.clear()
}
})
return bu
}
function showmass(ms) {
if($('.mass_top').attr('class')){$('.mass_top').text(ms);return}
$('body').append($('<div class="mass_top"></div>').css({
'font-size':'2vw',
'color':'rgba(0, 102, 0, 0.5)',
'position':'fixed',
'top':'10px',
'left':'10px',
'font-weight':'bold',
'z-index':99999999,
}).click(function(){$(this).hide()}))
$('.mass_top').text(ms)
}
function add_css(tag,object){
var style = $('<style></style>').text(tag+JSON.stringify(object).replace(/\"/g,'').replace(/,/g,";"))
$('body').append(style)
}
function remove_sameimg(){
var hs = new Array()
$('img').each(function(){
if(hs.indexOf(this.src)<0){
hs.push(this.src)
}else{
$(this).remove()
}
})
}
function reflash_unloadimg(){
var check = setInterval(function(){
if(sessionStorage.reflashlock == 'yes'){return}
var img = $('img:visible').filter(function(){return this.naturalWidth==0})
img.each(function(){
if(this.naturalWidth==0){
//console.log(this.src)
var clone = $(this).clone(true)
var parent = $(this).parent()
var pre = $(this).prev()
var next = $(this).next()
$(this).remove()
setTimeout(function(){
if(pre[0]){pre.after(clone);return}
if(next[0]){next.before(clone);return}
if(parent[0]){parent.append(clone);return}
},500)
}
})
},1000)
}
let addedKeyControl = false;
function AddKeyControl(downItem,upItem,leftItem,rightItem,closew){
if(addedKeyControl){return;}else{addedKeyControl = true;}
if(!downItem){
downItem = $('<a></a>').click(function(){
// 获取当前窗口的滚动位置
var currentScroll = document.documentElement.scrollTop;
// 设置每次 PageDown 后滚动的距离,可以根据需要调整
var scrollDistance = $(window).height()/2;
console.log(currentScroll);
// 使用 scrollTo 方法将窗口滚动到当前位置加上设定的滚动距离
window.scrollTo(0,currentScroll + scrollDistance);
})
}
if(!upItem){
upItem = $('<a></a>').click(function(){
// 获取当前窗口的滚动位置
var currentScroll = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
// 设置每次 PageDown 后滚动的距离,可以根据需要调整
var scrollDistance = window.innerHeight/2;
// 使用 scrollTo 方法将窗口滚动到当前位置加上设定的滚动距离
window.scrollTo(0,currentScroll - scrollDistance);
})
}
document.addEventListener('keydown', function(event) {
if (event.key === 'ArrowLeft' && leftItem && leftItem.length>0) {
// 用户按下了左箭头键
leftItem[0].click();
console.log('Left arrow key pressed');
// 执行左方向键对应的操作
} else if (event.key === 'ArrowRight' && rightItem && rightItem.length>0 ) {
// 用户按下了右箭头键
rightItem[0].click();
console.log('Right arrow key pressed');
// 执行右方向键对应的操作
} else if (event.key === 'ArrowUp' && upItem && upItem.length>0) {
// 用户按下了上箭头键
upItem[0].click();
console.log('Up arrow key pressed');
// 执行上方向键对应的操作
} else if (event.key === 'ArrowDown' && downItem && downItem.length>0) {
// 用户按下了下箭头键
downItem[0].click();
console.log('Down arrow key pressed');
// 执行下方向键对应的操作
} else if (event.key === '0' && closew) {
// 用户按下了下箭头键
window.close();
// 执行下方向键对应的操作
}
});
}
async function hold_and_zoom(img,name,GetBigImg){
if(!$('.holdbox:first')[0]){
$('body').append($('<div class="holdbox"></div>').css({
'z-index':'9999999',
width:'100vw',
height:'100vh',
position:'fixed',
top:0,
left:0,
display:'flex',
'justify-content':'center',
}))
$('.holdbox').hide()
$('.holdbox').on('mousedown',function(e){
if(e.button!=0){return}
if(showimg){clearTimeout(showimg)}
if(showimg){showimg = null}
$('.holdbox:first').hide()
}).on('mousewheel',function(){
if(showimg){clearTimeout(showimg)}
if(showimg){showimg = null}
$('.holdbox:first').hide()
})
}
var showimg = null
var state = 'mouseout'
img.on('mouseout',function(){
state = 'mouseout'
})
let getting = false;
img.on('mouseover',async function(event){
state = 'mousemove'
if(showimg){clearTimeout(showimg)}
if(showimg){showimg = null}
let _img = $(this);
let iimg = $(this).clone();
showimg = setTimeout(function(){
if(state=='mouseout'){return}
if(GetBigImg && img.attr('big-src')==null && !getting){
new Promise((resolve,reject)=>{
getting = true;
GetBigImg(function(src){
if(!src || src==""){console.log('src error');resolve()}else{
getting = false;
_img.attr('big-src',src).attr('src',src);
let mass = $('<p>loading...</p>');
_img.after(mass);
let ioo = new Image();
ioo.onload = function(){
iimg.attr('src',src);
console.log('src success');
mass.remove();
resolve();
}
ioo.src = src;
}
})
})
}
$('.holdbox img').remove()
$('.holdbox:first').append(iimg.css({
width:'auto',
height:'100vh',
}).attr('name',name(_img)).on('mousedown',function(){
var src = this.src
var n = $(this).attr('name')
if($(this).attr('big-src')){
src = $(this).attr('big-src')
}
GM_download({
url:src,
name:n,
})
}))
console.log('show');
$('.holdbox').fadeIn(200)
},500)
})
}
function Add_css(cssString){
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = cssString;
document.body.appendChild(style);
}
class Downloader{
constructor(){
this.downloadType = "";
this.downloaded = [];
this.downloading = 0;
this.downloadError = [];
this.imgs = "";
this.maxDownloadingCounts = 10;
this.timeout = null;
this.AllComplete = null;
this.OneSuccess = null;
this.OneError = null;
}
async Download_img(imgs){
let self = this;
if(this.downloadType==""){
await this.Test_downloadType(imgs.eq(0));
}
this.Set_download(imgs)
if(this.downloadType=="GM_download"){
this.Donwload_img_by_GM();
}else if(this.downloadType=="atag"){
this.Donwload_img_by_atag();
}else if(this.downloadType=="blob"){
this.Donwload_img_by_blob();
}
}
async Test_downloadType(img){
return new Promise((resolve)=>{
let timeout = 3000;
let isOk = false;
GM_download({
url:img[0].src,
name:"test.png",
onload:()=>{isOk = true;this.downloadType = "GM_download";resolve()},
})
setTimeout(()=>{
if(!isOk){this.downloadType = "atag";}
resolve()
},timeout)
})
}
Set_download(imgs){
this.downloaded = [];
this.downloading = 0;
this.downloadError = [];
this.imgs = imgs;
}
Donwload_img_by_GM(){
let self = this;
async function Download_one(i){
if(i>=self.imgs.length){if(self.AllComplete){self.AllComplete()};return;}
if(self.downloading>=self.maxDownloadingCounts){setTimeout(()=>{Download_one(i)},1000);return;}
let name = '';
let src = '';
self.downloading++;
try{
await self.Check_and_get_nameAndsrc({img:self.imgs.eq(i),checkSrc:true})
.then((m)=>{
name = m.name;
src = m.src;
});
}catch(error){
console.log(error)
console.log('imgsrc is error:');
self.downloadError.push(i);
self.downloading--;
Download_one(i+1);
return;
}
console.log(name)
console.log(document.title)
let timeout = false;
setTimeout(() => {timeout = true;},10000);
const donwimg = self.imgs.eq(i);
GM_download({
url:src,
name:name,
onload:function(){
self.downloaded.push(downimg);
self.downloading--;
if(window.GAIL.showmass){
window.GAIL.showmass((self.downloaded.length+self.downloadError.length)+"/"+self.imgs.length);
}
self.imgs.eq(i).remove();
if(self.OneSuccess){self.OneSuccess(downimg);}
},
onerror:function(){
self.downloading--;
self.downloadError.push(downimg);
if(self.OnError){self.OnError(downimg);}
},
onprogress:function(){
if(timeout){return false;}
}
});
setTimeout(function() {Download_one(i+1);}, 10);
}
Download_one(0);
}
Check_and_get_nameAndsrc(args){
let self = this;
console.log(args);
return new Promise(async (resolve,reject)=>{
if(!args || !args.img){return reject();}
let src = args.img.attr('big_src');
if(!src){src = args.img.attr('src');}
if(!src){reject();}
console.log("check:"+src)
if(args.checkSrc){
try{
await self.check_src_is_right(src);
}catch(error){
reject();
}
}
let ext = src.match(/\.jpg|\.png|\.webp|\.gif|\.bmp/g);
if(!ext){ext = '.png';}else{ext = ext[0];}
let name = args.img.attr('name');
if(!name){name = document.title + new Date().getTime() + ext;}
resolve({name:name,src:src});
});
}
Donwload_img_by_atag(){
let self = this;
let myWindow = window.open('_blank');
async function Download_one(i){
if(i>=imgs.length){myWindow.alert('end');myWindow.history.go(0-myWindow.history.length+1);myWindow.close();return;}
let name = '';
let src = '';
self.downloading++;
try{
await self.Check_and_get_nameAndsrc({img:self.imgs.eq(i),checkSrc:true})
.then((m)=>{
name = m.name;
src = m.src;
});
}catch(error){
console.log('imgsrc is error:');
self.downloadError.push(i);
self.downloading--;
Download_one(i+1);
return;
}
if(!myWindow){alert('windows is closed');return;}
myWindow.location.href = src;
let a = $('<a></a>').attr({
'href':$(myWindow.document.body).find('img:first').attr('src'),
'download':name,
})
a[0].click();
setTimeout(function() {Download_one(i+1);self.imgs.eq(i).remove()}, 1000);
self.downloaded.push(i);
self.downloading--;
}
Download_one(0);
}
check_src_is_right(src){
let self = this;
return new Promise((resolve,reject)=>{
let iimg = new Image();
iimg.onload = function(){
if(this.width*this.height*this.naturalWidth*this.naturalHeight==0){reject();}else{resolve();}
}
iimg.onerror = function(){reject();}
iimg.src = src;
setTimeout(function() {iimg.abort();reject();}, 2000);
})
}
Donwload_img_by_blob(){
if(!this.imgs || this.imgs.length==0){return;}
const _this = this;
const imgs = this.imgs;
const obo = (i)=>{
if(i>=imgs.length){return;}
if(_this.maxDownloadingCounts>1 && _this.downloading >= _this.maxDownloadingCounts){
setTimeout(function() {obo(i)}, 1000);
}
let src = imgs.eq(i).attr('big_src');
if(!src){src = imgs.eq(i).attr('src');}
const checkimg = imgs.eq(i);
_this.downloading++;
_this.UrlToBlob({url:src,timeout:_this.timeout})
.then(blob=>{
_this.Check_and_get_nameAndsrc({img:checkimg}).then(args=>{
const name = args.name;
let a = $('<a></a>').attr({
download:name,
href:blob
})
a[0].click();
_this.downloaded.push(checkimg);
_this.downloading--;
checkimg.attr('src',blob);
OneSuccess?.(checkimg);
})
if(_this.maxDownloadingCounts==1){
obo(++i);
}else{
window.GAIL.showmass((_this.downloaded.length+_this.downloadError.length)+"/"+_this.imgs.length);
}
})
.catch(er=>{
console.log(er);
_this.downloadError.push(imgs.eq(i));
_this.downloading--;
if(_this.maxDownloadingCounts==1){
obo(++i);
}else{
window.GAIL.showmass((_this.downloaded.length+_this.downloadError.length)+"/"+_this.imgs.length);
}
});
if(_this.maxDownloadingCounts>1){
setTimeout(function() {obo(++i);}, 10);
}
}
obo(0);
}
/*
args = {url:url,timeout:timeout}
*/
async UrlToBlob(args) {
let _this = this;
return new Promise((resolve,reject)=>{
if(!args.url){reject("no url");}
if(args.timeout){
const timeout = setTimeout(function() {reject("fetch timeout")}, args.timeout);
}
fetch(args.url)
.then(response => {
const contentLength = response.headers.get('Content-Length');
const total = parseInt(contentLength, 10);
let loaded = 0;
// 克隆响应以便分别读取流和获得 Blob
const clonedResponse = response.clone();
const reader = clonedResponse.body.getReader();
// 更新进度的函数
function updateProgress({ done, value }) {
if (done) {
return; // 如果读取完毕,直接返回
}
loaded += value.byteLength; // 累加已加载字节
const progress = (loaded / total) * 100; // 计算进度百分比
console.log(`Loading: ${progress.toFixed(2)}%`);
_this.FetchShowProgress?.(progress);
// 继续读取下一块数据
return reader.read().then(updateProgress);
}
// 开始读取流以更新进度
return reader.read().then(updateProgress).then(() => {
// 完成后返回原始响应的 Blob
return response.blob();
});
})
.then(blob => {
const blobUrl = URL.createObjectURL(blob);
resolve(blobUrl);
})
.catch(error => {
console.error('Error caching video:', error);
reject(error);
});
});
}
FetchShowProgress(pro){
if(this.maxDownloadingCounts==1 && this.imgs.length==1){
window.GAIL.showmass(pro);
$(".mass_top").css('font-size',"10vw");
}
}
}
window.GAIL = {
get_img_obo_sessionStorage : get_img_obo_sessionStorage,
get_img_obo_ajax_href : get_img_obo_ajax_href,
new_bottom_bu : new_bottom_bu,
obo_sessionStorage_start_bu : obo_sessionStorage_start_bu,
showmass : showmass,
add_css : add_css,
remove_sameimg : remove_sameimg,
reflash_unloadimg : reflash_unloadimg,
AddKeyControl : AddKeyControl,
hold_and_zoom : hold_and_zoom,
Add_css:Add_css,
Downloader:Downloader,
}