Check whether user has a Chrome extension installed

前端 未结 16 2434
一向
一向 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:12

    I thought I would share my research on this. I needed to be able to detect if a specific extension was installed for some file:/// links to work. I came across this article here This explained a method of getting the manifest.json of an extension.

    I adjusted the code a bit and came up with:

    function Ext_Detect_NotInstalled(ExtName, ExtID) {
      console.log(ExtName + ' Not Installed');
      if (divAnnounce.innerHTML != '')
        divAnnounce.innerHTML = divAnnounce.innerHTML + "
    " divAnnounce.innerHTML = divAnnounce.innerHTML + 'Page needs ' + ExtName + ' Extension -- to intall the LocalLinks extension click here'; } function Ext_Detect_Installed(ExtName, ExtID) { console.log(ExtName + ' Installed'); } var Ext_Detect = function (ExtName, ExtID) { var s = document.createElement('script'); s.onload = function () { Ext_Detect_Installed(ExtName, ExtID); }; s.onerror = function () { Ext_Detect_NotInstalled(ExtName, ExtID); }; s.src = 'chrome-extension://' + ExtID + '/manifest.json'; document.body.appendChild(s); } var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; if (is_chrome == true) { window.onload = function () { Ext_Detect('LocalLinks', 'jllpkdkcdjndhggodimiphkghogcpida'); }; }

    With this you should be able to use Ext_Detect(ExtensionName,ExtensionID) to detect the installation of any number of extensions.

提交回复
热议问题