Chrome Extension: How to detect if an extension is installed using Content Scripts

后端 未结 3 1973
小蘑菇
小蘑菇 2020-12-21 15:19

I am asking this question after looking at several related questions on stackoverflow. I started with how to detect if an extension is installed. I opted for the method wher

3条回答
  •  梦毁少年i
    2020-12-21 15:34

    I got this working...

    • Make the extension insert content script after DOM loads but before other resources like images are loaded

    manifest.json (added "run_at": "document_end")

    {
        "name": "Install Check",
        "content_scripts": [
            {
                "matches": ["http://host.com/*"],
                "js" : ["insert_node.js"],
                "run_at": "document_end"
            }
        ],
        "permissions": [
            "tabs", "host.com/*"
        ]
    }
    
    • use window.onload instead of $(document).ready

    insert_node.js (changed the $(document).ready to window.onload)

    window.onload = function() {
        if ($('#HOST_SITE').length > 0) {
            alert("you have our extension installed");
        } else {
            alert("not installed");
        }
    };
    

    However, I don't completely understand why using window.onload works and $(document).ready does not.

    May be someone can shed some light on that?

    Also I agree with @abraham that using chrome.app.isInstalled is a better way to do this (answer to my comment would be the icing on cake) :)

提交回复
热议问题