This might fall under the category of "don't do that", but I'm attempting to develop a chrome extension that effectively injects a web-accessible service worker script ("sw.js") into a user's page, and then registers a service worker to that script.
Right now, I have the following: Manifest.json:
{
"manifest_version": 2,
"name": "MyApp",
"description": "none",
"version": "1.0",
"incognito": "split",
"web_accessible_resources": ["sw.js"],
"permissions": [
"webRequestBlocking","webRequest",
"*://*/*"
],
"background":
{
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["initPage.js"],
"all_frames": false
}]
}
background.js
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {redirectUrl: chrome.extension.getURL("sw.js")};
},
{urls: ["*://*/switchme.js"]},
["blocking"]);
initPage.js:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/switchme.js').then(function(registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
console.log('ServiceWorker registration failed: ', err);
})
}
Hopefully from this, you'll see what I'm trying to accomplish. Basically, I want to attempt to register a service worker to sw.js
, which is hosted in the web-accessible-scripts in the extension. The way I believe I can do this is by intercepting the request for switchme.js
and redirecting it to return sw.js
.
The only problem with this approach is that my chrome browser crashes when I attempt this. I traced the stack into the line of extensions::sendRequest
:
nativeFunction(functionName,
request.args,
requestId,
hasCallback,
optArgs.forIOThread,
optArgs.preserveNullInObjects);
Any debugging ideas or slaps upside the head with a nice explanation of why I'm crazy to think this might work are appreciated.
The browser crashing seems to be related to this reported bug on the chromium issue tracker.
However, note that this change in the chromium source, suggests the problem is no longer in registering service workers from an extension's URL.
From the commit message:
As a footnote, service workers in chrome extensions are still a work-in-progress. You can follow its progress by tracking this issue.