Skip to content

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

:bug: Identify native settings before user agent initialization https://github.com/siyuan-note/siyuan/issues/20002

Daniel0edaa5dff52d
diff --git a/app/src/util/frontend.test.ts b/app/src/util/frontend.test.ts
index dce530a206..02e991ebfa 100644
--- a/app/src/util/frontend.test.ts
+++ b/app/src/util/frontend.test.ts
@@ -6,7 +6,8 @@ import {createSourceFile, isVariableStatement, ModuleKind, ScriptTarget, transpi
const {parse} = require("ifdef-loader/preprocessor");
-const getFrontend = (mobile: boolean, native: boolean, toolbar: boolean, settings: boolean) => {
+const getFrontend = (mobile: boolean, native: boolean, toolbar: boolean, settings: boolean,
+ userAgent = native ? "SiYuan/3.8.7 Electron" : "Mozilla/5.0") => {
const source = createSourceFile("functions.ts", parse(readFileSync("src/util/functions.ts", "utf8"),
{MOBILE: mobile, BROWSER: !native}, false, true), ScriptTarget.ES2021, true);
const declarations = source.statements.filter(statement => isVariableStatement(statement) &&
@@ -15,7 +16,7 @@ const getFrontend = (mobile: boolean, native: boolean, toolbar: boolean, setting
runInNewContext(transpileModule(declarations.map(item => item.getText(source)).join("\n"), {
compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
}).outputText, {
- exports, window: {navigator: {userAgent: native ? "SiYuan/3.8.7 Electron" : "Mozilla/5.0"}},
+ exports, window: {navigator: {userAgent}},
document: {getElementById: () => toolbar ? {} : null, body: {classList: {contains: () => settings}}},
});
return exports.getFrontend();
@@ -23,6 +24,7 @@ const getFrontend = (mobile: boolean, native: boolean, toolbar: boolean, setting
test("native settings use desktop package compatibility rather than document-window compatibility", () => {
assert.equal(getFrontend(false, true, false, true), "desktop");
+ assert.equal(getFrontend(false, true, false, true, "Mozilla/5.0 Electron"), "desktop");
});
test("main windows, detached documents, browsers and mobile retain their frontend identities", () => {
diff --git a/app/src/util/functions.ts b/app/src/util/functions.ts
index 8849c766b6..f2095829c4 100644
--- a/app/src/util/functions.ts
+++ b/app/src/util/functions.ts
@@ -29,9 +29,14 @@ export const getFrontend = () => {
return "browser-mobile";
}
/// #else
+ /// #if !BROWSER
+ // 设置窗口首屏的浏览器标识可能尚未更新,使用桌面主窗口的集市包兼容性。
+ if (document.body.classList.contains("body--settings")) {
+ return "desktop";
+ }
+ /// #endif
if (window.navigator.userAgent.startsWith("SiYuan/")) {
- // 设置窗口使用桌面主窗口的集市包兼容性,文档新窗口单独判断。
- if (isWindow() && !document.body.classList.contains("body--settings")) {
+ if (isWindow()) {
return "desktop-window";
}
return "desktop";
diff --git a/app/tests/settingsWindow.test.js b/app/tests/settingsWindow.test.js
index d723b454a5..c442428520 100644
--- a/app/tests/settingsWindow.test.js
+++ b/app/tests/settingsWindow.test.js
@@ -43,6 +43,7 @@ const bootChild = async (sources) => {
const {ipcRenderer} = require("electron");
const {waitForSettingsWindowPaint} = loadRendererModule(sources.paint, {});
const {Dialog, genUUID, context, fit, frontend} = rendererModules(sources);
+ require("node:assert/strict").equal(navigator.userAgent.startsWith("SiYuan/"), false);
require("node:assert/strict").equal(frontend.getFrontend(), "desktop", navigator.userAgent);
const host = await new Promise(resolve => {
const token = new URLSearchParams(location.search).get("settingsWindowToken");
@@ -244,7 +245,6 @@ if (process.versions.electron && process.type === "browser") {
const {createServer} = require("node:http");
const {createSettingsWindows} = require("../electron/settingsWindows");
app.setPath("userData", process.argv[2]);
- app.userAgentFallback = "SiYuan/fixture Electron";
app.whenReady().then(async () => {
let owner;
let code = 0;
@@ -310,6 +310,7 @@ if (process.versions.electron && process.type === "browser") {
const origin = "http://127.0.0.1:" + server.address().port;
const policy = createSettingsWindows({ipcMain, screen, getTarget: id => owner?.webContents.id === id ? {origin, mode: "local"} : undefined,
initialize: win => {
+ win.webContents.userAgent = "SiYuan/fixture Electron " + win.webContents.userAgent;
win.webContents.on("console-message", details => {
if (details.level === "error" && details.message.startsWith("Uncaught")) {
console.error(details.message);
@@ -365,6 +366,7 @@ if (process.versions.electron && process.type === "browser") {
}
try {
owner = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});
+ owner.webContents.userAgent = "SiYuan/fixture Electron " + owner.webContents.userAgent;
owner.webContents.setWindowOpenHandler(details => {
const result = policy(owner.webContents, details);
return result || {action: "deny"};