Check whether user has a Chrome extension installed

前端 未结 16 2089
一向
一向 2020-11-22 08:50

I am in the process of building a Chrome extension, and for the whole thing to work the way I would like it to, I need an external JavaScript script to be able to detect if

相关标签:
16条回答
  • 2020-11-22 09:21

    I used the cookie method:

    In my manifest.js file I included a content script that only runs on my site:

     "content_scripts": [
            {
            "matches": [
                "*://*.mysite.co/*"
                ],
            "js": ["js/mysite.js"],
            "run_at": "document_idle"
            }
        ], 
    

    in my js/mysite.js I have one line:

    document.cookie = "extension_downloaded=True";
    

    and in my index.html page I look for that cookie.

    if (document.cookie.indexOf('extension_downloaded') != -1){
        document.getElementById('install-btn').style.display = 'none';
    }
    
    0 讨论(0)
  • 2020-11-22 09:23

    Another possible solution if you own the website is to use inline installation.

    if (chrome.app.isInstalled) {
      // extension is installed.
    }
    

    I know this an old question but this way was introduced in Chrome 15 and so I thought Id list it for anyone only now looking for an answer.

    0 讨论(0)
  • 2020-11-22 09:25

    Webpage interacts with extension through background script.

    manifest.json:

    "background": {
        "scripts": ["background.js"],
        "persistent": true
    },
    "externally_connectable": {
        "matches": ["*://(domain.ext)/*"]
    },
    
    background.js:
    chrome.runtime.onMessageExternal.addListener(function(msg, sender, sendResponse) {
        if ((msg.action == "id") && (msg.value == id))
        {
            sendResponse({id : id});
        }
    });
    

    page.html:

    <script>
    var id = "some_ext_id";
    chrome.runtime.sendMessage(id, {action: "id", value : id}, function(response) {
        if(response && (response.id == id)) //extension installed
        {
            console.log(response);
        }
        else //extension not installed
        {
            console.log("Please consider installig extension");
        }
    
    });
    </script>
    
    0 讨论(0)
  • 2020-11-22 09:27

    If you're trying to detect any extension from any website, This post helped: https://ide.hey.network/post/5c3b6c7aa7af38479accc0c7

    Basically, the solution would be to simply try to get a specific file (manifest.json or an image) from the extension by specifying its path. Here's what I used. Definitely working:

    const imgExists = function(_f, _cb) {
        const __i = new Image();
        __i.onload = function() {
            if (typeof _cb === 'function') {
                _cb(true);
            }
        }
        __i.onerror = function() {
            if (typeof _cb === 'function') {
                _cb(false);
            }
        }
        __i.src = _f;
        __i = null;
    });
    
    try {
        imgExists("chrome-extension://${CHROME_XT_ID}/xt_content/assets/logo.png", function(_test) {
            console.log(_test ? 'chrome extension installed !' : 'chrome extension not installed..');
            ifrm.xt_chrome = _test;
            // use that information
        });
    } catch (e) {
        console.log('ERROR', e)
    }
    
    0 讨论(0)
提交回复
热议问题