Jack M
Jack M

Reputation: 5995

"Error: Parameter 1 required" when attempting to set a listener on onBeforeRequest

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

Answers (1)

Rob W
Rob W

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

Related Questions