Safari App Extension: Inject script to specific tab

Can a Safari App Extension inject a JS or CSS file into a tab, dynamically, like the WebBrowser API's tabs.executeScript function? I know Safari Extensions didn't have this capability. Although there were the two functions below

safari.extension.addContentScript
safari.extension.addContentScriptFromURL

But those functions would not execute the script and acted like an update to the content scripts in the Info.plist. I'd like to see a function that executes a script on a specifc tab.

Replies

This is not currently something that is supported. What functionality are you trying to put in your extension that you need this dynamic script injection? That kind of information helps us schedule and prioritize what improvements to make to Safari App Extensions.

For what I'm currently working on I can get around it by wrapping some current functionality in conditionals and loading the scripts into specified pages.


But when I originally wrote the extension for Chrome this API made it a lot easier to load a single script that could then manage code execution by sending a message to the background to execute/add specific functionality as needed. It made the code base much more modular and maintainable.


I think adding similar functionality to Safari App Extension as found in the Browser Extension API would benefit the Safari App Extension eco system as developers can more easily devleop cross platform and maintain the extensions. Thanks for taking the time to respond and happy to keep throwing out suggestions!

Same for me +1

The usecase for me is that my extension does a lot of things and is pretty heavy on the content side (500kb js alltogether). If it gets injected everywhere, in every iframe, it will soon freeze the browser. Therefore, with the Chrome extension, I only inject a minimal 10K initializator script first, that only injects the rest if the document within the iframe has the contents that it can work on. That works very well.

This would be an essential API to be developed on Safari as far as I'm concerned.

Would also be great to be able to select wether injected scripts should also be injected in iFrames or not.

That could be also set in the bundle properties in SFSarariContentScript array.