admin管理员组文章数量:1323023
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);
}
}
}
});
Problem
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?
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);
}
}
}
});
Problem
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?
2 Answers
Reset to default 4In 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 the data you need is in the localStorage of the page, go to 6
- remove everything from the page
- do your asynchronous request for data
- when you have the data, store it in the localStorage of the page (not the background one)
- refresh the page with same URL
- profit
There is a way to get an arbitrary amount of data synchronously from the background page to the current content script. You should avoid synchronously blocking code in your extension, so I will not provide sample code, to discourage copy-pasting this in production code.
The flow is:
- Use synchronous
XMLHttpRequest
in the content script (to a fake URL) to initiate these steps. - In the background page, use the
chrome.webRequest.onBeforeRequest
event to intercept this request. - Synchronously get the data, e.g. from
localStorage
or a local variable on the background page, convert it to a blob then convert it to an URL usingvar url = URL.createObjectURL(new Blob([ 'data chunks here' ]));
. - Use
return {redirectUrl: url};
in theonBeforeRequest
event to send the data back. - Make sure that you eventually use
URL.revokeObjectURL(url);
to free the resources used by the data.
And fnally, avoid synchronous, blocking code if possible!!
本文标签: javascriptSynchronously get Stored data from content scriptsStack Overflow
版权声明:本文标题:javascript - Synchronously get Stored data from content scripts - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742113036a2421338.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论