6

Is there a way through JavaScript to detect if a Safari Extension is already installed? https://extensions.apple.com has some way of doing it because they update the install link to "installed" if the extension is already installed. However, I can't figure out how they do it. I've traced it back to an object of type 'SafariExtensionGalleryController" but that's as far as I get.

Did Apple put special hooks into the extension system just for their stuff??

Lost...

Thx, Joel

JayRu
  • 319
  • 3
  • 11
  • When I go to that page, the install links do not change even for extensions which are installed. – dan8394 Aug 13 '11 at 14:39
  • You're right... for moment. When I went to the extensions URL last week, official extensions (the ones that work with one-click) had their links updated to say installed if they were installed. Now, none of the extensions seem to be official as they require a three-click installation. My guess is that it's a temporary but on Apple's site. – JayRu Aug 15 '11 at 14:32

2 Answers2

3

If you are the author of the extension, then you can detect if your own extension is installed. I simply inject an invisible string into my website which I then scan for when the page is loaded. If the string has been injected, my extension must be installed and you can then do whatever you like with the result.

identifier.js

if (window.top === window) 
{
    //detect if the extension has been installed and disable "Install" button if that's the case
    if (document.title === "YOUR PAGE TITLE") //we don't want to inject the string into any website, just ours
    {
        var p = document.createElement("noscript");
        var texto = document.createTextNode("Extension Installed");
        p.appendChild(texto);

        document.body.appendChild(p);
    }
}

I suspect Apple does this programmatically using the WebView that Safari uses to display websites and then runs javascript scripts internally that change the Extensions website depending on the extensions returned in Safari's code.

Hope this helps!

Pripyat
  • 2,937
  • 2
  • 35
  • 69
  • 1
    Instead of using `document.title` to determine if the current webpage is on your site, use `document.URL`. Any webpage can set its title to anything. – breakingobstacles Sep 30 '16 at 21:38
  • Make the content-script store all the necessary data inside a `` tag, which is the least obtrusive action to the native page. – avalanche1 Oct 03 '22 at 17:23
1

To detect your extension server-side, the easiest way is to inject a session cookie in your domain with a content script.

In Chrome and Firefox, you could inject an HTTP Header, but the API in Safari does not allow it.

The problem with cookies: if your user uninstall the extension, you will notice only when his session ends (closing browser).

Eloims
  • 5,106
  • 4
  • 25
  • 41