Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to load electron webContents from an in memory html string?

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

like image 426
Megajin Avatar asked Feb 03 '17 11:02

Megajin


People also ask

How do I import an electron renderer?

@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?

How to see the console of the browser in electron?

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

How to save the current browserwindow instance as HTML file?

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.

How to save webpages as HTML files in Chromium browser?

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.


2 Answers

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/>');?

like image 110
sritmak Avatar answered Oct 15 '22 20:10

sritmak


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.

like image 45
devilpreet Avatar answered Oct 15 '22 21:10

devilpreet