From a464653d542975e3b8610230c486545dd22956f4 Mon Sep 17 00:00:00 2001 From: Yoko Date: Fri, 4 Dec 2026 21:09:21 +0800 Subject: [PATCH 1/2] fix(ai-chat-turbo-engine): stop a site hover-close from collapsing the peek --- .../src/core/sidebar-peek.ts | 135 +++++++++++++++- tests/ai-chat-turbo-sidebar-peek.test.ts | 151 ++++++++++++++++++ 2 files changed, 285 insertions(+), 1 deletion(-) diff --git a/scripts/ai-chat-turbo-engine/src/core/sidebar-peek.ts b/scripts/ai-chat-turbo-engine/src/core/sidebar-peek.ts index 444b75c..1f08f74 100644 --- a/scripts/ai-chat-turbo-engine/src/core/sidebar-peek.ts +++ b/scripts/ai-chat-turbo-engine/src/core/sidebar-peek.ts @@ -138,6 +138,16 @@ export const SIDEBAR_PEEK_ADOPT_MARGIN_PX = 32; */ export const SIDEBAR_PEEK_APPROACH_MARGIN_PX = 96; +/** + * How often the peek re-reads the panel a site owns while the pointer stays on + * it, and how many times it may press the expand control again. Sites that + * drive their own sidebar close it on hover-out; the departure into a menu + * they portaled to the body is one the peek refuses, so the site's collapse + * has to be undone while the pointer is still on the peek. + */ +export const SIDEBAR_PEEK_NATIVE_GUARD_MS = 120; +export const SIDEBAR_PEEK_NATIVE_GUARD_ATTEMPTS = 3; + /** How a candidate box is tied to the peek when it is adopted. */ interface AdoptPeekLayerOptions { /** Distance from the box the peek paints, in pixels. */ @@ -374,6 +384,9 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { let nativeOpenTimer: ReturnType | null = null; let nativeOpenChecks = 0; let nativeOpenSeq = 0; + /** Watches the panel the site owns while the pointer stays on the peek. */ + let nativeGuardTimer: ReturnType | null = null; + let nativeGuardAttempts = 0; /** When the native expand control was last pressed with a painted box. */ let nativeOpenPressedAt = Number.NEGATIVE_INFINITY; let transitionHoldTimer: ReturnType | null = null; @@ -758,11 +771,20 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { nativeOpenChecks = 0; } + function cancelNativeGuard(): void { + if (nativeGuardTimer !== null) { + clearTimeout(nativeGuardTimer); + nativeGuardTimer = null; + } + nativeGuardAttempts = 0; + } + function releaseActive(reason: ReleaseReason = "hover"): void { cancelHide(); cancelHold(); cancelOpen(); cancelNativeOpenCheck(); + cancelNativeGuard(); cancelReacquire(); cancelProbe(); cancelClickRelease(); @@ -1126,6 +1148,48 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { nativeOpenTimer = setTimeout(check, SIDEBAR_PEEK_NATIVE_OPEN_CHECK_MS); } + /** + * A site that opens its own sidebar also reserves the right to close it: its + * hover-close runs on the pointer leaving the rail, and the row menu it + * portals to the body is outside the rail, so walking onto that menu looks + * like a real departure to the page. The peek refuses that departure, which + * leaves the site's collapse as the only thing still shrinking the panel. + * The guard re-reads the site's own state while the pointer stays on the + * peek and presses the expand control again instead of letting the panel + * close under the cursor. + */ + function scheduleNativeGuard(rail: HTMLElement): void { + if (nativeGuardTimer !== null) { + clearTimeout(nativeGuardTimer); + nativeGuardTimer = null; + } + if (!nativeOnly || !expandedByPeek) return; + nativeGuardTimer = setTimeout(() => { + nativeGuardTimer = null; + if (!expandedByPeek || active === null) return; + // The pointer has to still be on the peek: a pointer that really left + // hands the panel back to the site rather than fighting its close. + if (!pointerStillOverRail(active)) return; + // A site's collapse re-lays the rail out under the pointer and can fire + // a synthetic pointerout while the cursor never moved. The peek's own + // grace must not swallow the re-expansion this check is about to run. + cancelHide(); + const current = resolveRail() ?? active; + // While the press that opened the panel is still being verified, a rail + // that reads closed is the site finishing its own render, not a close: + // pressing the toggle again there would shut the panel the peek wants. + if (nativePanelOpen(current) || nativeOpenTimer !== null) { + scheduleNativeGuard(current); + return; + } + if (nativeGuardAttempts >= SIDEBAR_PEEK_NATIVE_GUARD_ATTEMPTS) return; + nativeGuardAttempts += 1; + const target = rail.isConnected ? rail : current; + pressNativeExpand(target); + scheduleNativeGuard(target); + }, SIDEBAR_PEEK_NATIVE_GUARD_MS); + } + /** * Retries the expand press for a few frames. The peek is already open, so a * control that mounts with the site's next render still gets its press @@ -1655,6 +1719,7 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { active = node; rescanNeeded = true; reliftAttempts = 0; + nativeGuardAttempts = 0; // A hold left over from the previous release would mute the menus this // peek is about to open, and the rail is instant again while it peeks. finishTransitionHold(); @@ -1697,6 +1762,7 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { ); rememberBand(node.getBoundingClientRect()); } + if (nativeOnly && expandedByPeek) scheduleNativeGuard(node); const view = node.ownerDocument.defaultView; appliedHref = view?.location.href ?? ""; } @@ -2124,7 +2190,13 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { // A menu the page already had on screen is the page's own, and a wrapper // it closed is not the one the pointer is using. if (!vouched && !holds && isPreexistingPopover(layer)) continue; - if (!isVisiblePopover(layer, view)) continue; + // A box the pointer is inside is on screen by definition. Menus animate + // in, and the first frames of that fade read `opacity: 0`; a site that + // parks the box transparent and then makes it interactive reads the + // same. Only a box the pointer is not holding has to prove it is + // painted, or the peek would collapse in the one frame the menu spends + // becoming visible. + if (!holds && !isVisiblePopover(layer, view)) continue; if ( view.innerWidth > 0 && rect.width >= view.innerWidth * SIDEBAR_PEEK_ADOPT_MAX_WIDTH_RATIO @@ -2378,6 +2450,57 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { scheduleShow(rail); } + /** + * A site that drives its own sidebar also closes it when the pointer leaves + * the rail, and the row menu it portals to the body sits outside that + * subtree: walking onto the menu reads as a real departure to the page, which + * then collapses the panel the user is still using. The peek has already + * decided that this departure is still the peek, so the event must not reach + * the site's own hover handler. Capturing it at the window keeps it away from + * every listener the page registered below, and the peek's own bookkeeping + * happens here instead. + */ + function handleSiteDeparture(event: Event): void { + if (!isMousePointer(event)) return; + if (active === null || !expandedByPeek) return; + const target = event.target; + if (!(target instanceof Element)) return; + const rail = resolveRail() ?? active; + if (rail === null) return; + const doc = rail.ownerDocument; + // The page root is not a site hover-close: an event there is the pointer + // leaving the window, which the peek must see itself so the panel closes. + if (target === doc.documentElement || target === doc.body) return; + // A leave event does not bubble: the browser dispatches a second one to + // every ancestor the pointer quits, and a site that binds its hover-close + // to the shell holding the rail receives an event whose target is that + // shell, not the rail. Both shapes are departures the peek has already + // decided to keep, so both have to be refused here. + const insideRail = rail.contains(target); + const holdsRail = !insideRail && target.contains(rail); + if (!insideRail && !holdsRail) return; + // A leave that stays inside the panel belongs to the site: its row hover + // handlers must keep working. Only a departure into a box the peek + // claimed, or a null-target departure whose coordinates still sit on one, + // is the exit the site must not see. + const mouse = event as MouseEvent; + const related = mouse.relatedTarget; + const kept = + (related instanceof Element && + railFor(related) === null && + (peekLayerAt(related) !== null || + adoptPeekLayer(related, DEPARTURE_ADOPTION) !== null)) || + (!(related instanceof Element) && + withinPeekLayers( + mouse.clientX, + mouse.clientY, + SIDEBAR_PEEK_APPROACH_MARGIN_PX, + )); + if (!kept) return; + cancelHide(); + event.stopPropagation(); + } + function handlePointerOut(event: Event): void { if (!isMousePointer(event)) return; pointerX = (event as MouseEvent).clientX; @@ -2670,6 +2793,12 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { doc.addEventListener("click", handleClick, true); const win = doc.defaultView; if (win !== null) { + // Captured above every listener the page owns, so a departure the peek + // keeps never reaches the site's own hover-close. + win.addEventListener("pointerout", handleSiteDeparture, true); + win.addEventListener("mouseout", handleSiteDeparture, true); + win.addEventListener("pointerleave", handleSiteDeparture, true); + win.addEventListener("mouseleave", handleSiteDeparture, true); win.addEventListener("popstate", handleNavigation); win.addEventListener("hashchange", handleNavigation); } @@ -2710,6 +2839,10 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek { boundDocument.removeEventListener("click", handleClick, true); const win = boundDocument.defaultView; if (win !== null) { + win.removeEventListener("pointerout", handleSiteDeparture, true); + win.removeEventListener("mouseout", handleSiteDeparture, true); + win.removeEventListener("pointerleave", handleSiteDeparture, true); + win.removeEventListener("mouseleave", handleSiteDeparture, true); win.removeEventListener("popstate", handleNavigation); win.removeEventListener("hashchange", handleNavigation); } diff --git a/tests/ai-chat-turbo-sidebar-peek.test.ts b/tests/ai-chat-turbo-sidebar-peek.test.ts index c8a48cd..d64a3ea 100644 --- a/tests/ai-chat-turbo-sidebar-peek.test.ts +++ b/tests/ai-chat-turbo-sidebar-peek.test.ts @@ -7,6 +7,7 @@ import { SIDEBAR_PEEK_HOLD_ATTRIBUTE, SIDEBAR_PEEK_KEYBOARD_FOCUS_MS, SIDEBAR_PEEK_NAVIGATION_COLLAPSE_MS, + SIDEBAR_PEEK_NATIVE_GUARD_MS, SIDEBAR_PEEK_NATIVE_OPEN_CHECK_MS, SIDEBAR_PEEK_NATIVE_OPEN_GRACE_MS, SIDEBAR_PEEK_OPEN_ATTEMPTS, @@ -1033,6 +1034,42 @@ describe("sidebar hover peek", () => { } }); + /** + * Menus animate in. The frame the pointer lands on one, its computed opacity + * is still the `0` it started from, and the box under the cursor is the menu + * all the same. Visibility is evidence for a box the pointer is only heading + * into, never for the box it is resting on. + */ + it("adopts a menu in its transparent first frame when it holds the pointer", () => { + const instance = start(); + hoverButton(); + vi.advanceTimersByTime(SIDEBAR_PEEK_SHOW_DELAY_MS); + + const row = document.getElementById("history"); + if (!(row instanceof HTMLElement)) throw new Error("missing history row"); + stubNodeRect(row, 248, 32, 0); + const menu = document.createElement("div"); + menu.setAttribute("role", "menu"); + menu.style.position = "absolute"; + menu.style.opacity = "0"; + stubNodeRect(menu, 180, 120, 300); + const entry = document.createElement("button"); + entry.textContent = "Rename"; + menu.appendChild(entry); + document.body.appendChild(menu); + const restore = stubHit(entry); + + try { + dispatchPointer("pointerover", row, null, "mouse", 40, 100); + dispatchPointer("pointerout", row, entry, "mouse", 300, 100); + vi.advanceTimersByTime(SIDEBAR_PEEK_HIDE_DELAY_MS); + expect(instance.isPeeking()).toBe(true); + } finally { + restore(); + menu.remove(); + } + }); + it("closes immediately on an outside press or the native toggle", () => { const instance = start(); hoverButton(); @@ -2783,6 +2820,120 @@ describe("sidebar peek native expansion", () => { restore(); } }); + + /** + * A site that expands through its own control keeps its hover-close on the + * shell holding the rail. Walking onto the history menu fires a non-bubbling + * leave from that shell, and the peek has to keep that event from reaching + * the site, or the site closes the panel out from under the cursor. + */ + it("keeps a site hover-close from seeing a departure onto a row menu", () => { + const { open } = paintsControls(); + open.addEventListener("click", () => stubRect("#rail", 248, 768)); + + peek = createSidebarPeek({ + sidebar: { + ...SIDEBAR, + nativeExpand: { expand: "#open-sidebar", collapse: "#close-sidebar" }, + }, + }); + peek.bind(document); + + hoverButton(); + vi.advanceTimersByTime(SIDEBAR_PEEK_SHOW_DELAY_MS); + expect(peek.isPeeking()).toBe(true); + + const app = document.getElementById("app"); + if (!(app instanceof HTMLElement)) throw new Error("missing app"); + const row = document.getElementById("history"); + if (!(row instanceof HTMLElement)) throw new Error("missing history row"); + stubNodeRect(row, 248, 32, 0); + const menu = document.createElement("div"); + menu.setAttribute("role", "menu"); + menu.style.position = "absolute"; + stubNodeRect(menu, 180, 120, 300); + const entry = document.createElement("button"); + entry.textContent = "Rename"; + menu.appendChild(entry); + document.body.appendChild(menu); + const restore = stubHit(entry); + + let siteDepartures = 0; + const watch = (event: Event): void => { + siteDepartures += 1; + }; + document.addEventListener("pointerout", watch); + document.addEventListener("mouseout", watch); + + try { + dispatchPointer("pointerout", row, entry, "mouse", 300, 100); + expect(siteDepartures).toBe(0); + vi.advanceTimersByTime(SIDEBAR_PEEK_HIDE_DELAY_MS); + expect(peek.isPeeking()).toBe(true); + } finally { + document.removeEventListener("pointerout", watch); + document.removeEventListener("mouseout", watch); + restore(); + menu.remove(); + } + }); + + /** + * A site may already have run its collapse before the pointer entered the + * menu. The pointer is still on the peek, so the guard presses the expand + * control again instead of letting the panel close beneath it. + */ + it("re-expands a native panel the site collapsed while the pointer stays", () => { + const { open } = paintsControls(); + const openSpy = vi.spyOn(open, "click"); + open.addEventListener("click", () => stubRect("#rail", 248, 768)); + + peek = createSidebarPeek({ + sidebar: { + ...SIDEBAR, + nativeExpand: { expand: "#open-sidebar", collapse: "#close-sidebar" }, + }, + }); + peek.bind(document); + // The guard starts before the menu exists, so its first tick must already + // be able to find the pointer on the rail; jsdom otherwise has no hit + // test at all and the guard dies before the site collapse arrives. + const restoreRailHit = stubHit(rail()); + + hoverButton(); + vi.advanceTimersByTime(SIDEBAR_PEEK_SHOW_DELAY_MS); + expect(openSpy).toHaveBeenCalledTimes(1); + expect(peek.isPeeking()).toBe(true); + // Let the open check confirm the panel before the guard is allowed to + // press again: it never re-presses while that verification is pending. + vi.advanceTimersByTime(SIDEBAR_PEEK_NATIVE_OPEN_CHECK_MS); + + const row = document.getElementById("history"); + if (!(row instanceof HTMLElement)) throw new Error("missing history row"); + const menu = document.createElement("div"); + menu.setAttribute("role", "menu"); + menu.style.position = "absolute"; + stubNodeRect(menu, 180, 120, 300); + const entry = document.createElement("button"); + menu.appendChild(entry); + document.body.appendChild(menu); + const restore = () => { + const restoreEntryHit = stubHit(entry); + restoreEntryHit(); + restoreRailHit(); + }; + + try { + dispatchPointer("pointerout", row, entry, "mouse", 300, 100); + stubRect("#rail", 52, 768); + vi.advanceTimersByTime(SIDEBAR_PEEK_NATIVE_GUARD_MS * 3); + expect(openSpy).toHaveBeenCalledTimes(2); + expect(peek.isPeeking()).toBe(true); + } finally { + restore(); + menu.remove(); + } + }); }); describe("sidebar peek instant release", () => { From 74ce5c6253b3fbbc399eb02e9ad3ea65f4f67aba Mon Sep 17 00:00:00 2001 From: Yoko Date: Fri, 4 Dec 2026 21:09:22 +0800 Subject: [PATCH 2/2] chore(ai-chat-turbo-engine): release 0.23.21 --- docs/ai-chat-turbo-engine.md | 12 +++++++++++- scripts/ai-chat-turbo-engine/meta.ts | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/ai-chat-turbo-engine.md b/docs/ai-chat-turbo-engine.md index f57ca66..0ddcc31 100644 --- a/docs/ai-chat-turbo-engine.md +++ b/docs/ai-chat-turbo-engine.md @@ -2,7 +2,7 @@ 面向长对话与重度使用的跨平台 AI 聊天增强用户脚本。在受支持的 AI 对话页面上叠加一层轻量增强,解决四类实际痛点:长会话渲染变卡、超长代码块挤占阅读空间、大段文本撑爆输入框、上下文难以迁移到新会话。 -当前版本:**0.23.20** +当前版本:**0.23.21** 许可证:**MIT** 运行方式:单文件用户脚本,无外部依赖,无网络请求,不申请任何 GM 权限。 @@ -524,6 +524,16 @@ pointerdown → mousedown → pointerup → mouseup → click ## 版本记录 +### 0.23.21 + +**继续修悬停探出在历史记录二级菜单上误收回:这次处理的是站点自己收起面板的路径。** + +- **问题在站点,不在探出的收回逻辑。** 前几版一直在调探出自身的收回判定,但真实站点把侧栏的 hover 收起绑在包住导轨的外壳上;指针移到导轨之外的行菜单时,站点先判定为离开并收起自己的面板。脚本再怎么否决自己的收回,也拦不住站点的这一次收起。 +- **在窗口捕获阶段拦截站点离开。** 指针离开导轨、但落点是被探出认领的菜单(或其交接走廊)时,脚本现在会在 `window` 的捕获阶段取消这次离开事件,让站点看不到这次「离开」。站点内部从一个行移到另一个行(离开目标仍在导轨内)不受影响,正常离开窗口也照常放行。 +- **站点已抢先收起时再展开。** 若站点仍先一步收起了面板,探出会每 **120ms** 检查一次站点是否已关闭;只要指针还停在探出上、且展开校验已完成,就再次按下站点自己的展开控件,最多重试 3 次,避免面板在光标下面消失。 +- **淡入首帧也认领。** 菜单淡入的第一帧常是 `opacity: 0`;只要指针的命中点落在这个框里,这一帧也直接认领,避免在菜单刚出现的瞬间被判定为离开。 +- **测试覆盖。** 新增「透明首帧但罩住指针的菜单被认领」「站点 hover 收起不把行菜单当成离开」「站点抢先收起后在指针仍停留时重新展开」三条用例;三条在上一版代码上全部失败,现在全部通过,原有探出与站点展开用例保持通过。 + ### 0.23.20 **继续修悬停探出在历史记录二级菜单上误收回:这次补的是上一版仍然漏掉的三种情况。** diff --git a/scripts/ai-chat-turbo-engine/meta.ts b/scripts/ai-chat-turbo-engine/meta.ts index 191eec8..28f6ea7 100644 --- a/scripts/ai-chat-turbo-engine/meta.ts +++ b/scripts/ai-chat-turbo-engine/meta.ts @@ -4,7 +4,7 @@ import { PLATFORM_MATCH_PATTERNS } from "./src/config"; const meta = { name: "AI Chat Turbo Engine", namespace: "https://cyojkoy.github.io/Payment/", - version: "0.23.20", + version: "0.23.21", description: "跨平台 AI 对话加速:常驻统计 + 完整消息提取 + YAML 交接 + 多平台适配 + 长代码折叠 + 自适应脱水 + 推理强度记忆 + 元宝原子工具记忆 + 新对话快捷键覆盖", author: "Yoko", updateURL: "https://cdn.jsdelivr.net/gh/CYoJkoY/Yoko-ScriptCat-Scripts@release/ai-chat-turbo-engine.user.js",