Context menu item disappears in converted Safari Web Extension

I'm trying to convert an existing Chrome Extension to Safari Web Extension using this converter.

Immediately after convert, xcode opens and I build the project with my developer certificate. Everything looks perfect in Safari: the popup works fine, the context menu item is there, options are there.

The moment I restart Safari OR rebuild the project (even without changes), the context menu item disappears.

This is how context menu is defined in my context.js script:
Code Block javascript
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
"title": chrome.i18n.getMessage("contextMenu"),
"contexts": ["selection"],
"id": "context" + "selection"
});
});
chrome.contextMenus.onClicked.addListener(onClickHandler);


My manifest.json has this:
Code Block json
"permissions": ["activeTab", "storage", "contextMenus"],


I followed these instructions and added this key to info.plist in the folder "ProjectName Extension":
Code Block language
<key>SFSafariContextMenu</key>
<array>
<dict>
<key>Text</key>
<string>Context Menu Text</string>
<key>Command</key>
<string>commandMenu</string>
</dict>
</array>


After rebuilding the project, nothing changed.

Any ideas how to make context menu consistent?

Accepted Reply

Apparently there is a bug that context menus are not persisted between loads / launches.

So I deleted this part:
Code Block javascript
chrome.runtime.onInstalled.addListener(function() {


and with just this it works fine:
Code Block javascript
chrome.contextMenus.create({
"title": chrome.i18n.getMessage("contextMenu"),
"contexts": ["selection"],
"id": "selection"
});


Replies

Apparently there is a bug that context menus are not persisted between loads / launches.

So I deleted this part:
Code Block javascript
chrome.runtime.onInstalled.addListener(function() {


and with just this it works fine:
Code Block javascript
chrome.contextMenus.create({
"title": chrome.i18n.getMessage("contextMenu"),
"contexts": ["selection"],
"id": "selection"
});


Did you manage to get it working for Safari though? I assume the code you've shared above for Chrome doesn't work for a Safari web extension.
The code in the post doesn't work, the code in the comment does work in Safari.

In chrome it might throw a runtime error though if the background page reloads. Though there's no big harm in that.