135

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 a user has my extension installed.

For example: A user installs my plugin, then goes to a website with my script on it. The website detects that my extension is installed and updates the page accordingly.

Is this possible?

Yehuda Katz
  • 28,535
  • 12
  • 89
  • 91
  • 4
    Yes, it is possible to detect extensions, so long as you know your extension ID (which I'm sure you do). Check this site for more information: http://blog.kotowicz.net/2012/02/intro-to-chrome-addons-hacking.html Skip down to the section on 'Finding your addons one by one'. Good luck! – Martin Hughes Feb 29 '12 at 23:14
  • The proper way to implement this is described by BJury bellow. – Rahatur Mar 29 '18 at 08:18
  • this post helped: https://ide.hey.network/post/5c3b6c7aa7af38479accc0c7 – nab. Jan 20 '19 at 22:31

19 Answers19

148

Chrome now has the ability to send messages from the website to the extension.

So in the extension background.js (content.js will not work) add something like:

chrome.runtime.onMessageExternal.addListener(
    function(request, sender, sendResponse) {
        if (request) {
            if (request.message) {
                if (request.message == "version") {
                    sendResponse({version: 1.0});
                }
            }
        }
        return true;
    });

This will then let you make a call from the website:

var hasExtension = false;

chrome.runtime.sendMessage(extensionId, { message: "version" },
    function (reply) {
        if (reply) {
            if (reply.version) {
                if (reply.version >= requiredVersion) {
                    hasExtension = true;
                }
            }
        }
        else {
          hasExtension = false;
        }
    });

You can then check the hasExtension variable. The only drawback is the call is asynchronous, so you have to work around that somehow.

Edit: As mentioned below, you'll need to add an entry to the manifest.json listing the domains that can message your addon. Eg:

"externally_connectable": {
    "matches": ["*://localhost/*", "*://your.domain.com/*"]
},

2021 Update: chrome.runtime.sendMessage will throw the following exception in console if the extension isn't installed or it's disabled.

Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist

To fix this, add this validation inside the sendMessage callback

if (chrome.runtime.lastError) {
    // handle error 
}
Pietro Nadalini
  • 1,722
  • 3
  • 13
  • 32
BJury
  • 2,526
  • 3
  • 16
  • 27
  • 3
    This works like a charm. Another drawback is of course that you have to control the extension - you can't use this to see if an arbitrary third-party extension is installed. – Eric P Feb 07 '14 at 00:47
  • 5
    @EricP The original question stated that they were writing the extension, so the issue is moot. – BJury May 20 '14 at 08:10
  • 14
    You will also have to add the following to your manifest.json: "externally_connectable": { "matches": ["*://*.yourdomain.com/*"] } – noname Sep 01 '14 at 10:54
  • 4
    It should be {version: '1.0'} and not {version: 1.0} or else you'll get 'Uncaught SyntaxError: Unexpected number' in extension Inspect view console. – ET-CS Jan 26 '15 at 05:01
  • 2
    On the "checking" side (the web page trying to check availability of a given extension), chrome.runtime is undefined, chrome 36 on linux. – reallynice Feb 11 '15 at 13:12
  • @niconic That doesn't sound right. Its been in [Chrome since 22](https://developer.chrome.com/extensions/runtime). (And sendMessage has been there since [26](https://developer.chrome.com/extensions/runtime#method-sendMessage).) – BJury Feb 12 '15 at 14:30
  • @niconic This is the error you'll receive if you don't implement the "externally_connectable" property in your manifest as per noname's comment – CodingIntrigue Feb 18 '15 at 10:03
  • 1
    This didn't work for me untill I installed the extension part into a background script. Hint from Dawid Szymański's answer. – Íhor Mé May 19 '17 at 00:44
  • 1
    @user This is not correct, as chrome.runtime.sendMessage is not available on the webpage side, and you get "chrome.runtime is undefined". Note that it IS available in a content script, just not a webpage. There is an open bug to fix that, but as of FF 54 it is not resolved. See https://bugzilla.mozilla.org/show_bug.cgi?id=1319168 – Haydentech Aug 01 '17 at 21:02
  • @BillHayden thanks for clarifying. I was using a polyfill of sorts from https://github.com/EmailThis/extension-boilerplate that made it available. Removing the common to prevent confusion. – user Aug 10 '17 at 01:22
  • The extension side of the code must be declared in the background component of the app, not the content. There is no onMessageExternal availability inside content.js as per the documentation https://developer.chrome.com/extensions/runtime#event-onMessageExternal – guiomie Aug 16 '17 at 04:18
  • 1
    FWIW, this is a Chrome-only solution — doesn't work on Firefox's new WebExtensions. – mlissner Mar 26 '18 at 22:43
  • when i tried with above code my chrome Version 60.0.3112.113. every time i am getting reply object is undefined and hasExtension = false from the sendMessage().I changed version to {version: '1.0'} and my manifest file contains "background": { "scripts": ["background-script.js"], "persistent": true }, "content_scripts": [ { "js": [ "content-script.js" ], "all_frames": true, "run_at": "document_end", "matches": ["http://localhost:4200/*","https://localhost:4200/*"] }], "permissions": [ "management" ] Plz help me – Nani Aug 01 '18 at 11:25
  • @Nani Its probably best you make a new question. – BJury Aug 01 '18 at 11:33
  • I am facing with error `Uncaught TypeError: Cannot read properties of undefined (reading 'sendMessage')` – huykon225 Nov 15 '21 at 09:08
53

I am sure there is a direct way (calling functions on your extension directly, or by using the JS classes for extensions), but an indirect method (until something better comes along):

Have your Chrome extension look for a specific DIV or other element on your page, with a very specific ID.

For example:

<div id="ExtensionCheck_JamesEggersAwesomeExtension"></div>

Do a getElementById and set the innerHTML to the version number of your extension or something. You can then read the contents of that client-side.

Again though, you should use a direct method if there is one available.


EDIT: Direct method found!!

Use the connection methods found here: https://developer.chrome.com/extensions/extension#global-events

Untested, but you should be able to do...

var myPort=chrome.extension.connect('yourextensionid_qwerqweroijwefoijwef', some_object_to_send_on_connect);
Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
  • 5
    hmmm chrome.extension.connect only seems to work when executed from within the extension (or any extension). I need it to work from any random js script. Any ideas? –  Jun 09 '11 at 13:55
  • Strange, the documentation says it should work. "Unlike the other chrome.* APIs, parts of chrome.extension can be used by content scripts" and it lists `sendRequest()`, `onRequest`, `connect()`, `onRequest`, and `getURL()`. – Brad Jun 09 '11 at 14:01
  • @James are you executing the script that uses .connect() from a hosted script? I know Chrome tries hard not to do stuff with just local files that aren't hosted for security purposes. - Just checking. – JamesEggers Jun 09 '11 at 14:13
  • @James the script I'm executing .connect() from is on the same server if that's what you mean? –  Jun 09 '11 at 14:16
  • I've done some research into chrome.extension, it won't work unfortunately. However, this seems promising: http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication –  Jun 09 '11 at 15:11
  • Heh, that's the method I originally posted. Just out of curiousity, why won't the chrome.extension stuff work? Is this documented somewhere? Am I interpreting what trey mean by a "content page" incorrectly? – Brad Jun 09 '11 at 16:10
  • Ye basically the content page has to be part of the extension, so chrome.extension.connect cannot be executed from a js script that is not part of the extension :-/ Oh ye sorry I forgot you posted that :P –  Jun 09 '11 at 17:09
  • Aha, that makes sense! Sorry you're stuck with such a hackish solution. – Brad Jun 09 '11 at 18:34
  • @JamesEggers Any chance you could post the entire code for this on pastie.org or something like that? I am trying to achieve the same but couldn't yet. – user979390 Jan 19 '12 at 18:05
  • 23
    **The last method is no longer valid**, as `connect` function was moved to `chrome.runtime` namespace. See BJury's answer (and comments) for a more up-to-date version – Xan Jan 13 '15 at 19:40
  • You could add this link to add more weight to your answer : https://developer.chrome.com/webstore/inline_installation#already-installed Also, the advantage of this method is that you do not need to know the extension's id, which is handy because you do not need any config for dev/production. – Dabrule Jun 18 '20 at 11:58
  • is there a compatible method for users not using chrome? – Mathieu J. Feb 04 '21 at 09:13
25

Another method is to expose a web-accessible resource, though this will allow any website to test if your extension is installed.

Suppose your extension's ID is aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, and you add a file (say, a transparent pixel image) as test.png in your extension's files.

Then, you expose this file to the web pages with web_accessible_resources manifest key:

  "web_accessible_resources": [
    "test.png"
  ],

In your web page, you can try to load this file by its full URL (in an <img> tag, via XHR, or in any other way):

chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/test.png

If the file loads, then the extension is installed. If there's an error while loading this file, then the extension is not installed.

// Code from https://groups.google.com/a/chromium.org/d/msg/chromium-extensions/8ArcsWMBaM4/2GKwVOZm1qMJ
function detectExtension(extensionId, callback) { 
  var img; 
  img = new Image(); 
  img.src = "chrome-extension://" + extensionId + "/test.png"; 
  img.onload = function() { 
    callback(true); 
  }; 
  img.onerror = function() { 
    callback(false); 
  };
}

Of note: if there is an error while loading this file, said network stack error will appear in the console with no possibility to silence it. When Chromecast used this method, it caused quite a bit of controversy because of this; with the eventual very ugly solution of simply blacklisting very specific errors from Dev Tools altogether by the Chrome team.


Important note: this method will not work in Firefox WebExtensions. Web-accessible resources inherently expose the extension to fingerprinting, since the URL is predictable by knowing the ID. Firefox decided to close that hole by assigning an instance-specific random URL to web accessible resources:

The files will then be available using a URL like:

moz-extension://<random-UUID>/<path/to/resource>

This UUID is randomly generated for every browser instance and is not your extension's ID. This prevents websites from fingerprinting the extensions a user has installed.

However, while the extension can use runtime.getURL() to obtain this address, you can't hard-code it in your website.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • Although this answer gets the "juice" from http://stackoverflow.com/a/9216924/1504300, IMHO adds some pretty important informations such as exposing the resource in the extension and the fact that you can use an ajax request for checking for existence (Image object seems available only on HTML5 if I'm not wrong http://goo.gl/HBeI1i). With the info in this answer I've been able to solve the problem, I found it like an "out of the box" solution – reallynice Feb 11 '15 at 15:44
  • @niconic That answer (being bad as link-only anyway) refers to the situation before manifest version 2 came in effect. Previously, one did not need to declare resources web-accessible. – Xan Feb 11 '15 at 15:47
  • the webpage is not allow we access to `chrome-extension://` – huykon225 Nov 16 '21 at 06:51
20

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 + "<BR>"

  divAnnounce.innerHTML = divAnnounce.innerHTML + 'Page needs ' + ExtName + ' Extension -- to intall the LocalLinks extension click <a href="https://chrome.google.com/webstore/detail/locallinks/' + ExtID + '">here</a>';
}

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.

