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

前端 未结 1 719
渐次进展
渐次进展 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 += "<div id='myExt-toolbar'>..."
    // or $('body').append("<div id='myExt-toolbar'>...");
    

    then you could detect the extension by

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

    alternatively, you could do

    <div id="myExt-replacement">
       ...
    

    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)
提交回复
热议问题