WebRequest can't using multi time with a type event.
phanthai12 opened this issue · comments
const {session} = require('electron')
// Modify the user agent for all requests to the following urls.
const filter_mainFrame = {
urls: ['*://example.com/*']
}
const filter_stylesheet = {
urls: ['*://otherpage.com/*']
}
session.defaultSession.webRequest.onBeforeSendHeaders(filter_mainFrame, (details, callback) => {
if(details.resourceType=="mainFrame") callback({cancel: true})
})
session.defaultSession.webRequest.onBeforeSendHeaders(filter_stylesheet, (details, callback) => {
if(details.resourceType=="stylesheet") callback({cancel: true})
})
i think it only work with filter_stylesheet
.
I want it to work both filters, but not possible.
👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.
To help make it easier for us to investigate your issue, please follow the contributing guidelines.
It appears that webRequest events can accept only 1 listener each. Each time the method is called, the listener is overridden.
const {session} = require('electron');
session.defaultSession.webRequest.onBeforeSendHeaders(() => {console.log('onBeforeSendHeaders cb #1')})
defaultSession.webRequest.onBeforeSendHeaders(() => {console.log('onBeforeSendHeaders cb #2')})
session.defaultSession.webRequest.onCompleted(() => {console.log('cb onCompleted #1')})
session.defaultSession.webRequest.onCompleted(() => {console.log('cb onCompleted #2')})
// navigate to trigger events
// onBeforeSendHeaders cb #2'
// cb onCompleted #2
We finally wrote a plug and play library at Station to add capabilities on WebRequest.
Still issue.
Probably right solution add addListener
/removeListener
/hasListener
to native Electron implemention, like: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest