在每张图片下面增加查看原图&下载原图
当前为
// ==UserScript==
// @name bcy(半次元)查看原图&下载原图
// @version 0.102
// @description 在每张图片下面增加查看原图&下载原图
// @author jotting
// @match http://bcy.net/*
// @grant none
// @namespace https://greasyfork.org/users/92069
// ==/UserScript==
(function () {
'use strict';
var imgs = $(".content-img-wrap img");
var i = 1;
var title = document.title.replace(/\|.*$/i, '').replace(' cosplay ', ' ').replace(/\s/ig, '_');
imgs.each(function () {
var $this = $(this);
var url = this.src.replace(/\/w650$/i, '');
var av = document.createElement('a');
var p = $this.closest("div.content-img-wrap");
p.css({ "position": "relative" });
av.href = url;
av.target = "_blank";
av.title = title+(i>9?i:'0'+i);
av.innerHTML = "查看原图";
var complete=this.complete;
if(!complete)
{
$this.on('load',function(){
setTimeout(function(){
var right = Number(((p.width() - $this.width()) / 2).toFixed(0));
ad.style.right = right + "px";
av.style.right = (right + 66) + "px";
},100);
});
}
var right = Number(((p.width() -Math.max($this.width(),132)) / 2).toFixed(0));
av.style.cssText = "z-index:9999;position:absolute;width:66px;text-align:center;bottom:-21px;right:" + (right + 66) + "px;background:rgba(255,255,255,0.6);";
p.append(av);
var ad = av.cloneNode(true);
ad.style.right = right + "px";
ad.innerHTML = "下载原图";
ad.download = ad.title + /\.\w+$/i.exec(url)[0];
p.append(ad);
i++;
});
})();
(function () {
'use strict';
function Download(a) {
var xhr = new XMLHttpRequest();
var oldhtml = a.innerHTML;
xhr.open("GET", a.href, true);
xhr.responseType = 'blob';
xhr.onprogress = function (e) {
if (e.lengthComputable) {
a.innerHTML = (e.loaded / e.total * 100).toFixed(2) + "%";
}
};
xhr.onload = function (e) {
if (this.status == 200) {
var blob = this.response;
saveFile(blob, a.download);
a.innerHTML = oldhtml;
a.isDownload = false;
}
};
xhr.send(null);
}
function saveFile(blob, filename) {
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(blob, filename);
} else {
var a = document.createElement("a");
var url = URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
}
var ars = document.querySelectorAll("a[href][download]");
for (var i = 0; i < ars.length; i++) {
var a = ars[i];
a.addEventListener("click", function (e) {
e.preventDefault();
if (!this.isDownload) Download(this);
this.isDownload = true;
return false;
}, true);
}
})();