【问题标题】:JS - Modify bookmarklet for Google TranslateJS - 修改 Google 翻译的书签
【发布时间】:2013-05-08 17:02:07
【问题描述】:

此页面提供可与 Google 翻译一起使用的书签 http://translate.google.com/translate_buttons,但是这些书签会在同一选项卡/窗口中打开 Google 翻译并替换原始页面。如何修改 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)
};

但是这会在新标签中打开谷歌翻译,如果你想在新窗口中打开谷歌翻译,还需要传递几个参数 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);在 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);

干杯

【问题讨论】:

    标签: javascript bookmarklet google-translate


    【解决方案1】:

    在两个地方将location.href = '...' 更改为window.open('...')

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

    【讨论】:

      【解决方案2】:

      为什么不直接将翻译栏添加到页面?如果页面还没有包含翻译栏(div.skiptranslate 中的 iframe),那么它会等到看到通过注入翻译 javascript 加载的 google.translate.TranslateElement 函数,然后调用它来绘制工具栏.

      (function () {
        function loadJS(url, callback) {
          var s = document.createElement('script');
          s.src = url;
          if (s.addEventListener) {
            s.addEventListener('load', callback, false);
          } 
          else {
            s.onreadystatechange = function () {
              if (this.readyState == 'complete') {
                callback();
                s = null;
              }
            }
          }
          s.type = 'text/javascript';
          document.getElementsByTagName('head') [0].appendChild(s);
        };
        loadJS('https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit', function () {
          window.setTimeout(doTrans, 100);
        });
      })();
      
      function doTrans()
      {
        if (!document.querySelector("div.skiptranslate")) {
          if (typeof google != "undefined" && typeof google.translate != "undefined" && typeof google.translate.TranslateElement != "undefined")
            new google.translate.TranslateElement({layout:google.translate.TranslateElement.InlineLayout.SIMPLE,autoDisplay:true},null);
          window.setTimeout(doTrans, 100);
        }  
      }
      

      【讨论】:

        猜你喜欢
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 2011-02-23
        • 2021-04-03
        • 1970-01-01
        • 2023-02-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多