Repository navigation
Can you provide a better example converting from nodintegration = true to conTextBridge for Electron? #10702
Replies: 3 comments 8 replies
|
I would really recommend following the secure way of setting the app: That approach works just fine - have been tested in a couple of applications. |
|
That is fine I have no problem with that. It just that after searching the internet and looking through the examples it is still not very clear to me what is going on and how I convert from the old to the new contextBridge. The example you listed does not really help me understand how to convert. Any way you could provide a better example of how to convert from the example I stated above. Maybe an example showing the old way and the steps you need to take to convert. |
|
Here is the whole conversion for exactly your two calls ( Before (renderer had Node): const fs = require("fs");
const store = require("electron-store");1. main process - own the capability, decide the policy: const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const fs = require("fs/promises");
const Store = require("electron-store");
const store = new Store({ defaults: { theme: "dark" } });
new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, "electron-preload.js"),
contextIsolation: true,
nodeIntegration: false,
},
})
ipcMain.handle("file:read", (_e, name) => {
// never accept an arbitrary path from the renderer
const safe = path.join(app.getPath("userData"), path.basename(name));
return fs.readFile(safe, "utf8");
});
ipcMain.handle("settings:get", (_e, key) => store.get(key));
ipcMain.handle("settings:set", (_e, key, value) => store.set(key, value));2. electron-preload.js - the only file that may require const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("native", {
readFile: (name) => ipcRenderer.invoke("file:read", name),
getSetting: (key) => ipcRenderer.invoke("settings:get", key),
setSetting: (key, value) => ipcRenderer.invoke("settings:set", key, value),
});3. render.vue - <script setup>
const theme = await window.native.getSetting("theme");
await window.native.setSetting("theme", "light");
const data = JSON.parse(await window.native.readFile("profile.json"));
</script>4. App.vue / boot - if you want it like an injectable service, wrap it once: export const settings = {
async get(key) { return window.native.getSetting(key); },
async set(key, value) { return window.native.set(key, value); },
};Things that make this feel confusing at first:
onTheme: (cb) => {
const wrapped = (_e, theme) => cb(theme);
ipcRenderer.on("theme:set", wrapped);
return () => ipcRenderer.removeListener("theme:set", wrapped);
},Returning the unsubscribe matters in Vue - without it every
|
Uh oh!
There was an error while loading. Please reload this page.
It would be great if you could provide a complete conversion example.
Lets say I have in my render.vue
fs = require('fs')
store = require('electron-store')
Could you provide a complete example of what we need to put in the electron-preload.js, the App vue, Main vue, and Render vue in order to get it working like it was by setting nodeIntegration = true and contextIsolation = false.
Currently even if you set contextIsolation = false and nodeIntegration = true and run quasar dev -m electron it fails. However in a standard electron app this still works as expected. I know it it very insecure but if I you just local packages and have one api from a trusted site it would be nice to be able to use nodeIntegration = true. But if you can give a nice complete example from the old nodeIntegration to the new way that would be great. TY
All reactions