Skip to content

Instantly share code, notes, and snippets.

@mgalla10
Created October 13, 2022 17:14
Show Gist options
  • Save mgalla10/8dc8da58f36238db0ef950a0a1d639d9 to your computer and use it in GitHub Desktop.
Save mgalla10/8dc8da58f36238db0ef950a0a1d639d9 to your computer and use it in GitHub Desktop.
webContents.opener undefined for target=_blank links
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<label>Window ID: </label><label id="lblWindowId">null</label><br />
<label>Opener window ID: </label><label id="lblOpenerId">null</label><br /><br />
<button id="btnOpen">New window via window.open()</button><br />
<a href="index.html" target="_blank">New window via target=&QUOT;_blank&QUOT;;</a><br />
<a id="aTarget" href="index.html" target="target">New window via target=&QUOT;target&QUOT;</a>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
const { app, BrowserWindow, webContents } = require("electron")
const path = require("path")
function createWindow () {
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, "preload.js")
}
})
setUpWindow(mainWindow);
}
async function setUpWindow(window)
{
window.webContents.setWindowOpenHandler(() =>
{
return {
action: "allow",
overrideBrowserWindowOptions: {
webPreferences: {
preload: path.join(__dirname, "preload.js")
}
}
};
});
window.webContents.on("did-create-window", setUpWindow);
if (window.id === 1)
{
await window.loadFile("index.html");
}
let openerId = null;
const openerFrame = window.webContents.opener;
if (openerFrame)
{
openerId = "frame";
const openerWebContents = webContents.fromFrame(openerFrame);
if (openerWebContents)
{
openerId = "webContents";
const openerWindow = BrowserWindow.fromWebContents(openerWebContents);
if (openerWindow)
{
openerId = openerWindow.id || "window";
}
}
}
window.webContents.send("set-window-info", { windowId: window.id, openerId });
}
app.whenReady().then(() => createWindow());
app.on("window-all-closed", () => app.quit());
{
"name": "faulty-account-altered-ocmt1",
"productName": "faulty-account-altered-ocmt1",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "mgallagh",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "21.1.1"
}
}
const { ipcRenderer } = require("electron");
ipcRenderer.on("set-window-info", (_event, args) =>
{
const openerId = args.openerId;
document.getElementById("lblWindowId").innerText = args.windowId;
document.getElementById("lblOpenerId").innerHTML = openerId;
if (openerId)
{
const aTarget = document.getElementById("aTarget");
aTarget.target = `target${openerId}`
aTarget.innerText = `New window via target="target${openerId}"`;
}
});
document.getElementById("btnOpen").addEventListener("click", () =>
{
window.open("index.html");
});
/* styles.css */
/* Add styles here to customize the appearance of your app */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment