// ==UserScript==
// @name ImgBB 相册下载器
// @name:en ImgBB Image Downloader
// @name:zh-CN ImgBB 相册下载器
// @namespace http://tampermonkey.net/
// @version 1.0.2
// @description Downbnload Single Image or album
// @description:zh-cn 批量下载ImgBB 相册图片
// @license MIT
// @author You
// @match https://ibb.co/album/*
// @match https://123.imgbb.com
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant GM_download
// @run-at document-body
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
/* globals jQuery, $, waitForKeyElements */
function findAlbumInfo() {
//Get Album Info
var albumInfo={albumName:"",albumURL:"",albumID:"",imgCount:0};
$("meta").each(function (index) {
if($(this).attr("property")){
switch($(this).attr("property")){
case("og:title"):
albumInfo.albumName=$(this).attr("content");
break;
case("og:url"):
albumInfo.albumURL=$(this).attr("content");
albumInfo.albumID=albumInfo.albumURL.slice(21,27);
break;
}
}
});
//Get image Count
$("span").each(function (index) {
if($(this).attr("data-text")=="image-count"){
albumInfo.imgCount=parseInt($(this).text());
return;
}
});
//console.log(albumInfo);
return albumInfo;
}
function listImageURLs(albumID) {
var imgInfos= [];
$.ajaxSettings.async = false;
$.ajax({
url: "https://ibb.co/json",
data: {"action":"get-album-contents","albumid":albumID},
type: "POST",
dataType: "json",
timeout: 5000, // 设置超时时间为5秒
success: function(data) {
//console.log(data);
imgInfos=data.contents;
},
error: function(jqXHR, textStatus, errorThrown) {
if (textStatus === 'timeout') {
// 超时错误处理
console.log('Request timed out.');
} else {
// 其他错误处理
console.log('Error: ' + errorThrown);
}
}
});
$.ajaxSettings.async = true;
//console.log(imgInfos);
return imgInfos;
}
function appendDownloadEle(albumInfo) {
if($("#img_downloader").length <= 0) {
// 下载按钮
let btnTab;
// Get location of the Tab
$("#tab-sub-link").each(function (index) {btnTab=$(this);});
//Create the Download Tab
var tabtxt=`
点击下载
`;
btnTab.parent().parent().after(tabtxt);
$("#img_downloader_origin" ).on("click", function (event) {
//$("#img_downloader_dropdown" ).attr("style","display: none;")
event.preventDefault();
let imgInfos=albumInfo.imgInfos;
console.log("Original Download Start...-By Yingest");
for(let i=0;i < imgInfos.length;i++){
var name = "ImgBB/" + albumInfo.albumName + "/" +imgInfos[i].filename;
var url = imgInfos[i].url;
//console.log([name,url]);
GM_download(url,name);
}
console.log("Original Downlaoad Finish!-By Yingest");
});
$("#img_downloader_medium" ).on("click", function (event) {
//$("#img_downloader_dropdown" ).attr("style","display: none;")
event.preventDefault();
let imgInfos=albumInfo.imgInfos;
console.log("Medium Download Start...-By Yingest");
for(let i=0;i < imgInfos.length;i++){
var name = "ImgBB/" + albumInfo.albumName + "/Medium/" + imgInfos[i].filename;
var url = imgInfos[i].medium.url;
//console.log([name,url]);
GM_download(url,name);
}
console.log("Medium Downlaoad Finish!-By Yingest");
});
$("#img_downloader_thumb" ).on("click", function (event) {
//$("#img_downloader_dropdown" ).attr("style","display: none;")
event.preventDefault();
let imgInfos=albumInfo.imgInfos;
console.log("Thumb Download Start...-By Yingest");
for(let i=0;i < imgInfos.length;i++){
var name ="ImgBB/" + albumInfo.albumName + "/Thumb/" + imgInfos[i].filename;
var url = imgInfos[i].thumb.url;
//console.log([name,url]);
GM_download(url,name);
}
console.log("Thumb Downlaoad Finish!-By Yingest");
});
$("#img_downloader_CB" ).on("click", function (event) {
//$("#img_downloader_dropdown" ).attr("style","display: none;")
event.preventDefault();
let imgInfos=albumInfo.imgInfos;
//console.log("Thumb Download Start...-By Yingest");
var copystring="";
for(let i=0;i < imgInfos.length;i++){
var name = "ImgBB/" + albumInfo.albumName + "/" +imgInfos[i].filename;
var url = imgInfos[i].url;
//console.log([name,url]);
//GM_download(url,name);
copystring=copystring+url+"\n";
}
console.log(copystring);
copyText(copystring);
console.log("Links copied!-By Yingest");
});
}
}
async function copyText(copystring) {
try {
await navigator.clipboard.writeText(copystring);
console.log('文本已复制到剪贴板');
} catch (err) {
console.error('复制失败: ', err);
}
}
$(document).ready(function () {
// 查找相册名称位置,向左上角添加【下载】按钮.
let albumInfo = findAlbumInfo();
albumInfo.imgInfos = listImageURLs(albumInfo.albumID);
//console.log(albumInfo);
if(albumInfo.imgInfos.length==albumInfo.imgCount){appendDownloadEle(albumInfo);}
});
/** useless function
function childernEle(father,Orders,attr){
// Tools for get childern elements
var len=Orders.length;
var info=new Array;;
if(len==0){
info.push(father.val());
info.push(father.text());
for(var i = 0;i < attr.length;i++){info.push(father.attr(attr[i]));}
}
else if(len>=1){
var childern=father.contents();
childern.each(function (index) {
if(index==Orders[0]){info = childernEle($(this),Orders.slice(1,len),attr);}
});
}
return info
}
function listImageURLs_old() {
var info0,info1,info2;
var imgInfos=new Array();
let imgJQList = $("div");
if(imgJQList.length > 0 ){
imgJQList.each(function (index) {
if($(this).attr("data-type")=="image"){
var imgInfo={
imgName:"",
imgID:"",
imgURL_Origin:"",
imgURL_View:"",
imgURL_Small:""
};
info0=childernEle($(this),[],["data-id"]);
//info1=childernEle($(this),[0,0],["href"]);
info2=childernEle($(this),[0,0,0],["src","alt"]);
imgInfo.imgName=info2[3];
imgInfo.imgID=info0[2];
imgInfo.imgURL_View=info2[2];
//console.log(imgInfo);
imgInfos.push(imgInfo);
}
});
}
return imgInfos
}
function getImageURL_old(viewerLink,type) {
console.info(viewerLink);
var p = GM_xmlhttpRequest({
method: "GET",
url: viewerLink,
responseType:type,
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
},
onload: function (response) {
console.log("请求成功");
console.log(response.responseText);
},
onerror: function (response) {
console.log("请求失败");
},
ontimeout: function (response) {
console.info("查询信息超时。");
}
});
console.info(p)
return p
}
**/
})();