在 Safari 12 [Safari App Extension] 中使用 JavaScript 打开新标签页
Open new tab with JavaScript in Safari 12 [Safari App Extension]
我有这个扩展程序,我想在扩展程序的 JavaScript 部分打开一个新选项卡。在将其迁移到 Safari 应用程序扩展程序 之前,我可以这样做
window.open(url, "_blank");
但是当我 运行 在 Safari 12 中将其作为应用程序扩展时,它会将 current link 添加到阅读列表 (?!)。 =13=]
当我在控制台中 运行 上面的代码时,它会打开 url
的新选项卡,但我必须在 Safari 首选项中启用弹出窗口。我在 Apple 非常糟糕的文档中找不到任何内容。
这甚至可以在 "client side" 上实现,还是我必须在 Swift 代码中处理它?
如果您想使用 Safari App Extensions 打开一个新标签页,您需要在本机代码中执行此操作。
您可以通过获取活动 window,然后调用 window 上的 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
// Perform some action here after the page loads if you'd like.
})
}
我有这个扩展程序,我想在扩展程序的 JavaScript 部分打开一个新选项卡。在将其迁移到 Safari 应用程序扩展程序 之前,我可以这样做
window.open(url, "_blank");
但是当我 运行 在 Safari 12 中将其作为应用程序扩展时,它会将 current link 添加到阅读列表 (?!)。 =13=]
当我在控制台中 运行 上面的代码时,它会打开 url
的新选项卡,但我必须在 Safari 首选项中启用弹出窗口。我在 Apple 非常糟糕的文档中找不到任何内容。
这甚至可以在 "client side" 上实现,还是我必须在 Swift 代码中处理它?
如果您想使用 Safari App Extensions 打开一个新标签页,您需要在本机代码中执行此操作。
您可以通过获取活动 window,然后调用 window 上的 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
// Perform some action here after the page loads if you'd like.
})
}