2

In content_script, I have used chrome's sendMessage api to exchange data with background script. But using sendMessage api the script outputs unexpected results.

As I start typing the url, in a new-tab, (url is not yet loaded) the sendMessage api fires to the background script (I checked it by placing debugger at background script).

I tried following cases in my content.js file to understand the problem:

Case1: Here, as url is typed (not loaded) this api is fired. (unexpected)

chrome.runtime.sendMessage({source:'registerTab', data:''}, function(){
console.log("response_after_background_page")
});

Case2: Here, it wait for the url to load and then there is an alert message. After that the sendMessage api fires. (this result is expected in case1)

alert();

chrome.runtime.sendMessage({source:'registerTab', data:''}, function(){
    console.log("response_after_background_page")
});

Case2: This outputs again as case1 and api is fired even before the url is loaded. (unexpected result as in case1)

console.log("SOme_message");

chrome.runtime.sendMessage({source:'registerTab', data:''}, function(){
    console.log("response_after_background_page")
});

I have tried excluding new-tab url in manifest.json file:

  "content_scripts": [
    {
      "matches": ["*://*.google.co.in/*", "*://*.somehost.com/*"],
      "exclude_matches": ["*://*/_/chrome/newtab*"],
      "js": ["libs/jquery-1.12.3.min.js", "libs/underscore-min.js", "content.bundle.js"],
      "run_at": "document_end"
    }
  ],

I expect my url to load and then only content.js should be injected. And then only the sendMessage api should fire. Where am I getting wrong?

Edit:

I am building a extension which tracks webpage whose url matches as rule specified in manifest.json file. And when the matched url is loaded the content script should fire a sendMessage call to background page to keep record of this created page. But as sendMessage api is called before the url is loaded some junk url info is sent to background page. I want all parts of content_script to load only after the url is fully loaded not when it is being typed.

Harsh K
  • 79
  • 1
  • 7
  • 1
    A hidden tab is being prefetched and prerendered by Chrome while you type; you can find answers about this on StackOverflow. See also the official documentation about the corresponding chrome.tabs.onReplaced event. – wOxxOm Jan 06 '17 at 15:23
  • @wOxxOm, http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script#9517879 refering this link I tried to enject chrome api into content-script. The prerendering while typing url ceased but it was not able to listen onMessage event at background.js. Should I try restricting prerendering at content script? Or something else? – Harsh K Jan 06 '17 at 20:02
  • Prerendering is a global browser option, you can't restrict it locally. Why do you send that message? You'll need to rework the logic. If you need help with that, add the relevant details to your question. – wOxxOm Jan 07 '17 at 04:40
  • @wOxxOm I have added required details to my question. Do I need to rework on the logic? – Harsh K Jan 07 '17 at 09:39
  • It's not clear why you track in the content script. Event page (or background page) script can do it via chrome.tabs.onUpdated listener or chrome.webNavigation.onCommited (or onCompleted) - see the documentation. In this case all you need to do is to check `tabId > 0` since prerendered tabs have `-1`. – wOxxOm Jan 07 '17 at 11:12
  • Thanks, it was a nice suggestion to use chrome.tabs.onUpdated api to track different tabs. I remodelled the logic accordingly and it's working fine. I will research later, if there exists any method to restrict asynchronous api calls (like chrome.runtime.sendMessage here) due to prerendering of tabs in chrome. – Harsh K Jan 08 '17 at 18:37

0 Answers0