2013-08-21 76 views
2

我正嘗試使用谷歌翻譯來創建翻譯網頁的擴展。 我撰寫網址如:url google translate + current tab + &sl=auto&tl=it&hl=&ie=UTF-8但無效。怎麼了?activeTab.url Safari擴展

謝謝

<script> 
safari.application.addEventListener("command", performCommand, false); 

function performCommand(event) { 
    if (event.command == "translate") {  
     var currentTab.url = safari.application.activeBrowserWindow.currentTab.url;  
     var rUrl = "http://translate.google.it/translate?u=" + encodeURIComponent(currentTab.url) + "&sl=auto&tl=it&hl=&ie=UTF-8";  
     safari.application.activeBrowserWindow.activeTab.url(rUrl); 
    } 
} 
</script> 

回答

3

一般來說,這是正確的,但也有一些簡單的錯誤。

  1. 在行6上,var currentTab.url是無效的語法。只需調用變量如currentUrl即可。

  2. 在第6行,它是safari.application.activeBrowserWindow.activeTab而不是safari.application.activeBrowserWindow.currentTab

  3. 在線8上,url不是函數,它是一個屬性。只需將其分配給一個等於。

這應該工作:

<script> 
safari.application.addEventListener("command", performCommand, false); 

function performCommand(event) { 
    if (event.command == "translate") {  
     var currentUrl = safari.application.activeBrowserWindow.activeTab.url; 
     var rUrl = "http://translate.google.it/translate?u=" + encodeURIComponent(currentUrl) + "&sl=auto&tl=it&hl=&ie=UTF-8";  
     safari.application.activeBrowserWindow.activeTab.url = rUrl; 
    } 
} 
</script> 
+0

馬特您好,感謝!現在工具欄中的按鈕可以工作,但不會加載當前的URL。可能在這裏:encodeURIComponent(currentUrl)https://www.dropbox.com/s/uemccj6urji0vud/error.png – francy89

+0

url google translate + current tab +&sl = auto&tl = it&hl =&ie ie = UTF-8不顯示 – francy89

+0

確認在「擴展」構建器中將「訪問級別」設置爲「全部」。 –