Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Created November 6, 2017 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iKlotho/4c9639e50c288e5f7d82c314160b0043 to your computer and use it in GitHub Desktop.
Save iKlotho/4c9639e50c288e5f7d82c314160b0043 to your computer and use it in GitHub Desktop.
greasemonkey translator with double click
// ==UserScript==
// @name clicktrans
// @namespace translator
// @version 1
// @include *://*/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant GM_addStyle
// ==/UserScript==
console.log('script runs');
var txt = '';
var api = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=tr&dt=t&q=';
body = document.getElementsByTagName('body') [0];
body.addEventListener('dblclick', function (e) {
translate(window.getSelection().toString(),e)
}, false);
body.addEventListener('click', function () {
$("#kwdHelpInfo").remove();
}, false);
function translate(txt,event) {
txt = txt.toLocaleLowerCase();
console.log(txt);
url = api + txt;
fetch(url).then(function (response) {
return response.json();
}).then(function (data) {
console.log(data);
showtranslation(data[0][0][0],event);
}).catch (function () {
console.log('Booo');
});
}
function showtranslation(data,e) {
console.log(data);
var settingsDiv = '<div style="position:fixed;top:topypx;left:topxpx;background:#f8b035;font-size:180%; border-radius:1ex;padding-top:11px;padding-right:15px;padding-bottom:15px;padding-left:15px; z-index: 777;" id="kwdHelpInfo">'+data+'</div>';
settingsDiv = settingsDiv.replace("topx",e.clientX+20);
settingsDiv = settingsDiv.replace("topy",e.clientY+20);
console.log(settingsDiv)
$('body').append(settingsDiv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment