Pregunta

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>
¿Fue útil?

Solución

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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top