Reputation: 1
In an electron app, I use window.loadURL('http://example.com')
to load my website, this website uses localStorage
to save user data. My question is how to use the localStorage
with Electron API?
Here is my code :
{
label:'test',
click:()=>{
let object = mainWindow.webContents.executeJavaScript('localStorage.length');
console.log("localstorage.length:"+JSON.stringify(object));
}
}
But it will print localstorage.length:{}
Upvotes: 0
Views: 4894
Reputation: 1245
executeJavaScript
is an asynchronous function and returns a Promise. You need to either use async/await
or chain .then()
to access the value returned by the function.
// inside an async function
const object = await mainWindow.webContents.executeJavaScript('localStorage.length');
console.log(`Localstorage length: ${object}`);
or
mainWindow.webContents.executeJavaScript('localStorage.length')
.then(object => console.log(`Localstorage length: ${object}`));
Check out this example openable in Electron Fiddle. Swap out loadFile
for loadURL
with your website, and you should be set.
Upvotes: 1