2013-05-08 64 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); 
    } 
}