user979390
user979390

Reputation: 309

Pass variable to webpage from chrome extension as soon as possible

I am trying to detect if my chrome extension is installed using js executed on my website. I have found some examples, some I don't even understand or could make them work. What I tried and kinda worked is adding elements to DOM or modifying some css property. For example:

On my extension background page I have:

function changeBG(tab){
     chrome.tabs.executeScript(tab.id, {
      code: ' document.body.bgColor="red" '
     });
}

chrome.tabs.onUpdated.addListener(changeBG);

On my website I have:

<script>
  alert(document.body.bgColor);
</script>

The background does turn red, but .bgColor is null because I try to read it on the top of my website code as soon as the page starts loading. After the page loaded I can read it and it returns "red". (same thing happen if injecting an element into the page and then trying to read innerHTML or a property of the element).

But I don't want to wait for onload or anything, because my content depends on if the extension is installed or not.

I have also tried the method of loading an image from the extension content but once again this is an indirect ¿asynchronous? method.

I don't want to "swap" the content, I want to know if the extension is installed before continuing to load anything else on my website.

Does someone knows how I could do this?

Upvotes: 1

Views: 785

Answers (2)

abraham
abraham

Reputation: 47833

Since it is your own website use the chrome.app.isInstalled method used for inline installation. You will have to verify your domain with Webmaster Tools first.

Upvotes: 0

Konrad Dzwinel
Konrad Dzwinel

Reputation: 37903

As I understand, javascript check is executed too late for you, because webpage content is already processed by a browser. If you want to know if extension is enabled before any content is loaded you should check it on the server side. The way to do this is to make your extension to append some signature to headers sent by browser to servers. Then, if signature is present, you will know that request sender has your extension installed and you will be able to return proper content. Header manipulation is described here. You will probably need to use this: chrome.webRequest.onBeforeSendHeaders.addListener.

Upvotes: 3

Related Questions