JS-修改Google翻译的小书签 [英] JS - Modify bookmarklet for Google Translate
问题描述
此页面提供了可与Google Translate一起使用的小书签 http://translate.google.com/translate_buttons,但是这些小书签会在相同的标签/窗口中打开Google翻译,并替换原始页面.如何修改bookrmarklet代码(请参见下面的内容)以在新窗口/选项卡中打开?也可以请人简要解释一下代码的实际作用.非常感谢.
This page offers bookmarklets to be used with Google Translate http://translate.google.com/translate_buttons however those bookmarklets opens the Google Translate in same tab/window and replaces the original page. How to modify the bookrmarklet code (see bellow) to open in new window/tab? Also can somebody please briefly explains what the code is really doing. Thanks a lot.
javascript: var t = ((window.getSelection && window.getSelection()) || (document.getSelection && document.getSelection()) || (document.selection && document.selection.createRange && document.selection.createRange().text));
var e = (document.charset || document.characterSet);
if (t != '') {
location.href = 'http://translate.google.com/?text=' + t + '&hl=en&langpair=auto|en&tbb=1&ie=' + e;
} else {
location.href = 'http://translate.google.com/translate?u=' + encodeURIComponent(location.href) + '&hl=en&langpair=auto|en&tbb=1&ie=' + e;
};
根据@DG.我已将代码修改为以下工作解决方案:
According to @DG. I have modifed the code to the following working solution:
javascript: var t = ((window.getSelection && window.getSelection()) || (document.getSelection && document.getSelection()) || (document.selection && document.selection.createRange && document.selection.createRange().text));
var e = (document.charset || document.characterSet);
if (t != '') {
window.open('http://translate.google.com/?text=' + t + '&hl=en&langpair=auto|sk&tbb=1&ie=' + e)
} else {
window.open('http://translate.google.com/translate?u=' + encodeURIComponent(location.href) + '&hl=en&langpair=auto|sk&tbb=1&ie=' + e)
};
但是这会在新选项卡中打开Google Translate,如果要在新窗口中打开Google Translate,则需要传递更多的参数window.open().
but this opens Google Translate in new tab, there are few more arguments which needs to be passed window.open() if you want to open Google Translate in new window:
javascript: var t = ((window.getSelection && window.getSelection()) || (document.getSelection && document.getSelection()) || (document.selection && document.selection.createRange && document.selection.createRange().text));
var e = (document.charset || document.characterSet);
if (t != '') {
var url1 = 'http://translate.google.com/?text=' + t + '&hl=en&langpair=auto|sk&tbb=1&ie=' + e;
window.open(url1, '_blank', "GoogleTranslate", "height=200,width=200")
} else {
var url2 = 'http://translate.google.com/translate?u=' + encodeURIComponent(location.href) + '&hl=en&langpair=auto|sk&tbb=1&ie=' + e;
window.open(url2, '_blank', "GoogleTranslate", "height=200,width=200")
};
仅一个问题,我已经意识到在Google Chrome浏览器中它可以按预期运行.但是在FF 18.0.2中,它也将原始页面替换为空白处,显示为:"[对象窗口]",URL栏包含整个脚本,如何避免这种情况,并保持原始页面显示,而无需返回上一页?
Just one question, I have realized that in Google Chrome it is working as expected. But in FF 18.0.2 it also replaces original page with blank where is shown: "[object Window]" and URL bar contains the whole script, how to avoid this, and keep original page displayed, without needing to go back one page?
好的,我已经知道了,因为它是在这里建议的: [对象窗口]是什么?我添加了void(0);在练习结束时.
OK I have got it, asi it was suggested here: what is the [object Window]? I have added void(0); at the end of the scipt.
javascript: var t = ((window.getSelection && window.getSelection()) || (document.getSelection && document.getSelection()) || (document.selection && document.selection.createRange && document.selection.createRange().text));
var e = (document.charset || document.characterSet);
if (t != '') {
var url1 = 'http://translate.google.com/?text=' + t + '&hl=en&langpair=auto|sk&tbb=1&ie=' + e;
window.open(url1, '_blank', "GoogleTranslate", "height=200,width=200")
} else {
var url2 = 'http://translate.google.com/translate?u=' + encodeURIComponent(location.href) + '&hl=en&langpair=auto|sk&tbb=1&ie=' + e;
window.open(url2, '_blank', "GoogleTranslate", "height=200,width=200")
};
void(0);
欢呼
推荐答案
在两个位置将location.href = '...'
更改为window.open('...')
.
bookmarklet代码只是检查用户是否选择了页面上的任何文本,然后在新的URL中使用了该文本.我的建议是将代码从更改位置更改为打开新窗口.
The bookmarklet code is just checking if user selected any text on the page and then using that text in a new URL. My suggestion will modify code from changing the location to opening a new window.
这篇关于JS-修改Google翻译的小书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!