【问题标题】:Is there a way to open a tab in a Safari extension using javascript?有没有办法使用 javascript 在 Safari 扩展程序中打开选项卡?
【发布时间】:2021-12-02 19:53:33
【问题描述】:

我正在尝试使用 javascript 打开一个新选项卡。它不起作用,我收到错误:

[Error] SyntaxError: Unexpected token ':'. Expected ')' to end an argument list.
    (anonymous function)

这是我的代码:

function openTab() {
    let myUrl = URL(string: "https://google.com")


    // This grabs the active window.
    SFSafariApplication.getActiveWindow { (activeWindow) in

        // Request a new tab on the active window, with the URL we want.
        activeWindow?.openTab(with: myUrl, makeActiveIfPossible: true, completionHandler: {_ in
        })
    }
}

openTab();

谢谢!

【问题讨论】:

    标签: javascript safari


    【解决方案1】:

    语法错误。您忘记了第二行对象中的大括号。 应该是

    URL({ string: "https:your.url" });
    

    代替:

    URL(string: "https:your.url");
    

    【讨论】:

    • 谢谢!但是,现在我收到错误:SyntaxError: Unexpected token '{'
    • 现在我深入研究了,你确定上面的代码是javascript吗? SFSafariApplication 之后的部分不对应任何 JS 语法。特别是在括号中指定所有参数,如(parameter: value),它对我来说就像 Dart。
    • 谢谢。我不知道如何使用 safari 打开标签,所以我在别人的问题上找到了代码。这是我获得代码的链接:link。使用window.open(url, "_blank"); 会起作用吗?我试过了,但它对我不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多