9

Is there a way for a Safari browser extension to detect its own uninstallation?

I would like to send an event to Google Analytics to keep track of the uninstallment rate of my extension.

  • I am using localStorage to keep the extension's state (= 'needs uninstallation'). Problem is that this value does not clear when the user uninstalls the ext. If I knew that the extension was uninstalled I could clear localStorage returning to clean slate. – Maciej Jankowski Jul 21 '14 at 08:10

2 Answers2

2

After crawling around on the development forums, I found out that apparently Safari has no such events (unlike Chrome) or if there are any, they are undocumented (and therefore not meant to be used). I think your best bets are:

a) Request the feature via Apple's bug-reporting and feature-requesting site

b) Set up an external service that can monitor ~/Library/Safari/Extensions or the Extensions.plist file for deletion (which is probably enormously difficult, considering the security risks)

c) Keep track of the active user base instead by pinging an external service once a day or on some other interval. The deletion/inactivity rate can then be estimated by drops in activity.

mleyfman
  • 582
  • 3
  • 11
  • any ideas how to solve my use case from above comment? – Maciej Jankowski Jul 26 '14 at 10:44
  • @MaciejJankowski localStorage is cleared only when safari clears it via reset Safari, or when you clear it in your extension. Since there is no way to detect uninstallation, there isn't a way to clear it upon uninstallation. It is up to the user to clear their localStorage, which is weird, but that's how the current system works. – mleyfman Jul 26 '14 at 19:08
  • 1
    @MaciejJankowski, you might also want to look into AppleScripts, as some popular extensions (1Password and others) utilize them to add hooks to outside the extension sandbox. – mleyfman Jul 27 '14 at 22:17
  • it appears that extension settings do not get preserved unlike localStorage in the extension's background page - that was the answer I was looking for. – Maciej Jankowski Jul 28 '14 at 10:13
0

You can register the window.onunload event in the global file script to detect when extension is uninstalled. Better to use addEventListener() with 'unload' as safari could be using the event to clear extension storage. Also make sure to detect if the event is firing in case of extension update or reload because in these cases the global page is unloaded as well.

window.addEventListener("unload", function() {
  //handle uninstall event
});