JayRu
JayRu

Reputation: 319

Detect if Safari Extension is Installed

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

Upvotes: 6

Views: 3163

Answers (2)

Eloims
Eloims

Reputation: 5224

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).

Upvotes: 1

Pripyat
Pripyat

Reputation: 2937

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!

Upvotes: 3

Related Questions