2013-05-08 133 views
1

本页面提供与Google Translate http://translate.google.com/translate_buttons一起使用的书签,但是这些书签在相同的标签页/窗口中打开Goog​​le翻译并替换原始页面。如何修改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); 

干杯

回答

1

变化location.href = '...'window.open('...')在两个地方。

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

+0

谢谢你的回答 – 2013-05-09 14:12:10

0

为什么不只是将翻译栏添加到页面?如果页面尚未包含翻译栏(div.skiptranslate中的iframe),则它会等待,直到它看到通过注入translate 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); 
    } 
}