I have Web Extensions which currently runs on Chrome, Firefox and Opera. Now I'm wondering is there a way to use same code to build Safari App Extension, maybe something like PhoneGap(wrap all existing JS code in Safari App Extension project) or there are limitations like tabs handling for javascript and some things just have to be written in native code.
Thanks
To create a Safari app extension: Launch Xcode and either open an existing project containing a macOS app or create a new one. Choose File > New > Target. From the list of templates in the Application Extension section, select Safari Extension, and click Next.
Apple has now made it easier for developers to convert an existing Chrome/Edge/Firefox extension to a Safari Web Extension. In their release of XCode 12, they have introduced the Safari Web Extension Converter tool which gives you the ability to convert extensions made for other browsers to Safari Web Extensions.
In the Safari app , you can install extensions to customize the way your browser works. For example, extensions can help you find coupons when shopping, block content on websites, give you access to features from other apps, and more.
Answer: A: Many chrome extensions are available as Safari extensions. With Safari open, go to Safari (next to the apple symbol) and click on Safari extensions.
May be it too late to share my finding, but nevertheless.
Short answer for the possibility of converting web extension to safari app extension is Yes. It definitely possible, But it's hard to do so.
I had a hard time Figuring out the best possible and minimal way to port web extension to safari app-extension.
Problems with porting Web Extension
Solution
Writing some amount of swift/objective-c code is inevitable. But writing a bridge code in swift/objective C to facilitate the communication between content-script and background-script makes life easier.
In the diagram, Content-script remains the same, the background-script is injected by creating a webview. Now Any message received from content-script is forward to the webView background-script. Also setup the browser extension API using evaluvateJavascript
webView.evaluateJavaScript("chrome.runtime.id=function() { return "+ extensionId + " }")
Electron JS uses web technologies like simple HTML, CSS, and JavaScript. It does not require native skills unless you want to do something advanced. It can be designed for a single browser. Its file system belongs to Node.js APIs and works on Linux, Mac OS X, Windows.
By using native node modules enables you to write in C++ and ObjectiveC (or Swift) and expose an API to node.js using v8. This gives you a lot of flexibility and power but requires the most time to develop and still being to reuse you background-script by running in electron context.
Sample Application built using this approach https://github.com/AdguardTeam/AdGuardForSafari
If you want to do Window/Tab Management in safari app extension refer WindowTabManagement
UPDATE
If you are planning to port your legacy extension with zero native code (Objective-c/swift). Please follow this repo https://github.com/avast/topee
Now there is an official support for this converting:
Converting a Web Extension for Safari
You can actually do this fairly easy.
You should be able to reuse Content Scripts from your Web-Extensions (but you need to rewrite code to be able to send/receive messages with "background"). Background script will have to be re-written in Swift or Objective-C.
You need to developp:
You have an (small) example on Apple website : https://developer.apple.com/documentation/safariservices/safari_app_extensions?changes=_2&language=objc
You should not use Extension Builder anymore (accessible from Developer menu in Safari) since this is for legacy (pure JS) extensions only. So you need to get in XCode.
Last thing, but not least... Safari App Extensions, compared to Safari legacy extensions, are "a bit" more limited (for instance, you won’t be able to open the toolbar popup programmatically, just as with Chrome web-extensions actually).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With