Created
February 27, 2010 16:13
-
-
Save youpy/316795 to your computer and use it in GitHub Desktop.
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 translator | |
// @namespace http://d.hatena.ne.jp/youpy/ | |
// @include * | |
// @require http://github.com/cho45/jsdeferred/raw/master/jsdeferred.userscript.js | |
// ==/UserScript== | |
var div = document.createElement('div'); | |
div.className = 'gm_translator'; | |
window.addEventListener("mouseup", translate, false); | |
function translate(e) { | |
with(D()) { | |
var selection = window.getSelection().toString(); | |
if(selection && selection.match(/./) && selection.length < 400) { | |
var url = 'http://translator.jgate.de/convert?q=' | |
+ encodeURIComponent(selection) | |
+ '&to=ja&format=json'; | |
xhttp.get(url).next(function(res) { | |
var json = eval('(' + res.responseText + ')'); | |
addTip(json.result, | |
window.scrollX + e.clientX, | |
window.scrollY + e.clientY); | |
}); | |
} | |
} | |
} | |
function addTip(str, x, y) { | |
with(div.style) { | |
left = x + 'px'; | |
top = y + 'px'; | |
} | |
div.innerHTML = str; | |
document.body.appendChild(div); | |
window.addEventListener("mousedown", removeTip, false); | |
} | |
function removeTip() { | |
document.body.removeChild(div); | |
window.removeEventListener("mousedown", removeTip); | |
} | |
GM_addStyle(<><![CDATA[ | |
div.gm_translator { | |
font-size: 12px; | |
color: #fff; | |
position: absolute; | |
background: #000; | |
padding: 0.5em; | |
z-index: 999; | |
max-width: 300px; | |
opacity: 0.8; | |
-moz-border-radius: 3px; | |
} | |
]]></>); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment