示例工程 05-preload-and-contextbridge

05-preload-and-contextbridge/app/diagnostics/bad-preload.cjs

javascript · 0.7 KB

// 故意指向不存在的 preload,复现真实的加载错误。
// eslint-disable-next-line @typescript-eslint/no-require-imports -- 诊断文件
const { app, BrowserWindow } = require("electron");

app.whenReady().then(() => {
  const win = new BrowserWindow({
    show: false,
    webPreferences: {
      preload: "/nope/missing-preload.js",
      contextIsolation: true,
      sandbox: true,
      nodeIntegration: false,
    },
  });
  win.webContents.on("preload-error", (_event, preloadPath, error) => {
    console.error(`Unable to load preload script: ${preloadPath}`);
    console.error(`Error: ${error.message}`);
  });
  win.loadURL("data:text/html,<h1>x</h1>");
  setTimeout(() => app.quit(), 1500);
});