Problem with finding frames by routing ID in Electron 5

#1

0

I am working on a project in Electron where I will need to access user-loaded pages and perform some manipulations with their DOM. I’m using BrowserView because apparently it’s more performant and has fewer bugs, so my BrowserView init code looks like this:

let view = new BrowserView({
    webPreferences: {
        preload:  path.join(__dirname, 'preload.js'),
        nodeIntegration: false,
        contextIsolation: true,
        sandbox: true,
        enableRemoteModule: false,
        allowPopups: false
    }
})
win.setBrowserView(view)

Now, I need to inject a certain JS code into the main frame of the page, as well as the iframes. In order to do that, I’m currently using “did-frame-finish-load” listener in the main process, like so:

view.webContents.on("did-frame-finish-load", function (e, isMainFrame, frameProcessId, frameRoutingId) {
    view.webContents.send("from_mainland", {"command": "inject_code", "frameRoutingId": frameRoutingId, "isMainFrame": isMainFrame})
}); 

This technique allowed me to receive “from_mainland” message in my preload.js file using IPC, find a frame by routing ID supplied in the call and inject necessary code into relevant frame by calling executeJavaScript on it.

It works well in Electron 4.1.4. However, yesterday I tried it on another machine that has Electron 5.0.1 installed, and everything broke. Essentially, function webFrame.findFrameByRoutingId returns null when I supply it the routing id from the call. Any ideas on what’s causing this issue and how to work around it?

Thank you!

#2

Upon further investigation I discovered that findFrameByRoutingId and firstChild calls on webFrame will return frames if they belong to the same domain as the main frame and null in all other cases. Feels like a bug to me.