1

I'm trying to access the DOM inside the webview. I understand I need to use IPC, but with the following code which is in the same file, one accesses the parent and one accesses the webview.

Preload.js (using preload property of webview)

setInterval(() => {
  console.log(document.querySelector('.player')); // shows in webview
}, 1000);

ipcRenderer.on('ppause', () => {
  console.log(document); // shows in main window
});

index.js

window.webContents.once('dom-ready', () => {
    globalShortcut.register('MediaPlayPause', () => {
        window.webContents.send('ppause');
    });
});

I want to be able to access the webview DOM (such as in the setInterval function), but I can't seem to figure it out.

Nathan
  • 534
  • 1
  • 5
  • 13

1 Answers1

2

I ended up figuring it out.

I needed to send it to the webcontents, and from there send it to the webview.

preload

ipcRenderer.on('ppause', () => {
    console.log(document); // can now manipulate webview DOM
});

Browser

const webview = document.getElementById('webview');
ipcRenderer.on('ppause', () => {
    webview.send('ppause');
});

index.js

globalShortcut.register('MediaPlayPause', () => {
    window.webContents.send('ppause');
});

All that was needed needed was an additional send to webview.

Nathan
  • 534
  • 1
  • 5
  • 13