Fix CSS injection broken with Electron 3 (PR #709, Fixes #703)

See Electron 3.0 breaking API changes:
https://electronjs.org/blog/electron-3-0#breaking-api-changes

  [#12477] https://github.com/electron/electron/pull/12477
           refactor: removed `did-get-response-details` and `did-get-redirect-request events`
This commit is contained in:
Luccas Clezar 2018-12-01 00:14:20 -02:00 committed by Ronan Jouchet
parent 038812cd46
commit 114b4a9724
1 changed files with 10 additions and 6 deletions

View File

@ -43,20 +43,24 @@ function maybeInjectCss(browserWindow) {
const injectCss = () => {
browserWindow.webContents.insertCSS(cssToInject);
};
const onHeadersReceived = (details, callback) => {
injectCss();
callback({ cancel: false, responseHeaders: details.responseHeaders });
};
browserWindow.webContents.on('did-finish-load', () => {
// remove the injection of css the moment the page is loaded
browserWindow.webContents.removeListener(
'did-get-response-details',
injectCss,
);
browserWindow.webContents.session.webRequest.onHeadersReceived(null);
});
// on every page navigation inject the css
browserWindow.webContents.on('did-navigate', () => {
// we have to inject the css in did-get-response-details to prevent the fouc
// we have to inject the css in onHeadersReceived to prevent the fouc
// will run multiple times
browserWindow.webContents.on('did-get-response-details', injectCss);
browserWindow.webContents.session.webRequest.onHeadersReceived(
null,
onHeadersReceived,
);
});
}