JavaScript check if browser extension is installed for Chrome, Firefox and Opera

前端 未结 1 721
渐次进展
渐次进展 2021-02-09 17:34


I want to show custom bar ( notification like: Install our plugin. ) on our site if extension is not installed for Chrome, Firefox and Opera. None will b

1条回答
  •  花落未央
    2021-02-09 18:15

    If the extension is willing to cooperate, it could advertise its presence to the document easily. For example:


    The extension could do

    window.$$myExt = ...
    

    Then you can detect the extension by

    if(typeOf $$myExt !== 'undefined'){...
    

    (or any variation thereof)

    Obtaining the page window is somewhat tricky at least


    The extension could do

    document.body.classList.add("myExt-visited")
    

    Then you could detect the extension by

    if(document.body.classList.contains("myExt-visited")){...
    

    The extension could do

    document.body.innerHTML += "
    ..." // or $('body').append("
    ...");

    then you could detect the extension by

    if(document.getElementByID("myExt-toolbar")){...
    // or if($("#myExt-toolbar").length){...
    

    alternatively, you could do

    ...

    and the extension would do

    var replacement = document.getElementByID("myExt-replacement");
    replacement && replacement.remove();
    

    or you could do

    function onMyExtExists(){
      ...
    }
    

    and the extension would do

    onMyExtExists && onMyExtExists();
    

    0 讨论(0)
提交回复
热议问题