On Windows magpie's window is http://wails.localhost (WebView2). page/boot.js counted only localhost, 127.0.0.1 and an empty host as this computer, so the Windows window got the "remote browser" message (#1024). It now counts *.localhost, ::1 and 127.* too, the same rule as chat, fingerprint, kimi-desktop, page-model-quota and window-warmup. magpie.service still wins. What was run at 84a6a812739b96191e9d63805cd408e0abef717a (mote, macOS): - boot.js in a node vm with a mocked location and magpie bridge, new and old side by side. wails.localhost, WAILS.LOCALHOST, 127.0.0.2 and [::1]: old shows the remote message, new opens http://127.0.0.1:3497/. localhost, 127.0.0.1 and an empty host open it in both. 192.168.1.5, evil.com and localhost.evil.com show the remote message in both. A magpie.service is used in both. - packages/amp-usage: node --test test/*.test.mjs, 48 pass, 0 fail. bun scripts/check.mjs amp-usage passed. Not tried: a real magpie window on Windows. No test of boot.js in the package yet. Co-authored-by: overstarry <overstarry@users.noreply.github.com>
75 lines
2.8 KiB
JavaScript
75 lines
2.8 KiB
JavaScript
(async () => {
|
|
const zh = (lang) =>
|
|
String(lang || navigator.language || "")
|
|
.toLowerCase()
|
|
.startsWith("zh");
|
|
const words = (lang) =>
|
|
zh(lang)
|
|
? {
|
|
title: "Amp 用量",
|
|
connecting: "正在连接本地采集服务…",
|
|
stopped: "采集服务没有启动。请查看插件日志,并确认端口没有被占用。",
|
|
port: "插件选项中的服务端口无效。",
|
|
remote: "请更新 Magpie,才能从远程浏览器打开这个服务。",
|
|
retry: "重新连接",
|
|
}
|
|
: {
|
|
title: "Amp usage",
|
|
connecting: "Connecting to the local collector…",
|
|
stopped:
|
|
"The collector did not start. Check the plugin log and whether its port is already in use.",
|
|
port: "Invalid service port in plugin options.",
|
|
remote: "Update Magpie to open this service from a remote browser.",
|
|
retry: "Reconnect",
|
|
};
|
|
const message = document.getElementById("message");
|
|
const retry = document.getElementById("retry");
|
|
retry.addEventListener("click", () => location.reload());
|
|
let text = words();
|
|
const say = (key) => {
|
|
document.title = text.title;
|
|
document.getElementById("title").textContent = text.title;
|
|
retry.textContent = text.retry;
|
|
message.textContent = text[key];
|
|
};
|
|
say("connecting");
|
|
try {
|
|
const theme = await magpie.ready;
|
|
text = words(theme.lang);
|
|
say("connecting");
|
|
const options = await magpie.options.get();
|
|
const port = Number(options?.port ?? 3497);
|
|
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
throw new Error("port");
|
|
// This computer's window: wails://localhost on macOS and Linux,
|
|
// http://wails.localhost on Windows (Wails v3, WebView2).
|
|
const host = location.hostname.toLowerCase().replace(/^\[|\]$/g, "");
|
|
const local =
|
|
host === "" ||
|
|
host === "localhost" ||
|
|
host.endsWith(".localhost") ||
|
|
host === "::1" ||
|
|
/^127\./.test(host);
|
|
const target =
|
|
magpie.service || (local ? `http://127.0.0.1:${port}/` : "");
|
|
if (!target) throw new Error("remote");
|
|
const url = new URL(target);
|
|
url.searchParams.set("theme", theme.dark ? "dark" : "light");
|
|
url.searchParams.set("lang", zh(theme.lang) ? "zh" : "en");
|
|
// The service's page wears the window's own colours.
|
|
const tokens = {};
|
|
for (const [k, v] of Object.entries(theme.tokens || {}))
|
|
if (/^[\x20-\x7e]{1,160}$/.test(String(v))) tokens[k] = String(v);
|
|
if (Object.keys(tokens).length)
|
|
url.searchParams.set("tokens", btoa(JSON.stringify(tokens)));
|
|
setTimeout(() => {
|
|
say("stopped");
|
|
retry.hidden = false;
|
|
}, 6000);
|
|
location.replace(url.href);
|
|
} catch (e) {
|
|
say(text[e.message] ? e.message : "stopped");
|
|
retry.hidden = false;
|
|
}
|
|
})();
|