Skip to content

Instantly share code, notes, and snippets.

@mkanenobu
Last active April 24, 2020 05:02
Show Gist options
  • Save mkanenobu/7f9f0611b131bcd42dde79e3fc981dac to your computer and use it in GitHub Desktop.
Save mkanenobu/7f9f0611b131bcd42dde79e3fc981dac to your computer and use it in GitHub Desktop.
send selection to Google Translate
// japanese to english
(function() {
var text = window.getSelection();
var url = "https://translate.google.co.jp/?hl=ja#view=home&op=translate&sl=ja&tl=en&text=" + text;
window.open(url);
})();
// compiled with Google Closure Compiler
// javascript:window.open("https://translate.google.co.jp/?hl=ja#view=home&op=translate&sl=ja&tl=en&text="+window.getSelection());
// english to japanese
(function() {
var text = window.getSelection();
var url = "https://translate.google.co.jp/?hl=ja#view=home&op=translate&sl=en&tl=ja&text=" + text;
window.open(url);
})();
// compiled with Google Closure Compiler
// javascript:window.open("https://translate.google.co.jp/?hl=ja#view=home&op=translate&sl=en&tl=ja&text="+window.getSelection());
// if selected string starts alphanumeric, translate to japanese, else translate to english
(function() {
var text = window.getSelection().toString().replace(/\n/g, '%250A');
if (text.search(/^\w/) === 0) {
var url = "https://translate.google.co.jp/?hl=ja#view=home&op=translate&sl=en&tl=ja&text=";
} else {
var url = "https://translate.google.co.jp/?hl=ja#view=home&op=translate&sl=ja&tl=en&text=";
}
window.open(url + text);
})();
// compiled with Google Closure Compiler
// var a=window.getSelection().toString().replace(/\n/g, '%250A'),b=0===a.search(/^\w/)?"https://translate.google.co.jp/?hl=ja#view=home&op=translate&sl=en&tl=ja&text=":"https://translate.google.co.jp/?hl=ja#view=home&op=translate&sl=ja&tl=en&text=";window.open(b+a);
// DeepL
(function() {
var text = window.getSelection().toString().replace(/\n/g, '%250A');
if (text.search(/^\w/) === 0) {
var url = "https://www.deepl.com/translator#en/ja/";
} else {
var url = "https://www.deepl.com/translator#ja/en/";
}
window.open(url + text);
})();
// compiled
// var a=window.getSelection().toString().replace(/\n/g,"%250A"),b=0===a.search(/^\w/)?"https://www.deepl.com/translator#en/ja/":"https://www.deepl.com/translator#ja/en/";window.open(b+a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment