I have the following code:
chrome.webRequest.onBeforeSendHeaders.addListener(function (data){
for (let header of data.requestHeaders) {
if (header.name === "User-Agent") {
chrome.storage.sync.get({pua: "Non-Set"}, function (result) {
data.requestHeaders[data.requestHeaders.indexOf(header)].value = result.pua;
})
}
}
return {requestHeaders:data.requestHeaders}
}, {urls: ["<all_urls>"]}, ["requestHeaders", "blocking", "extraHeaders"])
It is supposed to get the option value the user sets, and return that as the user-agent header, but it does not work at all! I used netcat, and my user-agent is normal!
However, if I hardcode the header value, it works! I am getting really annoyed.
I tried using new Promise, and awaiting it but that still didn't work! How can I do this?
Since chrome.storage.sync.get() is asynchronous, there is no way to assure the callback of addEventListner return after the callback of chrome.storage.sync.get() is executed.
The alternative is that the request is always cancelled by the callback of addEventListner, and send modified request in the callback of chrome.storage.sync.get().