本页面提供与Google Translate http://translate.google.com/translate_buttons一起使用的书签,但是这些书签在相同的标签页/窗口中打开Google翻译并替换原始页面。如何修改bookrmarklet代码(见下文)在新窗口/选项卡中打开?也有人可以简要地解释代码的真正作用。非常感谢。编号: 根据@DG。我已经体改代码如下工作方案:JS - 修改Google Translate的书签
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)
};
但这打开谷歌在新标签页翻译,有几个参数需要被传递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")
};
只有一个问题,我已经意识到,在谷歌浏览器它按预期工作。但在FF 18.0.2中,它也将原始页面替换为空白,其中显示:“[object Window]”,并且URL栏包含整个脚本,如何避免这种情况,并保持原始页面显示,而无需返回一页?
EDIT2: 好吧,我已经知道了,asi在这里建议:what is the [object Window]?我已经添加void(0);在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);
干杯
谢谢你的回答 – 2013-05-09 14:12:10