Question

I am developing a Google Chrome Extension which has a content-script, a background page, a settings page and a popup page. I learnt the hard way that though the content-script loads on the Chrome Web Store, the onMessage listeners are never invoked.

I can consistently break at the point where I am executing chrome.extension.onMessage.addListener, but the listener is never invoked. Therefore when I execute chrome.tabs.sendMessage from the background or popup pages, the responseCallback function never gets invoked - i.e. the logic inside the responseCallback never executes. Chrome doesn't even treat this like an error scenario - it accepts the request, but goes silent thereafter.

If it had treated this as an error case (as with attempting to message a chrome:// page) then the responseCallback would've been invoked with a undefined response argument - and that would cause my error-handling code to execute... but no such luck.

I could add special behavior in my code for http(s)://chrome.google.com/webstore/*, but was hoping that there was a more official way of finding pages that were barred from execution. Would appreciate any pointers there.

I found a great post here, but it doesn't have a programmatic API, hence this question.

I am already aware that the Chrome webstore and chrome:// URLs are special cases. However, per my investigation, the behavior on chrome:// URLs is different than on the Chrome Web Store. On chrome:// URLs, my content-script appears to not load at all and the sendMessage's responseCallback function gets invoked with a null/undefined response object. That is the API's official way of signalling an error and it works fine for me.

My issue is with being able to programmatically detect the errors like those experienced with the Chrome Web Store without having to hardcode the URLs (the URLs could change with time).

Thanks in advance for your responses.

Was it helpful?

Solution

How about adding a listener for chrome.webNavigation.onCompleted in the background script? When a new page is loaded, send a special "ping" message to the tab, and use chrome.extension.onMessage to receive and respond to the ping in your content script. If the content script doesn't respond then you know that it isn't running on that page.

OTHER TIPS

You could try running chrome.tabs.executeScript to see if you're allowed scripting in that domain.

chrome.tabs.executeScript(tabId, {code: ""}, function() {
  if(chrome.runtime.lastError) {
    // Something went wrong, perhaps no rights
  } else {
    // Scripting allowed
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top