Vishwanath
  • 6,284
  • 4
  • 38
  • 57
Nato
  • 321
  • 2
  • 4
  • 3
    Looks like Google's made things more secure, I get the following error when I run Ext_Detect(): _Denying load of chrome-extension://[my_extension_id]/manifest.json. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension._ – Lounge9 Jan 06 '14 at 20:42
  • I'm able to get this to work with Version 32.0.1700.107 m of Chrome as of 2/27/2014 – J E Carter II Feb 27 '14 at 17:47
  • 2
    as @Lounge9 said. Resources inside of packages using manifest_version 2 (or above) are [blocked by default](https://developer.chrome.com/extensions/manifest/web_accessible_resources#availability), and must be [whitelisted](https://developer.chrome.com/extensions/manifest/web_accessible_resources) for use via this property by adding to manifest.json: "web_accessible_resources": [ "manifest..json" ], – ET-CS Jan 26 '15 at 04:19
  • Using @BJury answer you can also pass data easily from the extension to the script (for example the extension version) and you don't need to expose any file from the extension. – ET-CS Jan 26 '15 at 04:22
  • 1
    This worked for me best because our extension will be used across multiple domains and could not be pre-defined as new domains are added regularly. Instead of accessing manifest.json I created a new file version.json and put the version number inside. This works just the same. – Paul Haggo Apr 28 '15 at 00:11
  • Make sure you are specifying the correct extension id. I was using the id from my published extension, when I was testing this with my development version (which had a different id). – John Langford Mar 21 '17 at 22:59
  • Hint: if you get the "resources must be listed [as] web_accessible_resources" error, open a tab in chrome and visit chrome-extension://whatever-extension-id/manifest.json. This reliably displays the manifest on that tab. Search the text for "web_accessible_resources" and adjust the suggested code's src attribute to use one of the listed resources. – Stan Jan 08 '20 at 19:00
  • this will result in "site not secure" https warnings on iOS Chrome – Tom Aug 17 '23 at 20:43
8

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.

PAEz
  • 8,366
  • 2
  • 34
  • 27
  • 14
    This works great for a Chrome *App*, but not for a Chrome Extension AFAIK – Eran Medan Jan 14 '14 at 23:00
  • Yep that website does tell you how to inline *install* an extension, but apparently recommends "Extensions can communicate with the embedding page via content scripts to let it know that they are already installed." instead of being able to use chrome.app.isInstalled. Confused me too... – rogerdpack Apr 27 '17 at 06:34
  • https://developer.chrome.com/webstore/inline_installation#already-installed – E Ciotti May 14 '18 at 13:19
7

Here is an other modern approach:

const checkExtension = (id, src, callback) => {
    let e = new Image()
    e.src = 'chrome-extension://'+ id +'/'+ src
    e.onload = () => callback(1), e.onerror = () => callback(0)
}

// "src" must be included to "web_accessible_resources" in manifest.json
checkExtension('gighmmpiobklfepjocnamgkkbiglidom', 'icons/icon24.png', (ok) => {
    console.log('AdBlock: %s', ok ? 'installed' : 'not installed')
})
checkExtension('bhlhnicpbhignbdhedgjhgdocnmhomnp', 'images/checkmark-icon.png', (ok) => {
    console.log('ColorZilla: %s', ok ? 'installed' : 'not installed')
})
Kerem
  • 11,377
  • 5
  • 59
  • 58
5

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';
}
Chase Roberts
  • 9,082
  • 13
  • 73
  • 131
4

A lot of the answers here so far are Chrome only or incur an HTTP overhead penalty. The solution that we are using is a little different:

1. Add a new object to the manifest content_scripts list like so:

{
  "matches": ["https://www.yoursite.com/*"],
  "js": [
    "install_notifier.js"
  ],
  "run_at": "document_idle"
}

This will allow the code in install_notifier.js to run on that site (if you didn't already have permissions there).

2. Send a message to every site in the manifest key above.

Add something like this to install_notifier.js (note that this is using a closure to keep the variables from being global, but that's not strictly necessary):

// Dispatch a message to every URL that's in the manifest to say that the extension is
// installed.  This allows webpages to take action based on the presence of the
// extension and its version. This is only allowed for a small whitelist of
// domains defined in the manifest.
(function () {
  let currentVersion = chrome.runtime.getManifest().version;
  window.postMessage({
    sender: "my-extension",
    message_name: "version",
    message: currentVersion
  }, "*");
})();

Your message could say anything, but it's useful to send the version so you know what you're dealing with. Then...

3. On your website, listen for that message.

Add this to your website somewhere:

window.addEventListener("message", function (event) {
  if (event.source == window &&
    event.data.sender &&
    event.data.sender === "my-extension" &&
    event.data.message_name &&
    event.data.message_name === "version") {
    console.log("Got the message");
  }
});

This works in Firefox and Chrome, and doesn't incur HTTP overhead or manipulate the page.

mlissner
  • 17,359
  • 18
  • 106
  • 169
  • Two issues that I see with this solution. First, if I uninstall or disable the extension, this solution provides no way of detecting this. Second, it cannot detect if the extension is not installed, as it only acts on a `message` event. Am I correct in assuming this? – kjmj Dec 07 '22 at 23:43
  • 3
    I think we have different goals. The post above lets you know if an extension is installed in somebody's browser. Not if the "installation action" happened or if the "uninstallation action" happened. Just whether it is currently installed. – mlissner Dec 09 '22 at 00:34
3

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>
Xan
  • 74,770
  • 16
  • 179
  • 206
Dawid Szymański
  • 775
  • 6
  • 15
3

Your extension could interact with the website (e.g. changing variables) and your website could detect this.

But there should be a better way to do this. I wonder how Google is doing it on their extension gallery (already installed applications are marked).

Edit:

The gallery use the chrome.management.get function. Example:

chrome.management.get("mblbciejcodpealifnhfjbdlkedplodp", function(a){console.log(a);});

But you can only access the method from pages with the right permissions.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Fox32
  • 13,126
  • 9
  • 50
  • 71
  • 1
    ye that would require the extension to interact with every site on every tab which would be slow/difficult to implement and buggy :-/ –  Jun 09 '11 at 13:40
  • The problem is that communication in the other direction (page to extension) is not possible, because of the security model of chrome. If you don't want to go the 'interacting' way, take the cookie way. – Fox32 Jun 09 '11 at 14:14
  • 4
    Dear @Fox32 , chrome.management.get... , returns this error: `Uncaught TypeError: Cannot read property 'get' of undefined` – Hosein Aqajani Dec 19 '15 at 11:09
3

You could have the extension set a cookie and have your websites JavaScript check if that cookie is present and update accordingly. This and probably most other methods mentioned here could of course be cirvumvented by the user, unless you try and have the extension create custom cookies depending on timestamps etc, and have your application analyze them server side to see if it really is a user with the extension or someone pretending to have it by modifying his cookies.

Niklas
  • 29,752
  • 5
  • 50
  • 71
3

There's another method shown at this Google Groups post. In short, you could try detecting whether the extension icon loads successfully. This may be helpful if the extension you're checking for isn't your own.

Beau
  • 11,267
  • 8
  • 44
  • 37
1

You could also use a cross-browser method what I have used. Uses the concept of adding a div.

in your content script (whenever the script loads, it should do this)

if ((window.location.href).includes('*myurl/urlregex*')) {
        $('html').addClass('ifextension');
        }

in your website you assert something like,

if (!($('html').hasClass('ifextension')){}

And throw appropriate message.

Prakash Palnati
  • 3,231
  • 22
  • 35
0

If you have control over the Chrome extension, you can try what I did:

// Inside Chrome extension
var div = document.createElement('div');
div.setAttribute('id', 'myapp-extension-installed-div');
document.getElementsByTagName('body')[0].appendChild(div);

And then:

// On web page that needs to detect extension
if ($('#myapp-extension-installed-div').length) {

}

It feels a little hacky, but I couldn't get the other methods to work, and I worry about Chrome changing its API here. It's doubtful this method will stop working any time soon.

jds
  • 7,910
  • 11
  • 63
  • 101
0

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)
}
nab.
  • 165
  • 2
  • 10
0

Here is how you can detect a specific Extension installed and show a warning message.

First you need to open the manifest file of the extension by going to chrome-extension://extension_id_here_hkdppipefbchgpohn/manifest.json and look for any file name within "web_accessible_resources" section.

<div class="chromewarning" style="display:none">
    <script type="text/javascript">
                            $.get("chrome-extension://extension_id_here_hkdppipefbchgpohn/filename_found_in_ web_accessible_resources.png").done(function () {
                              $(".chromewarning").show();
                            }).fail(function () {
                             //  alert("failed.");
                            });
                        </script>
                        <p>We have detected a browser extension that conflicts with learning modules in this course.</p>
            </div>
Rem
  • 81
  • 1
  • 3
0

Chrome Extension Manifest v3:

const isFirefox = chrome.runtime.OnInstalledReason.CHROME_UPDATE != "chrome_update";

For FireFox, I believe chrome.runtime.OnInstalledReason.BROWSER_UPDATE will be "browser_update": https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/OnInstalledReason

AEQ
  • 1,339
  • 1
  • 16
  • 20
0

I wrote API which will count the installations for chrome & firefox. Called this API from background.js or extension. Register background.js in manifest.json for both chrome & firefox.

I'm passing browser name while making API call.

Chrome

function handleInstalled(details) {
    if (details.reason == 'install') {
        fetch('https://YOUR_API_URL/api/browser-extension', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ browser: 'chrome' }),
        })
            .then((response) => response.json())
            .then((response) => console.log(response))
            .catch((error) => console.error(error))
    }
}

chrome.runtime.onInstalled.addListener(handleInstalled)

Firefox

function handleInstalled(details) {
    if (details.reason == 'install') {
        fetch('https://dev.axisscambush.com/api/browser-extension', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ browser: 'firefox' }),
        })
            .then((response) => response.json())
            .then((response) => console.log(response))
            .catch((error) => console.error(error))
    }
}

browser.runtime.onInstalled.addListener(handleInstalled)
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
0

Modern way to check if extension is there and also can be used with Async/Await:

$.sendMessage = (appId, request) => {
    return new Promise(resolve => {
    if (chrome.runtime)     // undefined if no extension allowed to message from this domain
        chrome.runtime.sendMessage(appId, request, response => {
            if (chrome.runtime.lastError) resolve()  // handle error because our extension's background script not there to answer
            resolve(response);  // extension background script answered
        });
    else
        resolve(); // extension not found
});

}

Perry
  • 109
  • 10