此页面提供了可与Google Translate http://translate.google.com/translate_buttons一起使用的小书签,但是这些小书签在同一标签/窗口中打开Goog​​le Translate并替换了原始页面。如何修改bookrmarklet代码(请参见下面的内容)以在新窗口/选项卡中打开?也可以有人简要解释一下代码的实际作用。非常感谢。

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。我已将代码修改为以下工作解决方案:

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)
};


但这会在新标签页中打开Goog​​le Translate,如果要在新窗口中打开Goog​​le Translate,则需要传递更多的参数window.open():

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")
};


只是一个问题,我已经意识到在Chrome浏览器中它可以按预期运行。但是在FF 18.0.2中,它也将原始页面替换为空白处,显示为:“ [对象窗口]”,URL栏包含整个脚本,如何避免这种情况,并保持原始页面显示,而无需返回上一页?

编辑2:
好的,我已经知道了,在这里建议使用asi:what is the [object Window]?我已经添加了void(0);在练习结束时。

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('...')

小书签代码只是检查用户是否在页面上选择了任何文本,然后在新的URL中使用该文本。我的建议是将代码从更改位置更改为打开新窗口。

关于javascript - JS-修改Google翻译的小书签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16446273/

10-12 13:13