Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

error: contextbridge api can only be used when contextisolation is enabled

const {
  app,
  BrowserWindow,
  ipcMain
} = require("electron");
const path = require("path");
const fs = require("fs");

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;

async function createWindow() {

  // Create the browser window.
  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false, // is default value after Electron v5
      contextIsolation: true, // protect against prototype pollution
      enableRemoteModule: false, // turn off remote
      preload: path.join(__dirname, "preload.js") // use a preload script
    }
  });

  // Load app
  win.loadFile(path.join(__dirname, "dist/index.html"));

  // rest of code..
}

app.on("ready", createWindow);

ipcMain.on("toMain", (event, args) => {
  fs.readFile("path/to/file", (error, data) => {
    // Do something with file contents

    // Send result back to renderer process
    win.webContents.send("fromMain", responseObj);
  });
});
Comment

error: contextbridge api can only be used when contextisolation is enabled

const {
    contextBridge,
    ipcRenderer
} = require("electron");

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld(
    "api", {
        send: (channel, data) => {
            // whitelist channels
            let validChannels = ["toMain"];
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, data);
            }
        },
        receive: (channel, func) => {
            let validChannels = ["fromMain"];
            if (validChannels.includes(channel)) {
                // Deliberately strip event as it includes `sender` 
                ipcRenderer.on(channel, (event, ...args) => func(...args));
            }
        }
    }
);
Comment

error: contextbridge api can only be used when contextisolation is enabled

<!doctype html>
<html lang="en-US">
<head>
    <meta charset="utf-8"/>
    <title>Title</title>
</head>
<body>
    <script>
        window.api.receive("fromMain", (data) => {
            console.log(`Received ${data} from main process`);
        });
        window.api.send("toMain", "some data");
    </script>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: react-bootstrap sidebar 
Javascript :: on window resize and on page load 
Javascript :: js === 
Javascript :: 1. Write regular expression to describe a languages consist of strings made of even numbers a and b. CO1 K3 
Javascript :: how to deploy nextjs app on netlify 
Javascript :: create new Next.js 
Javascript :: js comparison operators 
Javascript :: what is the function of delete operator in javascript 
Javascript :: new Date().now 
Javascript :: use this inside a foreach 
Javascript :: how to see if user on phone 
Javascript :: @apify/http-request 
Javascript :: Type writer in react 
Javascript :: javascript update text in div 
Javascript :: share to gmail from website 
Javascript :: mongodb empty an array field 
Javascript :: JS clickable checkbox 
Javascript :: redirect to website from promise value fetch 
Javascript :: new map js 
Javascript :: async arrow function js 
Javascript :: use navigate in class component react native 
Javascript :: Square Every Digit 
Javascript :: check if value is number 
Javascript :: Odoo Plain Javascript files 
Javascript :: how to get current template in vuejs 
Javascript :: change console log to print javascript 
Javascript :: axios get request javascript stackoverflow 
Javascript :: what does document.getelementbyid return 
Javascript :: how to set window location search without reload 
Javascript :: lastindexof() javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =