Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

javascript - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...