PR

ブックマークレットでGoogle翻訳

XWSS017936_s4

先日と同じ要領だけれど、ブラウザで選択した語句をGoogle翻訳で開くサンプル。

スポンサードリンク

ブックマークレットでGoogle翻訳

接続先URLを以下に変更しただけだけどメモ程度に。

http://translate.google.co.jp/#auto/ja/

Google翻訳のURLは次のようになっているようだ。

http://translate.google.co.jp/#<翻訳元>/<翻訳先>/

翻訳元が#enなら英語で、翻訳先が/jaなら日本語となり、「英語を日本語に」という意味になるらしい。

Google翻訳には、「言語を自動検出」というのがあって、これを指定する場合は「#auto」になるようだ。

以下のコードは、元の言語を自動検出し、日本語に翻訳する設定になっている。

サンプルコード

javascript:(function(){
    x=document;
    y=window;
    if(x.selection){
        query=x.selection.createRange().text;
    }else if(y.getSelection){
        query=y.getSelection();
    }else if(x.getSelection){
        query=x.getSelection();
    };
    var w=window.ope();
    w.location.href='http://translate.google.co.jp/#auto/ja/'+encodeURIComponent(query)+'';
})();

1行コード

javascript:(function(){x=document;y=window;if(x.selection){query=x.selection.createRange().text;}else%20if(y.getSelection){query=y.getSelection();}else%20if(x.getSelection){query=x.getSelection();};var%20w=window.open();w.location.href='http://translate.google.co.jp/#auto/ja/'+encodeURIComponent(query)+'';})();

コメント