Last active
December 8, 2021 17:02
-
-
Save jangnezda/e669c6107b0a0de301252c82de252823 to your computer and use it in GitHub Desktop.
How to capture requests from <iframe> in Electron window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* By default, the Electron (or underlying Chrome instance, to be more precise) will not allow | |
* javascript communication between parent window and an <iframe>. Which makes it hard to cover | |
* scenarios when you want to do something after an action has been performed inside the <iframe>. | |
* This example shows how to react on web reqest from within <iframe>. | |
* | |
* The 'onBeforeRequest' listener is documented here: | |
* http://electron.atom.io/docs/api/session/#webrequestonbeforerequestfilter-listener | |
*/ | |
app.on('ready', () => { | |
let myWindow = new BrowserWindow({ | |
width: 800, | |
height: 800, | |
center: true | |
}); | |
// Listen to all requests in order to catch the one you want to react to. For example on | |
// link click, form submit, etc. | |
// This hack is needed, because it's otherwise not possible to react to javascript events | |
// within an <iframe> due to browser security/sandboxing when iframe url is on different domain. | |
myWindow.webContents.session.webRequest.onBeforeRequest({}, (details, callback) => { | |
if (details.url.endsWith('/actions/submit/')) { | |
// do your stuff ... | |
// ... for example, block the submit | |
callback({ cancel: true }); | |
myWindow.close(); | |
// ... or open new electron window, etc. | |
} | |
callback({ cancel: false }); | |
}); | |
// replace with whatever html you want to load in your electron window | |
myWindow.loadURL(`file://${__dirname}/index.html`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment