2

Something strange happens in the Chrome extension.

Content script:

console.log('content');

chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
    console.log('request received');
    sendResponse();
});

chrome.extension.sendRequest( JSON.stringify({'msg': 'page_loaded'}) );

It is just listens for extension messaging and sends the message to the background when page loaded.

Background script:

console.log('bg');

chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
    sendResponse();
    chrome.tabs.sendRequest(
        sender.tab.id,
        JSON.stringify({'msg': 'page_loaded_bg_receive'}),
        function(){
            console.log('sendRequest page_loaded_bg_receive callback');
        });
});

It is listens for messages and sends message to the sender tab.

And it seems to be working, at least in most cases in the page log appears 'request received'.

While url entering Chrome sometimes loads typed address before user hits 'enter'. And that's a strange behavior: page loading, content-script runs, sends the message to the background, but when the background sends the message back — it fails with the background log message:

Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:184 chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:184

Is this a Chrome bug? What to do to send message to the preloading tab?

This is the arfificial minimal sample to reproduce such behavior. I need to call 'chrome.tabs.sendRequest' after the handling the message and more than once, so calling the 'sendResponse' is not the solution.

5
  • When this strange behavior appers, tab has 'index' property equals '-1'. And callback is calling, so practically I can call 'chrome.tabs.sendRequest' until success. But it is seems weird. Commented Jul 2, 2012 at 13:06
  • So, whats the solution? I have been trying to do this since yesterday and keep getting the error. I also get the this error when I use sendRequest in the popup. Commented Jul 2, 2012 at 13:32
  • Do you have any conflicting extensions? PS. You don't have to use JSON.stringify and JSON.parse, Chrome automatically (de)serializes requests over the channel. Commented Jul 2, 2012 at 13:59
  • There's no other extensions enabled. Commented Jul 2, 2012 at 14:27
  • I try to listen for 'chrome.tabs.onUpdated' or 'chrome.tabs.onMoved' for waiting when the tabs 'index' property will be >=0, but none of it sends when page shown. Commented Jul 2, 2012 at 14:40

1 Answer 1

3

Solution based on the article https://developers.google.com/chrome/whitepapers/pagevisibility. I run content-script code if document.webkitVisibilityState is not 'hidden' or 'prerender', elsewhere I listen for 'webkitvisibilitychange' and wait for document.webkitVisibilityState is not 'hidden' or 'prerender'. I think check for 'prerender' is enough, but when I open a new empty tab it loads page with document.webkitVisibilityState='hidden' and this page also did not receive background messages.

function isDocumentReady() {
  return document.webkitVisibilityState != "hidden" && document.webkitVisibilityState != "prerender";
}

if (isDocumentReady())
  main();
else {

  function onVisibilityChange() {
    if (!isDocumentReady())
      return;
    document.removeEventListener(
      "webkitvisibilitychange",
      onVisibilityChange,
      false);
    main();
  }

  document.addEventListener(
    "webkitvisibilitychange",
    onVisibilityChange,
    false);

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.