Reputation: 97
Here is the full error that is being logged when my extension runs on a page that it is supposed to change headers on:
Error in event handler: TypeError: Error in invocation of webRequestInternal.eventHandled(string eventName, string subEventName, string requestId, integer webViewInstanceId, optional webRequest.BlockingResponse response): Error at parameter 'response': Error at property 'requestHeaders': Error at index 1: Error at property 'value': Invalid type: expected string, found object.
at subEventCallback (extensions::webRequestEvent:82:28)
It's being logged once for every time the handler gets called, and as you can imagine the request does not get successfully modified.
Also, this exact same code was working before, but as I modified the rest of the extension it stopped working. So, I have to assume this is some stupid small mistake somewhere, but even as I reverted changes the problem didn't go away. Unfortunately, I didn't use version control, because I'm stupid, but I've learned my lesson.
And here are the code and relevant files. The issue is with the handler in ua-req.js. I've replaced some details with descriptions of what data is there, and those are in angle braces.
manifest.json
{
"manifest_version": 2,
"name": "<name>",
"version": "<version>",
"background": {
"scripts": [
"constants.js",
"ua-cache.js",
"ua-req.js"
],
"persistent": true
},
"permissions": [
"cookies",
"storage",
"webRequest",
"webRequestBlocking",
"*://*.<URL>.com/*"
]
}
ua-cache.js
// Since getting the user agent from storage is async, it will only get done
// in time and be accessible from our listener if it's fetched and cached
// in a separate script that's loaded before it
// after this, cachedUA will be a string
var cachedUA = FB_UA;
const UA_LIST = "<STORAGE_KEY>";
(function() {
chrome.storage.local.get(UA_LIST, (res) => {
cachedUA = res[UA_LIST] || EDGE_UA; // or in case of a cache miss
});
})();
ua-req.js
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
for (let i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name == "User-Agent") {
details.requestHeaders[i].value = cachedUA;
break;
}
}
return {requestHeaders: details.requestHeaders};
}, {urls: [URL_FILTER]}, ["blocking", "requestHeaders"]);
constants.js
// fallback user agent if pastes cannot be fetched
const FB_UA = "<FALLBACK_USERAGENT>";
// domain of search engine
const ENGINE_TLD = "<URL>.com";
const URL_FILTER = `*://*.${ENGINE_TLD}/*`;
Also, I am running this extension in Vivaldi, and have not tried it in other browsers -- the problem might not be reproducible in normal Chrome. I need it to work in Vivaldi since it's an extension for personal use, and Vivaldi is my daily-use browser.
Thanks in advance.
Upvotes: 1
Views: 604