打开Safari扩展时执行Swift

发布于 2025-02-06 14:28:47 字数 189 浏览 5 评论 0原文

我正在尝试创建一个Safari应用扩展程序,该扩展程序在打开Safari弹出窗口时执行一些本地Swift代码,而不是执行经典script.js文件。

首先,我可以做吗?如果是这样,怎么样?

对于权限,我需要访问activetabcookies

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情栀口红 2025-02-13 14:28:47

打开Safari弹出式

我假设您的意思是当单击sfsafaritoolbaritem时,野生动物园显示的弹出窗口?如果您牢记一些事情,请在这一点上运行本机Swift代码并不是问题。

  1. 通过XCode创建默认的MacOS应用:

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

  3. 确保您选择了正确类型的Safari扩展名。因为您想运行本机Swift代码,请使用“ Safari App扩展名”类型。 “ Safari Web扩展名”不适合您的情况。查看Apple的以下声明( source ):

使用Safari Web扩展名添加了自定义功能
JavaScript API和来自Google扩展的常见文件格式
Chrome,Mozilla Firefox和Microsoft Edge浏览器。而Safari应用程序
扩展对于在本机MacOS应用之间共享代码很有用
和Safari,您主要在JavaScript上构建Safari Web扩展程序,
html和CSS,可以重新包装在其他浏览器中工作。

  1. 默认情况下,sfsafaritoolbaritem的操作是命令。这就是为什么在每次点击时执行ToolbAritememed(IN:)方法的原因。但是,您想显示弹出窗口,因此在info.plist文件中将操作更改为popover
<key>SFSafariToolbarItem</key>
<dict>
    <key>Action</key>
    <string>Popover</string>
    <key>Identifier</key>
    <string>Button</string>
    <key>Image</key>
    <string>ToolbarItemIcon.pdf</string>
    <key>Label</key>
    <string>Run Swift code</string>
</dict>
  1. 从您的sfsafariextensionHandler中删除除popoverViewController()以外的所有方法。 popoverViewController()负责为POPOVER提供sfsafariextensionViewController。因此,请确保您已经实施了此类。 sfsafariextensionHandler现在应该看起来像:
class SafariExtensionHandler: SFSafariExtensionHandler {
    
    override func popoverViewController() -> SFSafariExtensionViewController {
        return SafariExtensionViewController.shared
    }

}
  1. popoverwillshow(_:)方法sfsafariextensionhandler > sfsafaritoolbaritem :
override func popoverWillShow(in window: SFSafariWindow) {
    NSLog("Swift code executed")
        
    // TODO: Add your code here
}

此时,整个实施均已完成。每当单击sfsafaritoolbaritem时,都会调用popoverwillshow(_:)方法,并且执行您的本机Swift代码。最后,通过popoverViewController()方法检索并显示您的sfsafariextensionViewController(popover)。

总而言之:您可以意识到自己的项目,Safari App扩展API为其提供了所有功能。如果您在我的解释后仍然不了解实现,这是相应的GitHub存储库: stackoverflow725849558 。祝您项目好运!

when the Safari popup is opened

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.

  1. Create a default macOS app via Xcode:
    step1
    step2

  2. Add a new "Safari Extension" target to your app:
    step3
    step4

  3. 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):

A Safari web extension adds custom functionality to Safari using
JavaScript APIs and common file formats from extensions for Google
Chrome, Mozilla Firefox, and Microsoft Edge browsers. While Safari App
Extensions are useful for sharing code between your native macOS app
and Safari, you build Safari web extensions primarily on JavaScript,
HTML, and CSS, and can repackage them to work in other browsers.

  1. By default, the action of the SFSafariToolbarItem is Command. That's why the toolbarItemClicked(in:) method is executed on every click. However, you want to display the popover, so change the action to Popover in the Info.plist file.
<key>SFSafariToolbarItem</key>
<dict>
    <key>Action</key>
    <string>Popover</string>
    <key>Identifier</key>
    <string>Button</string>
    <key>Image</key>
    <string>ToolbarItemIcon.pdf</string>
    <key>Label</key>
    <string>Run Swift code</string>
</dict>
  1. Remove all methods except popoverViewController() from your SFSafariExtensionHandler. popoverViewController() is responsible to provide the SFSafariExtensionViewController for the popover. So make sure you have implemented this class. SFSafariExtensionHandler should now look like this:
class SafariExtensionHandler: SFSafariExtensionHandler {
    
    override func popoverViewController() -> SFSafariExtensionViewController {
        return SafariExtensionViewController.shared
    }

}
  1. Implement the popoverWillShow(_:) method in the SFSafariExtensionHandler to be able to respond to clicks on the SFSafariToolbarItem:
override func popoverWillShow(in window: SFSafariWindow) {
    NSLog("Swift code executed")
        
    // TODO: Add your code here
}

At this point, the entire implementation is complete. Every time the SFSafariToolbarItem is clicked, the popoverWillShow(_:) method is called and your native Swift code is executed. Finally, your SFSafariExtensionViewController (the popover) is retrieved and displayed via the popoverViewController() 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文