Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Prevent invalid window URLs from blocking document opening https://github.com/siyuan-note/siyuan/issues/18697

Danielf6ee822d5309
diff --git a/app/electron/main.js b/app/electron/main.js
index f0625cf1f1..e2b774c1cc 100644
--- a/app/electron/main.js
+++ b/app/electron/main.js
@@ -1773,22 +1773,35 @@ app.whenReady().then(() => {
}
}
if (data.cmd === "siyuan-open-file") {
- let hasMatch = false;
- BrowserWindow.getAllWindows().find(item => {
- const url = new URL(item.webContents.getURL());
- if (item.webContents.id === event.sender.id || data.port !== url.port) {
- return;
+ const options = JSON.parse(data.options);
+ return BrowserWindow.getAllWindows().some(item => {
+ if (item.isDestroyed() || item.webContents.isDestroyed() ||
+ item.webContents.id === event.sender.id) {
+ return false;
+ }
+
+ let url;
+ let ids;
+ try {
+ const currentURL = item.webContents.getURL();
+ if (!currentURL) {
+ return false;
+ }
+ url = new URL(currentURL);
+ ids = decodeURIComponent(url.hash.substring(1)).split("\u200b");
+ } catch {
+ return false;
+ }
+ if (data.port !== url.port) {
+ return false;
}
- const ids = decodeURIComponent(url.hash.substring(1)).split("\u200b");
- const options = JSON.parse(data.options);
if (ids.includes(options.rootID) || ids.includes(options.assetPath)) {
item.focus();
item.webContents.send("siyuan-open-file", options);
- hasMatch = true;
return true;
}
+ return false;
});
- return hasMatch;
}
});
diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts
index b9f70d5b05..6e1d4f1ccd 100644
--- a/app/src/editor/util.ts
+++ b/app/src/editor/util.ts
@@ -224,11 +224,15 @@ export const openFile = async (options: IOpenFileOptions) => {
optionsClone[key] = JSON.parse(JSON.stringify(options[key]));
}
});
- hasMatch = await ipcRenderer.invoke(Constants.SIYUAN_GET, {
- cmd: Constants.SIYUAN_OPEN_FILE,
- options: JSON.stringify(optionsClone),
- port: location.port,
- });
+ try {
+ hasMatch = await ipcRenderer.invoke(Constants.SIYUAN_GET, {
+ cmd: Constants.SIYUAN_OPEN_FILE,
+ options: JSON.stringify(optionsClone),
+ port: location.port,
+ });
+ } catch (e) {
+ console.warn("Check opened file window error:", e);
+ }
if (hasMatch) {
if (options.afterOpen) {
options.afterOpen();