Skip to content

Instantly share code, notes, and snippets.

@mgng
Created April 7, 2009 09:13
Show Gist options
  • Save mgng/91156 to your computer and use it in GitHub Desktop.
Save mgng/91156 to your computer and use it in GitHub Desktop.
Google翻訳APIで選択文字列を翻訳する
function trs(){
google.load('language','1',{'callback':function(){
var d=document,w=window,b=d.createElement('input');
b.type='button';
b.value='translate';
b.style.zIndex=99;
b.style.position='fixed';
b.style.top=0;
b.style.left='50%';
b.style.display='block';
b.onclick=function(){
var s=(w.getSelection?w.getSelection():d.selection.createRange().text)+'';
if(s!='')google.language.translate(s,'en','ja',function(r){alert(r.translation)})
};
d.body.insertBefore(b,d.body.firstChild)
}
})
};
(function(){
var s=document.createElement('script');
s.type='text/javascript';
s.src='http://www.google.com/jsapi?callback=trs';
document.body.appendChild(s)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment