本頁面提供與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