BrowserExtension webRequest.onBeforeRequest return 承诺

BrowserExtension webRequest.onBeforeRequest return promise

我在 Chrome 和 FireFox 扩展中有以下内容:

function webListener(requestDetails) {
    var asyncCancel = new Promise((resolve, reject) => {
        resolve({ cancel : true });
    });
    return asyncCancel;
}

chrome.webRequest.onBeforeRequest.addListener(
    webListener, {
        urls: ["<all_urls>"],
        types: ["script"]
    }, ["blocking", "requestBody"]
);

问题是它没有取消请求。我阅读了 Chrome 文档和 Firefox 文档,对于 Firefox,它说它基于 Chrome 的 API 并且可以 return 承诺异步处理请求..

但是,除非我让它同步,否则它无法取消(即:如果我 return 只是 {cancel : true} 而不是承诺,它会起作用)。

是我做错了什么还是 Chrome Firefox 只支持这里的同步请求处理?

Chrome documentation 对我来说好像你 必须 使用同步、阻塞响应来取消请求,这也是我的经验。

强调我的:

If the optional opt_extraInfoSpec array contains the string 'blocking' (only allowed for specific events), the callback function is handled synchronously. That means that the request is blocked until the callback function returns. In this case, the callback can return a webRequest.BlockingResponse that determines the further life cycle of the request. Depending on the context, this response allows cancelling or redirecting a request (onBeforeRequest), cancelling a request or modifying headers (onBeforeSendHeaders, onHeadersReceived), and cancelling a request or providing authentication credentials (onAuthRequired).

我的理解是,如果您的事件处理程序是同步的,您只能影响请求的生命周期(比如取消它)。

此外,文档也没有提及直接支持 Promise 而不是 BlockingResponse,因此这似乎是 Firefox 特定的功能。