Created
September 6, 2010 08:57
-
-
Save 8th713/566817 to your computer and use it in GitHub Desktop.
[Sleipnir] Google_Translate
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
// ==UserScript== | |
// @name Google_Translate | |
// @description 日本語じゃないページを見つけるとGoogle_Translateで翻訳するか聞いてくる | |
// @include http* | |
// @exclude http://translate.google.* | |
// @exclude http://google.*/* | |
// @exclude http://seesmic.com/* | |
// ==/UserScript== | |
(function(_d){ | |
if(_d.body.innerText.length===0) return; | |
var html = _d.getElementsByTagName('html')[0]; | |
if(html && html.lang==='ja') return; | |
// Advanced rate check | |
var ADVANSE = false; | |
// Threshold of translate | |
var RATE = 0.3; | |
var result; | |
if(ADVANSE) | |
result = checkRateA(); | |
else | |
result = checkRateN(); | |
if(result.rate <= RATE) | |
translate(result); | |
function checkRateN(){ | |
var ts,te; | |
ts = new Date * 1; | |
var arr = _d.body.innerText.split('\n'); | |
var len = arr.length; | |
var jp = 0; | |
for(var i=0; i<len; i++){ | |
if( /[ぁ-んァ-ンァ-ン]/.test(arr[i]) ) | |
jp++; | |
}; | |
te = new Date * 1; | |
return { rate: jp / len, time: te - ts }; | |
}; | |
function checkRateA(){ | |
var ts = new Date * 1; | |
var text = _d.body.innerText; | |
var len = text.length; | |
var jp = 0; | |
for(var i=0; i<len; i++){ | |
if( /[ぁ-んァ-ンァ-ン]/.test(text[i]) ) | |
jp++; | |
}; | |
var te = new Date * 1; | |
return { rate: jp / len, time: te - ts }; | |
}; | |
function translate(result){ | |
var isOldMode = ((typeof document.body.style.maxHeight == "undefined") || (document.compatMode != 'CSS1Compat')); | |
var head = _d.createElement('div'); | |
head.id = 'Sleipnir_Google_Translate'; | |
head.style.cssText = 'position:fixed; width:100%;top:0px; left:0px;padding:4px; margin:0px;background-color:lightgreen;text-align:center;z-index:9999;'; | |
if(isOldMode) head.style.position = 'absolute'; | |
var text = ['おや、どうやら日本語のページではないようです... ページによってはうまくいかない場合がありますがGoogle翻訳を試みますか?日本語率(', | |
Math.round(result.rate*1000) /10,'%) time: ',result.time,'ms'].join(''); | |
var p = _d.createElement('p'); | |
p.style.cssText = 'padding:0px; margin:0px;background-color:lightgreen;font-size:12px;color:#333;text-align:center;'; | |
p.innerText = text; | |
var btn1 = _d.createElement('a'); | |
btn1.href = "#"; | |
btn1.style.cssText = 'padding:0px 4px;font-size:12px;color:#33f;'; | |
btn1.innerText = '翻訳'; | |
var btn2 = _d.createElement('a'); | |
btn2.style.cssText = 'padding:0px 4px;font-size:12px;color:#33f;'; | |
btn2.href = "#"; | |
btn2.innerText = '閉じる'; | |
head.appendChild(p); | |
head.appendChild(btn1); | |
head.appendChild(btn2); | |
_d.body.appendChild(head); | |
btn1.onclick = click; | |
btn2.onclick = close; | |
}; | |
function click(){ | |
var t = ((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text)); | |
if(t!=''){ | |
var e = (document.charset||document.characterSet); | |
location.href = 'http://translate.google.com/?text=' | |
+ t | |
+ '&hl=ja&langpair=auto|ja&tbb=1&ie=' | |
+ e; | |
} | |
else{ | |
location.href='http://translate.google.com/translate?u=' | |
+ encodeURIComponent(location.href) | |
+ '&hl=ja&langpair=auto|ja&tbb=1&ie='; | |
}; | |
}; | |
function close(){ | |
document.getElementById('Sleipnir_Google_Translate').style.display = 'none'; | |
}; | |
})(document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment