Biede
Biede

Reputation: 56

Retrievin a class from an html in electron

I have a basic electron app where I am trying to retrieve data from the the html's document form. I basically want ot retrive data like in a ordinary html document with something like const form = document.querySelector('form'); but when I start the program it always shows an error that document is not defined

const electron = require('electron');
const url = require('url');
const path = require('path');

let mainWindow;

app.on('ready', () => {

mainWindow = new BrowserWindow({});

mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'MainWindow.html'),
    protocol: 'file:',
    slashes: true
}));

const form = document.querySelector('form');
});

<body>
    <main>
        <form class="submit-form">
            <input type="text" placeholder="Search Text..">
            <button>Search</button>
        </form>
    </main>
</body>

Upvotes: 0

Views: 45

Answers (1)

pergy
pergy

Reputation: 5531

document is available only in Renderer process. So you have to call your query there. Most straightforward thing to do in your case is to executeJavaScript on BrowserWindow's webContents

mainWindow.webContents.on('dom-ready', () => {
  mainWindow.webContents.executeJavaScript(`
    const form = document.querySelector('form');
    // manipulate form...
  `)
})

Upvotes: 1

Related Questions