I am trying to do block items on a webpage but I want to do that, before they are loaded. So, e.g., I could use
chrome.webRequest.onBeforeRequest.addListener(...);
And redirect/cancel the request. But i want to inspect the actual content of the request. What I am doing right now, is starting a XMLHttpRequest
to load the url/object myself, inspect the content, and block it if necessary.
However, the main problem is that in fact, not many objects are blocked. This means, that each object is loaded twice: Once for "my inspection" and once, after I said "okay, you may load it".
How can I intercept the loading process, so that I can inspect it on the fly and pass on the data bytes if they are allowed?
Hope you understand my question, thanks :-)
Example of how I do it right now:
function shall_be_blocked(info){
var xhr = new XMLHttpRequest();
xhr.open("GET", file, false);
//... #load the file and inspect the bytes
if (xhr.responseText=="block it") {
return true;
}
return false;
}
chrome.webRequest.onBeforeRequest.addListener(
function(info) {
ret = shall_be_blocked(info);
if (ret ...){return {cancel:true};}//loads the file once, as it is getting blocked
return {};//loads the file twice
},
{},["blocking"]
);
ServiceWorker
? – Pushcart