Is it possible to open a Safari WEB Extension popup through a command or keyboard shortcut?
I have a Safari Web Extension whose popup I want to access quickly through a Command or Keyboard Shortcut. I don’t care if the configuration for this is inside or outside the extension’s code.
The main problem I have is that, since this is a Safari WEB Extension (not App), it’s more difficult to interact with Safari functionalities.
From what I could find, there’s no built-in way to do it, but a workaround could be to add an event listener in content.js
and then send a message to SafariWebExtensionHandler
through browser.runtime.sendNativeMessage
inside background.js
to open the popup. I couldn’t find anything on how to do the latter.
content.js
document.addEventListener('keydown', function(event) {
// Check if the Control key is pressed
if (event.ctrlKey) {
console.log('Control key pressed!');
browser.runtime.sendMessage("open").then((response) => {
console.log("Received response: ", response);
});
}
});
background.js
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
browser.runtime.sendNativeMessage(
"com.owner.MyExtension",
"open"
)
.then((response) => {
sendResponse(response.echo);
});
});
SafariWebExtensionHandler.swift
class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
func beginRequest(with context: NSExtensionContext) {
...
// YOUR CODE
...
}
}
What I’ve tried:
- Add
"commands"
to themanifest.json
, but that is only for inside the app commands. - How to open Safari Extension ToolbarItem popover programmatically, but that only works for Safari App Extension. Mine is a Safari WEB Extension. (
SFSafariApplication.getActiveWindow
doesn’t work in this context)