1
votes

I currently implement a Safari App Extension, which I want to have the ability to show the extension's popover using a keyboard shortcut.

Using the old Safari Extension API, this was done by using

safari.extension.toolbarItem[0].showPopover()

in the inserted JavaScript. This, however, is not possible anymore in the new Safari App Extension API.

What I'm currently doing is to listen to a shortcut in the injected JavaScript and dispatch a message to the extension:

document.onkeyup = function(e) {
    if (e.shiftKey && e.altKey && e.which == 80) {
        safari.extension.dispatchMessage("showPopover");
    }
};

In the extension, I have a message listener, which should load the popover:

override func messageReceived(withName messageName: String, from page: 
  SFSafariPage, userInfo: [String : Any]? = nil) { 
    page.getPropertiesWithCompletionHandler { properties in
        if messageName == "showPopover" {
            // Here the popover should be loaded...
        }
    }
}

I can not figure out how to do this. The Apple documentation does not provide any information. Stackoverflow and Github also did not help. I found, however, the following two questions on Stackoverflow, which seem to try to do a similar thing, but with no luck: [1], [2]

Any help on this? Is it just not possible? Or am I missing something?

Thank you for your help.

1

1 Answers

0
votes

They don't have support to open popover dynamically. But anyhow if you manage to change info.plist file dynamically and able to inject into the extension, then you'll be able to do so.

Another way is, open info.plist file and go for "SFSafariToolbarItem" property, then change "Action" to "Popover"

Your code will like following,

<key>SFSafariToolbarItem</key>
    <dict>
        <key>Action</key>
        <string>Popover</string>
        <key>Identifier</key>
        <string>Button</string>
        <key>Image</key>
        <string>ToolbarItem.pdf</string>
        <key>Label</key>
        <string>Test Safari Extension</string>
    </dict>

Good luck.