Now that Google Chrome extensions can register Service Workers, how can I use them in order to modify HTTP responses from all hosts, e.g. by replacing all occurrences of cat
with dog
?
Below is a sample code from Craig Russell, but how to use it inside Chrome extensions and bind it to all hosts?
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request).then(function(response) {
var init = {
status: response.status,
statusText: response.statusText,
headers: {'X-Foo': 'My Custom Header'}
};
response.headers.forEach(function(v,k) {
init.headers[k] = v;
});
return response.text().then(function(body) {
return new Response(body.replace(/cat/g, 'dog'), init);
});
})
);
});
A service worker is a script that your browser runs in the background, separate from a web page, opening the door to features that don't need a web page or user interaction.
Service workers are specialized JavaScript assets that act as proxies between web browsers and web servers. They aim to improve reliability by providing offline access, as well as boost page performance.
Solution
≪manifest.json≫:
{"manifest_version":2,"name":"","version":"0","background":{"scripts":["asd"]}}
≪asd≫:
navigator.serviceWorker.register('sdf.js').then(x=>console.log('done', x))
≪sdf.js≫:
addEventListener('fetch', e=> e.respondWith(fetch/*btw xhr is undefined*/(e.request).then(r=>{
if(r.headers === undefined /*request not end with <.htm>, <.html>*/){}else{
console.assert(r.headers.get('content-type')===null)/*for some odd reason this is empty*///[
let h = new Headers()
r.headers.forEach((v,k)=>h.append(k,v))
Object.defineProperty(r,'headers',{'writable':true})
r.headers = h
r.headers.append('content-type','text/html'/*btw <htm> doesnt work*/)
//]
}
return r.text().then(_=>new Response(_.replace(/cat/g,'dog'),r))
})))
Go to ≪page url≫ (≪chrome-extension://≪ext id≫/≪page path≫≫) and see the replacements.
≪manifest.json≫ and ≪asd≫ same as above.
≪sdf.js≫:
addEventListener('fetch', e=> e.respondWith(new Response('url: '+e.request.url,{headers:{'content-type':'text/html'/*, etc*/}})))
Serviceworker has other events that can be delved into eg:
addEventListener('message', e=>{
console.log('onmessage', e)
})
addEventListener('activate', e=>{
console.log('onactivate', e)
})
addEventListener('install', e=>{
console.log('oninstall', e)
})
Note that currently serviceworker [–cf webNavigation, webRequest] is the only way to do this because some lamer in Google development team has decided that its "insecure" for response-modifying webNavigation and webRequest.
Chrome bugs:
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