import type { NotifySnapshot, ProgressMode, Urgency } from "../shared/ipc";
import { redactPayload } from "../shared/redact";
const TITLE = "构建完成:/Users/alice/project/app";
const BODY =
"请联系 alice@example.com,令牌 sk-demo1234567890,或 Bearer abcdefghijkl.";
function escape(text: string): string {
return text.replace(/[&<>"]/g, (char) => {
if (char === "&") return "&";
if (char === "<") return "<";
if (char === ">") return ">";
return """;
});
}
function time(ts: number): string {
return new Date(ts).toLocaleTimeString("zh-CN", { hour12: false });
}
export function mountApp(root: HTMLElement): void {
root.innerHTML = `
`;
const titleInput = root.querySelector(
'[data-testid="notify-title"]',
);
const bodyInput = root.querySelector(
'[data-testid="notify-body"]',
);
const silentInput = root.querySelector(
'[data-testid="silent"]',
);
const urgencySelect = root.querySelector(
'[data-testid="urgency"]',
);
const status = root.querySelector('[data-testid="status"]');
const platform = root.querySelector('[data-testid="platform"]');
const supported = root.querySelector(
'[data-testid="supported"]',
);
const redactedTitle = root.querySelector(
'[data-testid="redacted-title"]',
);
const redactedBody = root.querySelector(
'[data-testid="redacted-body"]',
);
const hits = root.querySelector('[data-testid="hits"]');
const toastTitle = root.querySelector(
'[data-testid="toast-title"]',
);
const toastBody = root.querySelector(
'[data-testid="toast-body-text"]',
);
const lastClicked = root.querySelector(
'[data-testid="last-clicked"]',
);
const logEl = root.querySelector('[data-testid="log"]');
const eventCount = root.querySelector(
'[data-testid="event-count"]',
);
const badgeInput = root.querySelector(
'[data-testid="badge-input"]',
);
const dockBadge = root.querySelector(
'[data-testid="dock-badge"]',
);
const progressRange = root.querySelector(
'[data-testid="progress-range"]',
);
const progressMode = root.querySelector(
'[data-testid="progress-mode"]',
);
const dockProgress = root.querySelector(
'[data-testid="dock-progress"]',
);
const capabilitiesEl = root.querySelector(
'[data-testid="capabilities"]',
);
titleInput!.value = TITLE;
bodyInput!.value = BODY;
let state: NotifySnapshot | null = null;
function payload() {
return {
title: titleInput?.value ?? "",
body: bodyInput?.value ?? "",
silent: silentInput?.checked ?? false,
urgency: (urgencySelect?.value ?? "normal") as Urgency,
};
}
function renderPreview(): void {
const result = redactPayload(
titleInput?.value ?? "",
bodyInput?.value ?? "",
);
if (redactedTitle) redactedTitle.textContent = result.title;
if (redactedBody) redactedBody.textContent = result.body;
if (toastTitle) toastTitle.textContent = result.title;
if (toastBody) toastBody.textContent = result.body;
if (hits) {
hits.innerHTML =
result.hits.length === 0
? `没有检测到敏感片段`
: result.hits
.map((hit) => `${escape(hit)}`)
.join("");
}
}
function renderState(): void {
if (!state) return;
if (platform) platform.textContent = state.platform;
if (supported) {
supported.textContent = state.supported ? "通知可用" : "通知不可用";
}
if (lastClicked) lastClicked.textContent = state.lastClicked || "—";
if (eventCount) eventCount.textContent = String(state.log.length);
if (dockBadge) dockBadge.textContent = state.badge;
if (dockProgress) {
const percent =
state.progress < 0 ? "0" : String(Math.round(state.progress * 100));
dockProgress.style.width = `${percent}%`;
dockProgress.dataset.mode = state.mode;
dockProgress.dataset.value = percent;
}
if (capabilitiesEl) {
const caps = state.capabilities;
const rows: Array<[string, boolean]> = [
["系统通知", caps.notifications],
["通知动作按钮", caps.actions],
["Dock / 启动器徽标", caps.badge],
["任务栏进度", caps.progress],
["任务栏覆盖图标", caps.overlay],
["请求注意", caps.bounce],
];
capabilitiesEl.innerHTML = rows
.map(
([label, ok]) =>
`${ok ? "✓" : "—"}${escape(label)}`,
)
.join("");
}
if (logEl) {
logEl.innerHTML =
state.log.length === 0
? `暂无事件`
: state.log
.map(
(entry) => `
${time(entry.at)}
${entry.kind}
${escape(entry.text)}
`,
)
.join("");
}
}
function apply(next: NotifySnapshot): void {
state = next;
renderState();
}
async function send(): Promise {
const next = await window.api.notify.send(payload());
apply(next);
if (status) status.textContent = "已发送(主进程已再次脱敏)";
}
async function simulateClick(): Promise {
apply(await window.api.notify.simulateClick());
if (status) status.textContent = "模拟点击:窗口已聚焦";
}
async function setBadge(text: string): Promise {
apply(await window.api.notify.setBadge(text));
}
async function setProgress(value: number, mode: ProgressMode): Promise {
apply(await window.api.notify.setProgress(value, mode));
}
window.api.notify.onChange(apply);
void window.api.notify.get().then(apply);
titleInput?.addEventListener("input", renderPreview);
bodyInput?.addEventListener("input", renderPreview);
renderPreview();
root
.querySelector('[data-testid="send"]')
?.addEventListener("click", () => void send());
root
.querySelector('[data-testid="simulate-click"]')
?.addEventListener("click", () => void simulateClick());
root
.querySelector('[data-testid="clear"]')
?.addEventListener("click", async () => {
apply(await window.api.notify.clear());
if (status) status.textContent = "已清除状态";
});
root
.querySelector('[data-testid="bounce"]')
?.addEventListener("click", async () => {
apply(await window.api.notify.bounce());
if (status) status.textContent = "已请求注意";
});
root
.querySelector('[data-testid="badge-apply"]')
?.addEventListener("click", () => void setBadge(badgeInput?.value ?? ""));
root
.querySelector('[data-testid="badge-clear"]')
?.addEventListener("click", () => {
if (badgeInput) badgeInput.value = "";
void setBadge("");
});
root
.querySelector('[data-testid="progress-apply"]')
?.addEventListener("click", () => {
const value = Number(progressRange?.value ?? "0");
void setProgress(
value,
(progressMode?.value ?? "normal") as ProgressMode,
);
});
root
.querySelector('[data-testid="progress-clear"]')
?.addEventListener("click", () => void setProgress(-1, "none"));
for (const button of root.querySelectorAll(
"[data-preview-action]",
)) {
button.addEventListener("click", () => void simulateClick());
}
window.__prepareShots = async (stage) => {
await window.api.notify.clear();
if (stage === "sent") {
await window.api.notify.send(payload());
await window.api.notify.simulateClick();
if (status) status.textContent = "已发送并模拟点击";
}
if (stage === "dock") {
await window.api.notify.setBadge("3");
await window.api.notify.setProgress(0.62, "normal");
await window.api.notify.bounce();
if (status) status.textContent = "徽标与进度已应用";
}
apply(await window.api.notify.get());
};
window.__smoke = async () => {
const api = window.api.notify;
await api.clear();
const preview = await api.preview(TITLE, BODY);
const sent = await api.send(payload());
const showLogged = sent.log.some((entry) => entry.kind === "show");
const clickedState = await api.simulateClick();
const clicked = clickedState.lastClicked;
await api.setBadge("7");
await api.setProgress(0.2, "normal");
await api.bounce();
const bounced = await api.get();
const bounceLogged = bounced.log.some((entry) => entry.kind === "bounce");
const clearedState = await api.clear();
const cleared =
clearedState.badge === "" &&
clearedState.progress === -1 &&
clearedState.log.length === 0;
const finalState = await api.setBadge("3");
const withProgress = await api.setProgress(0.5, "normal");
apply(withProgress);
return {
platform: finalState.platform,
supported: finalState.supported,
previewTitle: preview.title,
previewBody: preview.body,
hits: preview.hits,
showLogged,
clicked,
badge: withProgress.badge,
progress: withProgress.progress,
bounceLogged,
cleared,
logAfterClear: clearedState.log.length,
};
};
}