Error: EventPages (Service Workers?) + WebRequest API

1,176 views
Skip to first unread message

Aditya B

unread,
Nov 4, 2019, 11:53:30 AM11/4/19
to Chromium Extensions
Specifying WebRequest API in manifest permissions when using Event pages ("persistent": false) raises the error:

WebRequest API cannot be used with event pages.

 The alternative, declarativeWebRequest, appears to be in limbo as well

Note: this API is currently on hold, without concrete plans to move to stable.

What's the solution here for an extension to use WebRequest API from the Event page? 

Given the upcoming move to Service Workers, I was planning to update my extension to Event Pages as an intermediate step. However, with neither version of the WebRequest API reliably available, how is this supposed to work?

Aditya B

unread,
Nov 4, 2019, 12:06:23 PM11/4/19
to Chromium Extensions
A clarification: My extension doesn't use WebRequest to modify requests. It just uses it to observe requests. Specifically, I use `chrome.webRequest.onBeforeRedirect` to keep track of when automatic redirects happen. So declarativeNetRequest doesn't seem to be of much use for me.

Anton Bershanskiy

unread,
Nov 4, 2019, 7:38:32 PM11/4/19
to Chromium Extensions
WebRequest API is compatible only with persistent pages, so you should continue using WebRequest API but also you need to make the script that uses this API persistent. This is explicitly explained in the docs here:

wOxxOm

unread,
Nov 5, 2019, 6:14:34 AM11/5/19
to Chromium Extensions
You can use webRequest with event pages, but it's not straightforward:
  1. declare "webRequest" inside "optional_permissions" in manifest.json, not inside "permissions"
  2. if chrome.webRequest is undefined show a button in your extension's UI to grant webRequest permission with an explanation why it's needed
  3. in the button's "click" listener use chrome.permissions.request (docs) to obtain "webRequest" permission
  4. from this moment on your extension can use chrome.webRequest forever
The only problem left is that event pages won't resume for webRequest listeners so you need to prevent the event page from unloading for the duration of using webRequest. This is almost trivial: open a messaging port from an iframe to the background page listener, and once you're done with using webRequest, remove the iframe so the port will close and the event page will be able to unload. Example and more info: https://stackoverflow.com/a/58577052

In case your extension doesn't have a UI, you may be lucky if your extension's functionality can be activated via chrome.contextMenus.onClicked or chrome.browserAction.onClicked or chrome.pageAction.onClicked or chrome.commands.onCommand because you can call chrome.permissions.request in such a listener. Otherwise, well duh... you'll have to add a UI just to obtain the permission.
Reply all
Reply to author
Forward
0 new messages