Created
September 15, 2012 14:44
-
-
Save tompng/3728290 to your computer and use it in GitHub Desktop.
Dictionary(ja<->en) for Safari #save this html-file to your local disk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<meta http-equiv="content-type" content="charset=utf-8"> | |
<title>dictionary</title> | |
<script> | |
var http; | |
var lang=["en","ja"]; | |
var url="http://translate.google.co.jp/translate_a/t?client=t"; | |
function $(id){return document.getElementById(id);} | |
function getText(){ | |
return $("text").value.split("\n").join(" "); | |
} | |
function getLang(text){ | |
for(var i=0;i<text.length;i++){ | |
if(text.charCodeAt(i)>128)return ["ja","en"] | |
} | |
return ["en","ja"]; | |
} | |
function abort(){if(http){http.abort();http=null;}} | |
function loadData(){ | |
var text=getText(); | |
if(!text)return; | |
if(http&&http.text==text)return; | |
abort(); | |
var data=getCache(text); | |
if(data){show(data);return;} | |
http=new XMLHttpRequest();http.text=text; | |
var lang=getLang(text); | |
http.open("GET",url+"&sl="+lang[0]+"&tl="+lang[1]+"&text="+encodeURIComponent(text),true); | |
http.onreadystatechange=cbFunc; | |
http.send(null); | |
setloading(true); | |
} | |
var timer=0; | |
function load(){ | |
clearTimeout(timer); | |
var text=getText(); | |
if(text==""){show(null);return;} | |
var data=getCache(text); | |
if(data){abort();show(data);return;} | |
timer=setTimeout(loadData,200); | |
} | |
function cbFunc(){ | |
if(!http||http.readyState!=4)return; | |
setloading(false); | |
var value=http.responseText; | |
var status=http.status; | |
var text=http.text; | |
http=null; | |
if(status!=200){$("sentence").innerHTML=$("dict").innerHTML="error";return;} | |
try{ | |
var data=eval("("+value+")"); | |
addCache(text,value); | |
show(data); | |
}catch(e){} | |
} | |
var loadingtimer=0,loadingcnt=0; | |
function loadingfunc(){ | |
$("loading").style.opacity=0.4+0.2*Math.cos(loadingcnt++/10); | |
} | |
function setloading(flag){ | |
if(flag){ | |
loadingcnt=0; | |
loadingtimer=setInterval(loadingfunc,10); | |
}else{ | |
clearInterval(loadingtimer); | |
$("loading").style.opacity=0; | |
} | |
} | |
function htmlchars(str){ | |
return str.split("&").join("&").split("<").join("<").split(">").join(">").split(" ").join(" ").split("\n").join("<br>"); | |
} | |
function show(data){ | |
$("sentence").innerHTML=$("dict").innerHTML=""; | |
if(data==null)return; | |
var st=data[0]; | |
var dict=data[1]; | |
if(st&&st[0]&&st[0][0]){ | |
$("sentence").innerHTML+="<h3>翻訳</h3>"; | |
$("sentence").innerHTML+=htmlchars(st[0][0])+"<br>"; | |
} | |
if(dict&&dict.length>0){ | |
$("dict").innerHTML+="<h3>辞書</h3>"; | |
for(var i=0;i<dict.length;i++){ | |
$("dict").innerHTML+="<h4>"+dict[i][0]+"</h4>" | |
$("dict").innerHTML+=htmlchars(dict[i][1].join(" ")); | |
} | |
} | |
} | |
function info(){ | |
var text="Cache-size: "+cachedata.list_length+"/"+cachedata.MAX_LEN+"\n"; | |
text+="--recent words--\n--------------------------------\n"; | |
for(var i=0,o=cachedata.list_tail;o&&i<20;i++,o=o.prev){ | |
text+=o.key+"\n--------------------------------\n"; | |
} | |
alert(text); | |
} | |
var cachedata={MAX_LEN:4096,list_length:0,list_head:null,list_tail:null,map:{}}; | |
function getCache(key){ | |
var node=cachedata.map[key]; | |
if(!node)return null; | |
if(node.next){ | |
if(!node.prev){ | |
cachedata.list_head=node.next; | |
node.next.prev=null; | |
}else{ | |
node.prev.next=node.next; | |
node.next.prev=node.prev; | |
} | |
node.next=null; | |
node.prev=cachedata.list_tail; | |
node.prev.next=node; | |
cachedata.list_tail=node; | |
} | |
return eval("("+node.value+")"); | |
} | |
function addCache(key,value){ | |
if(getCache(key))return; | |
var node={key:key,value:value}; | |
cachedata.map[key]=node; | |
if(cachedata.list_length==0){ | |
cachedata.list_head=cachedata.list_tail=node; | |
cachedata.list_length=1; | |
return; | |
} | |
node.prev=cachedata.list_tail; | |
cachedata.list_tail.next=node; | |
cachedata.list_tail=node; | |
cachedata.list_length++; | |
if(cachedata.list_length>cachedata.MAX_LEN){ | |
cachedata.list_length=cachedata.MAX_LEN; | |
delete cachedata.map[cachedata.list_head.key]; | |
cachedata.list_head=cachedata.list_head.next; | |
cachedata.list_head.prev=null; | |
} | |
} | |
function escapeBS(str){return str.split("\\").join("\\\\").split("\n").join("\\n");} | |
function unescapeBS(str){return str.split("\\\\").join("\\").split("\\n").join("\n");} | |
function onload(){ | |
$('text').focus(); | |
window.onunload=onexit; | |
var dict=[]; | |
try{dict=JSON.parse(localStorage.dictionary);}catch(e){console.log(e);} | |
for(var i=0;i<dict.length;i++)addCache(dict[i][0],dict[i][1]) | |
} | |
function onexit(){ | |
var data=[]; | |
for(var obj=cachedata.list_head;obj;obj=obj.next){ | |
data.push([obj.key,obj.value]); | |
} | |
console.log(data); | |
localStorage.dictionary=JSON.stringify(data); | |
return false; | |
} | |
</script> | |
<style> | |
body{font-size:24px;} | |
#text{width:100%;background:#dde;font-size:32px;border:none;outline:none;font-family:Serif;resize:none} | |
h3{background:#ddf;padding;0;margin:4px 0;font-size:32px;} | |
h4{background:#ddf;padding;0;margin:0 0;font-size:28px;} | |
#loading{font-size:16px;color:gray;} | |
form{display:inline} | |
</style> | |
<body onload="onload();" onkeypress="if(event.metaKey&&event.keyCode==105){info();return false;}"> | |
<form onsubmit="load();return false;"> | |
<textarea type=text oninput="setTimeout(load,0)" id="text"></textarea> | |
</form> | |
<span id="loading" style="opacity:0">loading...</span> | |
<div id="sentence"></div> | |
<br> | |
<div id="dict"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment