Add support for returning a Promise from webRequest.onBeforeSendHeaders listener
Reported by
imp...@gmail.com,
Jan 26 2018
|
||||
Issue description
UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Steps to reproduce the problem:
1. Create an extension with this code in the background script
function promiseReturningFunction() {
return new Promise((resolved) => {
setTimeout(() => {
resolved('Boum');
}, 300);
});
}
function createListener() {
return async function (request) {
const result = await promiseReturningFunction();
const headers = [{ name: 'X-SPECIAL-HEADER', value: result }];
return { requestHeaders: request.requestHeaders.concat(headers) };
};
}
chrome.webRequest.onBeforeSendHeaders.addListener(createListener(), { urls: ['<all_urls>'] }, ['requestHeaders', 'blocking']);
What is the expected behavior?
All requests should now have a HTTP header
X-SPECIAL-HEADER: Boum
What went wrong?
The header is not present.
If I change the code to
function createListener() {
return function (request) {
const result = 'Boum';
it works as expected.
Did this work before? N/A
Does this work in other browsers? Yes
Chrome version: 63.0.3239.132 Channel: stable
OS Version: OS X 10.12.6
Flash Version:
It seems related to async functions.
FYI it works on latest Firefox
,
Jan 26 2018
,
Jan 29 2018
Hello, Thanks for your quick response and triage ! Actually, since the part of the spec allowing to return a Promise resolving to a BlockingResponse does not seem to be implemented as well (see 4th paragraph here https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onBeforeSendHeaders), I don't see how to implement my use case in a Chromium extension Indeed, this piece of code does not work as expected whereas it works as expected in Firefox : function promiseReturningFunction() { return new Promise((resolved) => { setTimeout(() => { resolved('Boum Paf Paf'); }, 300); }); } function createListener() { return function (request) { return promiseReturningFunction().then((result) => { const headers = [{ name: 'X-SPECIAL-HEADER', value: result }]; return { requestHeaders: request.requestHeaders.concat(headers) }; }); }; } chrome.webRequest.onBeforeSendHeaders.addListener(createListener(), { urls: ['<all_urls>'] }, ['requestHeaders', 'blocking']); If you think I forgot something, misuse or did something wrong, let me know. If you have any pointer, comments or advice, I'd love to hear them. Thanks again,
,
Jan 29 2018
(FYI I've updated the async/await implementation of my opening ticket description in favor of a Promise resolving to a BlockingResponse implementation in my latest comment)
,
Jan 29 2018
Looking at MDN for Chrome extensions can be misleading, because Firefox implements the Chrome extension API with some deviations and missing features. I think you caught one of the deviations here. https://developer.chrome.com/extensions/webRequest#event-onBeforeSendHeaders does not mention any support for promises. Sorry.
,
Jan 29 2018
Then it is a feature request
,
Jan 29 2018
I doubt no one has filed such a request already, but here you go.
,
Feb 9 2018
Currently AFAIK all extension APIs use callbacks. Merging with crbug.com/328932. |
||||
►
Sign in to add a comment |
||||
Comment 1 by phistuck@chromium.org
, Jan 26 2018