【问题标题】:activeTab.url Safari extensionactiveTab.url Safari 扩展
【发布时间】:2013-08-21 07:44:24
【问题描述】:

我正在尝试创建一个扩展程序来使用谷歌翻译来翻译网页。 我撰写的网址如下:url google translate + current tab + &sl=auto&tl=it&hl=&ie=UTF-8 但不起作用。怎么了?

谢谢

<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>

【问题讨论】:

    标签: javascript google-translate safari-extension


    【解决方案1】:

    总的来说这是正确的,但也有一些简单的错误。

    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>
    

    【讨论】:

    • 嗨,马特,谢谢!现在工具栏中的按钮工作但不加载当前网址。大概在这里:encodeURIComponent(currentUrl)dropbox.com/s/uemccj6urji0vud/error.png
    • url 谷歌翻译 + 当前标签页 + &sl=auto&tl=it&hl=&ie=UTF-8 不显示
    • 确保在扩展构建器中“访问级别”设置为“全部”。
    猜你喜欢
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多