Let's say I've got 2 processes:
Process 1 is sending to process 2 a valid html string:
ipcRenderer.send('open-window-from-string',
'<!DOCTYPE html>' + '<html>' + htmlElement.innerHTML + '</html>');
Process 2 (Electron Main-Process) is trying to open a new window from that string:
ipc.on('open-window-from-string', (event, htmlString) => {
const windowFromString= BrowserWindow.fromWebContents(htmlString);
}
I know I could save the html as an actual html file. That way everything worked while using:
loadURL(`file://${__dirname}/windowFromString.html`);
However that would cause unnecessary read/write actions.
That's why I am trying to load a new window from a htmlString out of my memory. So again the question is: Is it possible to load electron webContents from an in memory html string?
Thanks in advance for any help.
Regards, Megajin
@Hackjutsu, it is provided by electron package. You can import it like this: var ipcRenderer = require ('electron').ipcRenderer; or es6 import { ipcRenderer } from 'electron'; Do you know if arguments are passed as references, or if the renderer creates a new reference?
To see the console of the browser you need to open the dev tools, either from the default Electron menu or from your code. e.g. inside the createWindow () function
The win.webContents.savePage (filepath, saveType) Instance method is used to save the current BrowserWindow Instance as HTML file onto the native System depending upon the saveType parameter. It returns a Promise and it resolves when the webpage is saved successfully.
All Chromium browsers support saving webpages as HTML files onto the native system. Usually, this functionality is triggered by a simple Ctrl+S Keyboard shortcut on any webpage in the Chromium Browser.
in Chrome, you can display inline html pages by navigating URL with data: protocol such as data:text/html;charset=utf-8,<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>MyYTitle</title> <style type="text/css"> </style></head> <body>Hello world from Lyon, FR</body>
It works the same in Electron.
Can you try opening a window with loadURL('data:text/html;charset=utf-8,<YOUR HTML/>');
?
Well, perhaps you cannot directly load whole html. As a workaround, you can open a new browser window with just contents as:
<html>
<head></head>
<body></body>
</html>
After you open this you can use browserWindow.webContents.evaluate() to load actual HTML passed as String. If required you can use webContents.reload() for changes to take effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With