打开Safari扩展时执行Swift
我正在尝试创建一个Safari应用扩展程序,该扩展程序在打开Safari弹出窗口时执行一些本地Swift代码,而不是执行经典script.js
文件。
首先,我可以做吗?如果是这样,怎么样?
对于权限,我需要访问activetab
和cookies
。
I'm trying to create a Safari App Extension that executes some local Swift code when the Safari popup is opened, instead of executing a classic script.js
file.
First of all, can I do it? If so, how?
For the permissions, I need to access activeTab
and cookies
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的意思是当单击
sfsafaritoolbaritem
时,野生动物园显示的弹出窗口?如果您牢记一些事情,请在这一点上运行本机Swift代码并不是问题。通过XCode创建默认的MacOS应用:


在您的应用中添加新的“ Safari扩展”目标:


确保您选择了正确类型的Safari扩展名。因为您想运行本机Swift代码,请使用“ Safari App扩展名”类型。 “ Safari Web扩展名”不适合您的情况。查看Apple的以下声明( source ):
sfsafaritoolbaritem
的操作是命令
。这就是为什么在每次点击时执行ToolbAritememed(IN:)
方法的原因。但是,您想显示弹出窗口,因此在info.plist文件中将操作更改为popover
。sfsafariextensionHandler
中删除除popoverViewController()
以外的所有方法。popoverViewController()
负责为POPOVER提供sfsafariextensionViewController
。因此,请确保您已经实施了此类。sfsafariextensionHandler
现在应该看起来像:popoverwillshow(_:)
方法sfsafariextensionhandler
> sfsafaritoolbaritem :此时,整个实施均已完成。每当单击
sfsafaritoolbaritem
时,都会调用popoverwillshow(_:)
方法,并且执行您的本机Swift代码。最后,通过popoverViewController()
方法检索并显示您的sfsafariextensionViewController
(popover)。总而言之:您可以意识到自己的项目,Safari App扩展API为其提供了所有功能。如果您在我的解释后仍然不了解实现,这是相应的GitHub存储库: stackoverflow725849558 。祝您项目好运!
I assume you mean the popover that Safari displays when the
SFSafariToolbarItem
is clicked? Running native Swift code at this point is not a problem if you keep a few things in mind.Create a default macOS app via Xcode:


Add a new "Safari Extension" target to your app:


Make sure that you have selected the correct type of Safari Extension. Because you want to run native Swift code, use the "Safari App Extension" type. "Safari Web Extension" is not suitable for your case. Take a look at the following statement from Apple about this (Source):
SFSafariToolbarItem
isCommand
. That's why thetoolbarItemClicked(in:)
method is executed on every click. However, you want to display the popover, so change the action toPopover
in the Info.plist file.popoverViewController()
from yourSFSafariExtensionHandler
.popoverViewController()
is responsible to provide theSFSafariExtensionViewController
for the popover. So make sure you have implemented this class.SFSafariExtensionHandler
should now look like this:popoverWillShow(_:)
method in theSFSafariExtensionHandler
to be able to respond to clicks on theSFSafariToolbarItem
:At this point, the entire implementation is complete. Every time the
SFSafariToolbarItem
is clicked, thepopoverWillShow(_:)
method is called and your native Swift code is executed. Finally, yourSFSafariExtensionViewController
(the popover) is retrieved and displayed via thepopoverViewController()
method.In summary: you can realize your project, Safari App Extension API provides all functionalities for it. If you still don't understand the implementation after my explanation, here is the corresponding GitHub repository: StackOverflow72584958. Good luck with your project!