/*
* @title 俺仕様画像抽出ブックマークレット(a,img) by fashi2 2016.01.26-
* @description 動作選択式(XPath抽出、正規表現フィルタ、新窓)
* @include http://*
* @license MIT License
* @require
* @javascript_url
*/
(function(){
var a,i=0,j=0,k=0,l,m,e,t,tp,s="",u="",w,d=document,e,v="",sp,xp,pf="",n=null,et="",br="<br /><br /><br />",cr="\n",ln;
var dp = new Object();
var tag = prompt("mode [a|img|external|mix|xpath]","mix");
switch(tag){
case 'a': /* aタグ */
xp='//a/@href';
break;
case 'external': /* imgを囲むリンク */
case 'mix': /* imgを囲むリンクとリンクに囲まれてないimg */
pf=prompt("xpath prefix\nex.\n//div[contains(@class,'entry-content')]\n//div[contains(@class,'permalink-tweet')]","//body");
xp=pf+'//a[(child::img)]/@href';
if (tag=='mix') xp+='|'+pf+'//img[not(ancestor::a)]/@src';
break;
case 'xpath': /* XPath */
xp=prompt("xpath (nodeValue)","//body//img/@src");
break;
default:
xp='//'+tag+'/@src'; /* 任意タグのsrc */
/* TODO: css/background-image, text-link, both href & img */
}
sp=document.evaluate(xp,d,n,7,n);
for(k=0;k<sp.snapshotLength;k++){
dp[sp.snapshotItem(k).nodeValue]=1;
}
var f = new RegExp(prompt("filter (regexp)",'\\.(jpg|jpeg|gif|png)$'),'i');
var keys=Object.keys(dp);
for(j=0, l=keys.length; j<l; j+=1) {
t=keys[j];
if(!t.match(f)){
continue;
}
if (t.match(/(http.+twimg\.com\/media\/)([^:\/]+)/i)) {
ln = '<a href="'+RegExp.$1+RegExp.$2+':orig" download="'+RegExp.$2+'" target="_blank">';
s += ln+RegExp.$2+'</a><br />'+cr;
v += ln+'<img src="'+RegExp.$1+RegExp.$2+':orig" width=200 height=200 /></a>'+cr;
u += t+cr;
}
else {
ln = '<a href="'+t+'" target="_blank">';
s += ln+t+"</a><br />"+cr;
v += ln+'<img src="'+t+'" width=200 height=200 /></a>'+cr;
u += t+cr;
}
}
if (u) {
if (confirm("thumbnail?")) et+="<div style='float:left'>"+v+"</div>";
et+="<div style='clear:left'>"+br+s+"</div>";
if(confirm("new window ?")){
window.open("data:text/html,"+encodeURIComponent("<html><head><title>Images</title></head><body>"+br+et+br+"</body></html>"),"_blank");
} else {
d.body.innerHTML += br+"<hr />"+br+et+br;
}
if(_jsaCScript){ if(confirm("copy?")) _jsaCScript.setClipBoardString(u); }
}
else {
alert('none');
}
})();