user9797962
user9797962

Reputation:

Electron session webrequest function in a renderer window that has been closed or released

From main window i am using this code to open a window on click

pwin = new BrowserWindow({
  width: 355,
  height: 250
})
pwin.loadURL(`file://${__dirname}/players/jwplayer.html`);
// pwin.setMenu(null)
pwin.webContents.on('did-finish-load', () => {
  pwin.webContents.send('link', link);
  pwin.webContents.send('name', title);
});

on the new window i have a code to modify request headers

session.defaultSession.webRequest.onBeforeSendHeaders(['*://*./*'], (details, callback) => {
  if (details.url.indexOf('okaystreamz') > -1) {
    details.requestHeaders['User-Agent'] = '[email protected]!2018';
  }

  callback({
    cancel: false,
    requestHeaders: details.requestHeaders
  })
});

When I am opening the pwin first time, its working. When I close it and open it again, I get this error

"Attempting to call a function in a renderer window that has been closed or released."

Any help will be appreciated

Upvotes: 2

Views: 1597

Answers (1)

Joel
Joel

Reputation: 937

You need an event on window close that removes the listener.

From the docs: ⚠️ Only the last attached listener will be used. Passing null as listener will unsubscribe from the event. https://electronjs.org/docs/api/web-request

Eg. Add this after your function (note the null in place of the listener):

window.addEventListener('beforeunload', function(event) {
    session.defaultSession.webRequest.onBeforeSendHeaders( ['*://*./*'], null) 
})

Upvotes: 1

Related Questions