Reputation: 5995
background.js :
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse)
{
console.log(request.filter)
chrome.webRequest.onBeforeRequest.addListener(request.func, request.filter, ["blocking"]);
}
);
content script:
chrome.extension.sendRequest(
{
func: requestInterceptor,
filter: requestFilter
}
);
The line chrome.webRequest.onBeforeRequest.addListener(request.func, request.filter, ["blocking"]);
is triggering the following error:
Error in event handler for 'undefined': Error: Parameter 1 is required.
How can I fix it?
Upvotes: 3
Views: 2986
Reputation: 348992
On message passing, requests are JSON-serialized.
Functions are not serializable, so they're not sent.
chrome.extension.sendRequest( {
func: requestInterceptor, // Function
filter: requestFilter // Array of strings
});
is received as
{
filter: requestFilter
}
Move the function logic to the background page.
If you want to add a webRequest
event listener from the content script, pass all necessary (JSON-serializable) data to the background page using chrome.extension.sendRequest
. The receiver (background), also gets a MessageSender
type, which can be used to read the tab
of the origin (which supplies all necessary information, including the tabId).
Upvotes: 1