J
J
jxe32015-12-26 11:51:08
JavaScript
jxe3, 2015-12-26 11:51:08

How to get background response before page load?

manifest

/**/
"run_at": "document_start"
/**/

script.js
console.log('Extension Init');
chrome.extension.sendMessage('init', function(back)
{ console.log('sendMessage request');
  console.log(back)
});

background.js
chrome.extension.onMessage.addListener(function(request, sender, f_callback)
{
     f_callback('back message');
});

1.html (test page)
<html>
    <head>
    </head>
    <body>
      <script>
        window.onload = function(){console.log('ONLOAD');};
      </script>
    </body>
</html>

Log:
Extension Init
ONLOAD 
sendMessage request
back message

Current process:
script.js -> 1.html -> background.js
And we need to get the response before the page is loaded (ie script.js -> background.js -> 1.html ).
Specifically, get the localstorange extension.
(chrome.storage.local.get is also retrieved after page load).
Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
arentyev, 2015-12-31
@aterentyev

I don’t know if it’s possible to redefine the order of calling scripts, but when I myself wrote an extension for chrome using localstorage, I did something like this:
background.js:

function run(){
    chrome.storage.local.get('param', function(data){
      startApp(data);
    }
}

function startApp(){//....}
run();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question