![](/img/trans.png)
[英]Is there a way to open a tab in a Safari extension using javascript?
[英]Open new tab with JavaScript in Safari 12 [Safari App Extension]
我有此擴展,我想在該擴展的JavaScript部分中打開一個新標簽。 在將其遷移到Safari App Extension之前,我可以做
window.open(url, "_blank");
但是,當我在Safari 12中將其作為App Extension運行時,它將當前鏈接添加到閱讀列表(?!)。
當我在控制台中運行上面的代碼時,它將打開url
的新選項卡,但我必須在Safari偏好設置中啟用彈出窗口。 我在蘋果非常糟糕的文檔中找不到任何東西。
這甚至可以在“客戶端”上實現,還是我必須在Swift代碼中處理呢?
如果您想使用Safari App Extensions打開一個新標簽,則需要使用本機代碼進行。
您可以通過獲取活動窗口,然后在該窗口上調用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.
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.