I am currently working on a chrome extension and source code is available on Github. The goal is to inject custom Javascript into webpages.
Currently I store each custom Javascript Inject in localStorage and call them from content scipts. I have set run_at to be at document_start.
I use this, to get stored injects from background script:
chrome.extension.sendMessage({method:"get_injects"},function(injects){
for(index in injects){
if(/^items\./.test(index)){
itemJSON = injects[index];
//if(window.location.host.toString().indexOf(itemJSON.url)){
if(window.location.host.toString().match(itemJSON.url + '$')){
var js = itemJSON.js.toString();
eval(js);
}
}
}
});
I want to run Inject scripts exactly before document loads. But using presented method, control will pass chrome.extension.sendMessage
and wont wait to get responseinjects
. So, eval(js);
will be executed at the middle of page loading. How can I solve this, so that I will be able to inject before page loading?
In Chrome, each tab runs in its own process. The background page runs in the extension process which is different from the one of the tab you're hooking. So I'm afraid there can be no really synchronous solution.
Even using chrome.storage (which hasn't the origin limitation and thus doesn't need the background page) would be asynchronous.
But here's a solution that would work for most pages :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With