gpt4 book ai didi

javascript - JS - 修改谷歌翻译的书签

转载 作者:行者123 更新时间:2023-11-28 02:15:37 47 4
gpt4 key购买 nike

此页面提供可与 Google 翻译一起使用的书签 http://translate.google.com/translate_buttons然而,这些小书签会在同一选项卡/窗口中打开谷歌翻译并替换原始页面。如何修改书签代码(见下文)以在新窗口/选项卡中打开?另外有人可以简要解释一下代码的真正用途吗?非常感谢。

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

但这会在新选项卡中打开谷歌翻译,如果您想在新窗口中打开谷歌翻译,还需要传递一些参数到 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")
};

只有一个问题,我意识到在 Google Chrome 中它可以按预期工作。但在 FF 18.0.2 中,它还用空白替换原始页面,其中显示:“[object Window]”,并且 URL 栏包含整个脚本,如何避免这种情况,并保持原始页面显示,而不需要返回一页?

编辑2:好的,我明白了,这里建议: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 - 修改谷歌翻译的书签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16446273/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com