Insert JS into mainWindow via webContents


#1

Is there a way to do something similar to insertCSS via webContents but for JS scripts?

mainWindow.webContents.insertJS()

I need to inject some code into remote site loaded to mainWindow. Thanks


#2

executeJavaScript


#3

As stef_pellegrino said you can use executeJavaScript. You can find the documentation on it here: http://electron.atom.io/docs/api/web-contents/#contentsexecutejavascriptcode-usergesture-callback

this example adds the platform to the class attribute of the HTML-tag of the loaded page.

mainWindow.webContents.on('dom-ready', function(e) {
  mainWindow.webContents.executeJavaScript('document.getElementsByTagName("html")[0].className += " platform-' + process.platform + '" ')
})