macossafari-app-extension

Injected Javascript from Safari App Extension stops working once I make any modification


I'm trying to develop my first Safari App Extension and I followed Apple's document on building a safari app extension and my Xcode version is 11.5. It appears that the extension worked, as I can see the extension button loaded on Safari navigation bar, and I can get the "Hello World!" from the injected script.js in SFSafariExtensionHandler subclass's messageReceived method when a page is loaded in Safari. So far so good with all with the code auto generated by Xcode when the Safari Extension was added. However, any modification of the script.js, like adding a new empty line, or just repeating the same dispatchMessage call, would render the extension unable to run , like the code below:

document.addEventListener("DOMContentLoaded", function(event) {
    safari.extension.dispatchMessage("Hello World!");
    safari.extension.dispatchMessage("Hello World!");
});

This is very frustrating, particularly considering the auto generated extension code already generates a ton of warning and error messages even when it appeared to be working. But I do see there are many Safari Extension products in App Store, so could anybody point me in the right direction?


Solution

  • Figured this out: we have to clean the build every time the script is modified. Then the extension debug run works again. Cleaning also works if we modified the info.plist and get code signing errors in debug runs.