Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions app/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,39 @@ try {
app.exit();
}

const windowNavigate = (currentWindow) => {
const windowNavigate = (currentWindow, windowType) => {
currentWindow.webContents.on("will-navigate", (event) => {
const url = event.url;
if (url.startsWith(localServer)) {
return;
const urlObj = new URL(url);
const pathname = urlObj.pathname;
let isAllowed = false;
// 所有窗口都允许认证页面
if (pathname === "/check-auth") {
isAllowed = true;
} else {
// 根据窗口类型判断允许的路径
switch (windowType) {
case "app":
// 主窗口:只允许应用入口路径
isAllowed = pathname === "/stage/build/app/";
break;
case "window":
// 新窗口:允许 window.html
isAllowed = pathname === "/stage/build/app/window.html";
break;
case "export":
// 导出预览窗口:允许导出临时文件路径
isAllowed = pathname.startsWith("/export/temp/");
break;
Copy link

Copilot AI Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch statement lacks a default case to handle unexpected windowType values. If an unknown windowType is passed, isAllowed will remain false and navigation will be blocked, which may cause unexpected behavior. Consider adding a default case that either logs a warning or sets a reasonable default behavior.

Suggested change
break;
break;
default:
// 未知窗口类型:保持不允许导航,但记录警告以便调试
console.warn(
`Unexpected window type in windowNavigate: "${windowType}" for path "${pathname}"`
);
break;

Copilot uses AI. Check for mistakes.
}
}

if (isAllowed) {
return;
}
}
// 阻止导航并在外部浏览器打开
event.preventDefault();
shell.openExternal(url);
});
Expand Down Expand Up @@ -449,7 +476,7 @@ const initMainWindow = () => {
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
// 当前页面链接使用浏览器打开
windowNavigate(currentWindow);
windowNavigate(currentWindow, "app");
currentWindow.on("close", (event) => {
if (currentWindow && !currentWindow.isDestroyed()) {
currentWindow.webContents.send("siyuan-save-close", false);
Expand Down Expand Up @@ -1043,7 +1070,7 @@ app.whenReady().then(() => {
printWin.center();
printWin.webContents.userAgent = "SiYuan/" + appVer + " https://b3log.org/siyuan Electron " + printWin.webContents.userAgent;
printWin.loadURL(data);
windowNavigate(printWin);
windowNavigate(printWin, "export");
});
ipcMain.on("siyuan-quit", (event, port) => {
exitApp(port);
Expand Down Expand Up @@ -1092,7 +1119,7 @@ app.whenReady().then(() => {
win.webContents.userAgent = "SiYuan/" + appVer + " https://b3log.org/siyuan Electron " + win.webContents.userAgent;
win.webContents.session.setSpellCheckerLanguages(["en-US"]);
win.loadURL(data.url);
windowNavigate(win);
windowNavigate(win, "window");
win.on("close", (event) => {
if (win && !win.isDestroyed()) {
win.webContents.send("siyuan-save-close");
Expand Down