문제

I'm trying to create an extension to translate a web page using google translate. I compose url like: url google translate + current tab + &sl=auto&tl=it&hl=&ie=UTF-8 but doesn't work. What's wrong?

Thank you

<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>
도움이 되었습니까?

해결책

In general this is correct, but there are a few simple mistakes.

  1. On line 6, var currentTab.url is not valid syntax. Just call the variable something like currentUrl.

  2. On line 6, it is safari.application.activeBrowserWindow.activeTab not safari.application.activeBrowserWindow.currentTab.

  3. On line 8, url is not a function, it is a property. Just assign it with an equals.

This should work:

<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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top