Chrome extension: Serviceworker cross origin faili

2019-09-19 15:47发布

问题:

Docs claim that:

By adding hosts or host match patterns (or both) to the permissions section of the manifest file, the extension can request access to remote servers outside of its origin

My ≪manifest.json≫:

{"manifest_version":2,"name":"","version":"0","permissions":["http://example.org/"],"background":{"scripts":["asd"]}}

≪asd≫:

navigator.serviceWorker.register('sdf.js',{scope:'http://example.org/'}).then(x=>console.log('done', x))

≪sdf.js≫:

addEventListener('fetch', e=> e.respondWith(new Response('url: '+e.request.url,{headers:{'content-type':'text/html'/*, etc*/}})))

I loaded the code using ≪Load unpacked extension≫; It crashed without any error message:

Apparently the code "permissions":["http://example.org/"] crashed the extension.

How to set permissions for Chrome Extension cross-origin serviceworker? Is there a required Chrome flag?

回答1:

As various folks have pointed out in the comments, it's not possible for a service worker to control a Chrome Extension, and it's not possible to register a service worker scoped to an https: origin within the context of a Chrome Extension.