Skip to content

Commit 1ee6c48

Browse files
Merge pull request #111 from p2plabsxyz/fix/secure-tab-handling
fix: force target=_blank links to open in new tabs
2 parents d959741 + 5b65be3 commit 1ee6c48

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

src/context-menu.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -270,31 +270,31 @@ export function attachContextMenus(browserWindow, windowManager) {
270270
label: "Open Link in New Tab",
271271
click: () => {
272272
// First, attempt to add the URL as a new tab in the current window
273-
const escapedUrl = params.linkURL.replace(/'/g, "\\'");
274273

275274
browserWindow.webContents
276275
.executeJavaScript(`
277-
const tabBar = document.querySelector('#tabbar');
278-
if (tabBar && typeof tabBar.addTab === 'function') {
279-
tabBar.addTab('${escapedUrl}');
280-
// Indicate success so main process knows no fallback is required
281-
true;
282-
} else {
283-
// Tab bar not available – signal fallback
284-
false;
285-
}
276+
(function() {
277+
const tabBar = document.querySelector('tab-bar');
278+
const url = ${JSON.stringify(params.linkURL)};
279+
if (tabBar && typeof tabBar.addTab === 'function') {
280+
tabBar.addTab(url);
281+
return true;
282+
}
283+
const oldTabBar = document.querySelector('#tabbar');
284+
if (oldTabBar && typeof oldTabBar.addTab === 'function') {
285+
oldTabBar.addTab(url);
286+
return true;
287+
}
288+
return false;
289+
})()
286290
`)
287291
.then((added) => {
288-
if (!added && windowManagerInstance) {
289-
// Fallback: open in new window if tab creation failed
290-
windowManagerInstance.open({ url: params.linkURL });
292+
if (!added) {
293+
console.warn('Failed to find tab bar for new tab');
291294
}
292295
})
293296
.catch((err) => {
294297
console.error('Failed to add tab from context menu:', err);
295-
if (windowManagerInstance) {
296-
windowManagerInstance.open({ url: params.linkURL });
297-
}
298298
});
299299
},
300300
})
@@ -329,31 +329,32 @@ export function attachContextMenus(browserWindow, windowManager) {
329329
// Intercept window.open / target="_blank" requests and try to add them as tabs
330330
webviewWebContents.setWindowOpenHandler(({ url }) => {
331331
// First, attempt to add the URL as a new tab in the current window (renderer side)
332-
const escapedUrl = url.replace(/'/g, "\\'");
333332

334333
browserWindow.webContents
335334
.executeJavaScript(`
336-
const tabBar = document.querySelector('#tabbar');
337-
if (tabBar && typeof tabBar.addTab === 'function') {
338-
tabBar.addTab('${escapedUrl}');
339-
// Indicate success so main process knows no fallback is required
340-
true;
341-
} else {
342-
// Tab bar not available – signal fallback
343-
false;
344-
}
335+
(function() {
336+
const tabBar = document.querySelector('tab-bar');
337+
const url = ${JSON.stringify(url)};
338+
if (tabBar && typeof tabBar.addTab === 'function') {
339+
tabBar.addTab(url);
340+
return true;
341+
}
342+
// Fallback for older selector if web component not found
343+
const oldTabBar = document.querySelector('#tabbar');
344+
if (oldTabBar && typeof oldTabBar.addTab === 'function') {
345+
oldTabBar.addTab(url);
346+
return true;
347+
}
348+
return false;
349+
})()
345350
`)
346351
.then((added) => {
347-
if (!added && windowManagerInstance) {
348-
// Fallback: open in new window if tab creation failed
349-
windowManagerInstance.open({ url });
352+
if (!added) {
353+
console.warn('Failed to find tab bar to add tab for url:', url);
350354
}
351355
})
352356
.catch((err) => {
353357
console.error('Failed to add tab from windowOpenHandler:', err);
354-
if (windowManagerInstance) {
355-
windowManagerInstance.open({ url });
356-
}
357358
});
358359

359360
// Always deny the automatic window creation – we will handle it ourselves

0 commit comments

Comments
 (0)