dev4life
dev4life

Reputation: 11394

chrome.app.isInstalled always returns false for Google Chrome extensions?

why does chrome.app.isInstalled always return false for Google Chrome extensions?

Dynamically I add a link element on page load:

<link type="text/css" rel="chrome-webstore-item" href="https://chrome.google.com/webstore/detail/itemID">

The following is some Javascript that executes when a button has been clicked:

if (!chrome.app.isInstalled) {
 alert('extension is about to be installed!');
 install_extension();
}else{
  alert('extension is installed already.');
}

The first time I clicked the button, Google Chrome asked me if I wanted to install the extension. I agree and the extension was installed correctly. When I refreshed the page, I clicked the button once again and Google CHrome asked me to install the extension once again even when I had installed it 2 minutes ago. In other words, chrome.app.isInstalled always return false even when the extension is installed. Why?

Upvotes: 14

Views: 7465

Answers (3)

MAZ
MAZ

Reputation: 891

Found simple solution, that affects only one page

Check if tab starts with URL of your website, so do some minor DOM change.

chrome.tabs.query({active: true, currentWindow: true}, function (arrayOfTabs) {
    var tab = arrayOfTabs[0];

    if ( tab.url.indexOf('http://yoursite.com') === 0 ){
        chrome.tabs.executeScript({
                code: 'document.body.classList.add("your_сlass");'
        });
    }
});

Upvotes: 1

apsillers
apsillers

Reputation: 115940

See chrome.app.isInstalled Always Returns as "false":

chrome.app.isInstalled is meant for use by hosted apps (which define a set of URLs that encompass the app). Extensions can instead indicate that they're installed already by injecting a DOM node into the page (see second half of https://developers.google.com/chrome/web-store/docs/inline_installation#already-installed).

That link describes a strategy for testing if your extension is installed:

  1. Have a content script inject a DOM node into every page. This node should have a very specific ID, e.g., <div id='my-extension-installed-with-id-sdgdthsdfgdtyjufwknsdkos'>

  2. On button press, have your page test if that node exists.

  3. If the node exists, the content script is running; therefore, the extension is installed. If it does not exist, assume the extension is not installed.

Injecting a DOM node won't affect the state of app.isInstalled. Instead, you check for the existence of the DOM node as proof of the extensions presence.

Upvotes: 15

Xan
Xan

Reputation: 77523

Another solution is to employ externally_connectable.

Since inline install happens from a verified site, you have a set domain on which you want to check that extension exists. Let's say, example.com and its subdomains.

Then, you can define the following in your manifest:

"externally_connectable" : {
  "matches" : [
    "*://*.example.com/*"
  ]
}

This will expose chrome.runtime.sendMessage to the example.com domain.

You can then set up a message listener for onMessageExternal in your extension that will reply to a "ping" from the page.

For more details see this answer.

Upvotes: 6

Related Questions