Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion docs/ai-chat-turbo-engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

面向长对话与重度使用的跨平台 AI 聊天增强用户脚本。在受支持的 AI 对话页面上叠加一层轻量增强,解决四类实际痛点:长会话渲染变卡、超长代码块挤占阅读空间、大段文本撑爆输入框、上下文难以迁移到新会话。

当前版本:**0.23.20**
当前版本:**0.23.21**
许可证:**MIT**
运行方式:单文件用户脚本,无外部依赖,无网络请求,不申请任何 GM 权限。

Expand Down Expand Up @@ -524,6 +524,16 @@ pointerdown → mousedown → pointerup → mouseup → click

## 版本记录

### 0.23.21

**继续修悬停探出在历史记录二级菜单上误收回:这次处理的是站点自己收起面板的路径。**

- **问题在站点,不在探出的收回逻辑。** 前几版一直在调探出自身的收回判定,但真实站点把侧栏的 hover 收起绑在包住导轨的外壳上;指针移到导轨之外的行菜单时,站点先判定为离开并收起自己的面板。脚本再怎么否决自己的收回,也拦不住站点的这一次收起。
- **在窗口捕获阶段拦截站点离开。** 指针离开导轨、但落点是被探出认领的菜单(或其交接走廊)时,脚本现在会在 `window` 的捕获阶段取消这次离开事件,让站点看不到这次「离开」。站点内部从一个行移到另一个行(离开目标仍在导轨内)不受影响,正常离开窗口也照常放行。
- **站点已抢先收起时再展开。** 若站点仍先一步收起了面板,探出会每 **120ms** 检查一次站点是否已关闭;只要指针还停在探出上、且展开校验已完成,就再次按下站点自己的展开控件,最多重试 3 次,避免面板在光标下面消失。
- **淡入首帧也认领。** 菜单淡入的第一帧常是 `opacity: 0`;只要指针的命中点落在这个框里,这一帧也直接认领,避免在菜单刚出现的瞬间被判定为离开。
- **测试覆盖。** 新增「透明首帧但罩住指针的菜单被认领」「站点 hover 收起不把行菜单当成离开」「站点抢先收起后在指针仍停留时重新展开」三条用例;三条在上一版代码上全部失败,现在全部通过,原有探出与站点展开用例保持通过。

### 0.23.20

**继续修悬停探出在历史记录二级菜单上误收回:这次补的是上一版仍然漏掉的三种情况。**
Expand Down
2 changes: 1 addition & 1 deletion scripts/ai-chat-turbo-engine/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
135 changes: 134 additions & 1 deletion scripts/ai-chat-turbo-engine/src/core/sidebar-peek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -374,6 +384,9 @@ export function createSidebarPeek(options: SidebarPeekOptions): SidebarPeek {
let nativeOpenTimer: ReturnType<typeof setTimeout> | null = null;
let nativeOpenChecks = 0;
let nativeOpenSeq = 0;
/** Watches the panel the site owns while the pointer stays on the peek. */
let nativeGuardTimer: ReturnType<typeof setTimeout> | 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<typeof setTimeout> | null = null;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 ?? "";
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Loading
Loading