故開發本腳本 ,先用XMLHttpRequest解決之,爾後再看看有無好辦法
// ==UserScript==
// @name f12.wretch.yimg
// @description 顯示原圖、開新分頁
// @author NKid
// @version 2012-06-24
// @include http://www.wretch.cc/album/album.php?id=*&book=*
// ==/UserScript==
GM_registerMenuCommand('f12.wretch.yimg - Setting width of images', settingImgWidth);
var oriTitle=document.title; //原始標題
function getDocFromXHR(url) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, false);
xmlHttp.send(null);
return new DOMParser().parseFromString(xmlHttp.responseText,"text/html");
}
//Setting width of images
function settingImgWidth() {
var oldIW=GM_getValue('imgwidth')||240; //(Default Value:240)
GM_setValue('imgwidth', prompt('Please Enter the width(px) of images.', oldIW));
}
function APEvent() {
var untreatedImgs=document.querySelectorAll("td.side a img[src*='thumbs/t']"); //尚未處理的圖片
document.title= "AutoPagerize Working";
for(var i=0;i<untreatedImgs.length;i++)
{
untreatedImgs[i].parentNode.target="_blank";
var doc=getDocFromXHR(untreatedImgs[i].parentNode.href);
var paraF=/f=(\d*)&/.exec(untreatedImgs[i].parentNode.href)[1];
if (doc.getElementById("DisplayImage"))
untreatedImgs[i].src=doc.getElementById("DisplayImage").src;
else if (doc.querySelector("img[src*='"+ paraF +"']"))
untreatedImgs[i].src=doc.querySelector("img[src*='"+ paraF +"']").src;
}
document.title=oriTitle;
}
function main() {
var picLinks=document.querySelectorAll("td.side a"); //圖片超連結
var totalImgLength=picLinks.length; //圖片總數
var imgWidth=parseInt(GM_getValue('imgwidth')||240,10) //圖片寬度
GM_addStyle('td.side img{max-width:@imgwidthpx}'.replace('@imgwidth',imgWidth));
for(var i=0;i<picLinks.length;i++)
{
picLinks[i].target="_blank";
var doc=getDocFromXHR(picLinks[i].href);
var paraF=/f=(\d*)&/.exec(picLinks[i].href)[1];
if (doc.getElementById("DisplayImage"))
picLinks[i].children[0].src=doc.getElementById("DisplayImage").src;
else if (doc.querySelector("img[src*='"+ paraF +"']"))
picLinks[i].children[0].src=doc.querySelector("img[src*='"+ paraF +"']").src;
document.title= (i+1) + " / " + totalImgLength;
}
document.title=oriTitle;
window.addEventListener("AutoPagerize_DOMNodeInserted",APEvent,false);
}
main();