/*
* @title 俺仕様画像抽出ブックマークレット(a,img) by fashi2
* @description 動作選択式(抽出タグ、正規表現フィルタ、新窓)+XPathで画像リンクと非リンク画像の混在抽出
* @include http://*
* @license MIT License
* @require
*/
(function(){
var a,i=0,j=0,k=0,l,m,e,t,tp,s="",u="",w,d,e,v="",sp;
var tag = prompt("mode [a|img|special|xpath]","special");
var dp = new Object();
if (tag=='special') {
var xp=prompt("xpath prefix\nex.\n//div[contains(@class,'entry-content')]\n//div[contains(@class,'permalink-tweet')]","//body");
//imgタグを囲むaタグとaタグに囲まれてないimg
sp=document.evaluate(xp+'//a[(child::img)]/@href|'+xp+'//img[not(ancestor::a)]/@src', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
//TODO: css/background-image
for(k=0;k<sp.snapshotLength;k++){
dp[sp.snapshotItem(k).nodeValue]=1;
}
}
else if (tag=='xpath') {
var xp=prompt("xpath (nodeValue)","//body//img/@src");
sp=document.evaluate(xp, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for(k=0;k<sp.snapshotLength;k++){
dp[sp.snapshotItem(k).nodeValue]=1;
}
}
else {
a=document.getElementsByTagName(tag);
while (e=a[i++]) {
if (tag == 'a'){ tp=e.href; } else { tp=e.src; }
dp[tp]=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(/twimg\.com\/media\/([^\.]+)/i)) {
s += "<a href='"+t+":orig'>"+RegExp.$1+"</a><br />\n";
v += "<a href='"+t+":orig' target='_blank'><img src='"+t+":orig' width=200 height=200 /></a> ";
u += t+"\n";
}
else {
s += "<a href='"+t+"'>"+t+"</a><br />\n";
v += "<a href='"+t+"' target='_blank'><img src='"+t+"' width=200 height=200 /></a> ";
u += t+"\n";
}
}
if (u) {
if (tag=='xpath' && !xp.match(/img/)) { v=""; }
var ent="<div style='float:left;'>"+v+"</div><div style='clear:left;'><br /><br /><br />"+s+"</div>";
if(confirm("new window ?")){
window.open("data:text/html,"+encodeURIComponent("<html><head><title>Images</title></head><body><br /><br />"+ent+"<br /><br /></body></html>"),"_blank");
} else {
document.body.innerHTML += "<br /><hr /><br /><br /><br />"+ent+"<br /><br /><br />";
}
}
else {
alert('none!');
}
})();