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.
On line 6, var currentTab.url
is not valid syntax. Just call the variable something like currentUrl
.
On line 6, it is safari.application.activeBrowserWindow.activeTab
not safari.application.activeBrowserWindow.currentTab
.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.