AnyBot/mobile/sw.js

39 lines
2.0 KiB
JavaScript
Raw Normal View History

feat: 電腦版不再破版、通知用 AnyBot 的貓、手機網頁像 App;從帳號看畫面不再斷線 電腦版:右側「進行中」的長重試訊息蓋過「停止」、衝出卡片,現在以「…」結尾(滑鼠移上去看 全文);Bot 設定的「團隊成員可以使用…」疊到儲存/取消、權限規則疊到「刪除」、「新增規則」 被切掉、電腦分頁的時間欄被切掉、窄視窗裡允許卡片的按鈕、「已排入…」和輸入框提示被切掉, 都修好。粉圓體比思源黑體寬,寫死寬度的地方用它就擠爆。新的 layout_audit 測試用三種字型、 最窄/一般/寬的視窗、示範和很長的資料,把每個設定分頁、Bot 設定、例行任務、右側面板畫一遍, 字被切掉或疊在一起就失敗(截圖模式也多了 ANYBOT_SHOT_PANEL/STATUS/TAB/SCROLL)。 通知:Mac 上原本用 osascript 的 display notification,橫幅是「工序指令編寫程式」的圖示、 點了還打開它。現在是 AnyBot.app 自己發的(NSUserNotificationCenter:新的 UNUserNotificationCenter 只給有 Apple 開發者簽章的 App 用,AnyBot 自簽),圖示是貓,點一 下打開那個 Bot;不在 .app 裡才退回 osascript。Linux 的 notify-send 帶上 AnyBot 的圖示。 anybot --banner 標題 內文 可以送一則試試。 手機(PWA):manifest 和 service worker 改成 mobile/ 裡一份,帳號伺服器和這台共用;加滿版的 maskable 圖示和 Android 狀態列用的單色貓(原本彩色圖變成白方塊)。每支手機滿版(左右安全 區域、狀態列跟著畫面顏色)、首頁提示裝成 App、換畫面左右滑、iPhone 主畫面 App 從左邊緣滑回 上一頁、按鈕長按不選字、搜尋框不放大。通知可以選要哪幾種(需要你/做完了/例行任務暫停/ 收到檔案)、送測試通知,App 圖示上的數字是正在等你的 Bot。 帳號伺服器:看 Bot 電腦畫面時,瀏覽器回的 Pong 沒算成還活著,靜止畫面一分鐘就被切;看畫面 的票證十分鐘一到就切斷、重連又拿同一張過期的票回不來。現在票證記得誰申請的(那支手機的 登入、帳號裡那台電腦),申請者還有效就一直連著,使用中的票證會續期。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 16:06:12 +00:00
// AnyBot 手機 App 的 Service Worker(帳號伺服器送出):推播通知、點通知打開那個 Bot、App 圖示上的數字。
// 通知的圖示是 AnyBot 的貓(黃色底);Android 狀態列的小圖示只能是單色,用那隻貓的剪影(badge-96.png)。
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", (e) => e.waitUntil(self.clients.claim()));
// kind: need(需要你允許、問你、需要你處理)、done(做完了)、routine(例行任務暫停)、file(收到傳來的檔案)、test。
self.addEventListener("push", (e) => {
let d = {};
try { d = e.data ? e.data.json() : {}; } catch (_) { d = {body: e.data ? e.data.text() : ""}; }
const need = d.kind === "need";
const shown = self.registration.showNotification(d.title || "AnyBot", {
body: d.body || "",
// One notification per bot: the newest word about it replaces the last one, and still rings.
tag: d.kind === "test" ? "test" : "bot-" + (d.bot || 0),
renotify: true,
data: d,
icon: "icon-192.png",
badge: "badge-96.png",
timestamp: Date.now(),
// 需要你: stays until you look (Android, desktop), and buzzes twice.
requireInteraction: need,
vibrate: need ? [90, 70, 90] : [70],
});
// The number on the app's icon: bots waiting for you.
const n = typeof d.badge === "number" ? d.badge : null;
const badge = n === null || !self.navigator.setAppBadge ? null : n > 0 ? self.navigator.setAppBadge(n) : self.navigator.clearAppBadge();
e.waitUntil(Promise.all([shown, badge].filter(Boolean)).catch(() => {}));
});
self.addEventListener("notificationclick", (e) => {
e.notification.close();
const d = e.notification.data || {};
const url = "./#host=" + encodeURIComponent(d.host || "") + "&bot=" + (d.bot || "");
e.waitUntil(self.clients.matchAll({type: "window", includeUncontrolled: true}).then((cs) => {
for (const c of cs) { c.postMessage({open: d}); return c.focus(); }
return self.clients.openWindow(url);
}));
});