Skip to content

🐛 修复保存脚本改名后浏览器标签页标题未同步更新 - #1607

Merged
CodFrm merged 1 commit into
scriptscat:mainfrom
cyfung1031:claude/script-title-sync-bug-54f342
Jul 19, 2026
Merged

CodFrm merged 1 commit into
scriptscat:mainfrom
cyfung1031:claude/script-title-sync-bug-54f342

Conversation

@cyfung1031

Copy link
Copy Markdown
Collaborator

Checklist / 检查清单

  • Fixes 保存脚本改名后浏览器标签页标题未同步更新 / 已修复脚本编辑器保存改名后浏览器标签标题不更新的问题
  • Code reviewed by human / 代码通过人工检查
  • Changes tested / 已完成测试

背景

在脚本编辑器中修改脚本的 // @name 并保存后,编辑器内部的标签栏(EditorTabs)会立即显示新名称,但浏览器实际的标签页标题(document.title)仍停留在旧名称,直到用户切换到其他标签再切回来,或刷新页面,标题才会更新。

根因:src/pages/options/routes/ScriptEditor/index.tsx 中负责同步 document.title 的 useEffect 只依赖 state.activeUuid,仅在切换激活标签时重新计算标题;保存脚本后 activeUuid 本身不变,因此该 effect 不会重新执行。

本次改动

  • 将 activeTab 的 useMemo 提前声明,并新增 activeTabScriptName(已解析的 i18n 名称字符串)。
  • 标题同步的 useEffect 依赖数组由 [state.activeUuid] 改为 [state.activeUuid, activeTabScriptName],使其在保存改名后立即重新计算 document.title,同时避免了直接依赖整个 state.tabs 数组导致在每次按键编辑时都触发该 effect(因为 markChanged action 每次按键都会产出新的 tabs 数组引用)。

验证

  • npx eslint src/pages/options/routes/ScriptEditor/index.tsx — 无报错
  • npx tsc --noEmit -p . — 无新增报错(仅有与本次改动无关的 tests/mocks/network.ts 预置类型声明缺失)
  • 用户已在真实浏览器中人工测试确认:编辑脚本名称并保存后,浏览器标签页标题立即同步更新。

🤖 Generated with Claude Code

document.title 的更新只在切换标签(activeUuid 变化)时触发,保存后
即使脚本名已改变,当前激活标签的浏览器标题仍停留在旧名称,直到用户
切换标签或刷新页面。现改为依赖已解析的 i18n 名称,保存后立即同步。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@cyfung1031

Copy link
Copy Markdown
Collaborator Author

代码通过人工检查 & 用户已在真实浏览器中人工测试确认

@cyfung1031 cyfung1031 added the P1 🔥 重要但是不紧急的内容 label Jul 18, 2026
@cyfung1031 cyfung1031 added this to the 2026七月 Milestone milestone Jul 18, 2026
@CodFrm
CodFrm requested a review from Copilot July 19, 2026 00:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 修复了脚本编辑器中“保存后脚本改名但浏览器标签页标题(document.title)未立即同步更新”的问题,通过让标题同步逻辑在脚本名称变化时也能重新执行,提升编辑器标签与浏览器标签的一致性。

Changes:

  • 提前声明 activeTab 的 useMemo,并新增 activeTabScriptName 作为标题同步所需的派生值。
  • 将同步 URL/标题的 useEffect 依赖从仅监听 state.activeUuid 扩展为同时监听脚本名变化,避免依赖整个 tabs 导致频繁触发。
  • 移除后续重复声明的 activeTab,统一使用前置的 memo 值。

Comment on lines +151 to +153
const activeTab = useMemo(() => state.tabs.find((x) => x.uuid === state.activeUuid), [state.tabs, state.activeUuid]);
const activeTabScriptName = activeTab?.script && i18nName(activeTab.script);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18nName不可能为空

Comment on lines +154 to +162
// 激活标签或其脚本数据变化(如保存后改名) → 同步 URL + 标题
useEffect(() => {
if (!state.activeUuid) return;
if (params.uuid !== state.activeUuid) {
void navigate(`/script/editor/${state.activeUuid}`, { replace: true });
}
const tab = state.tabs.find((x) => x.uuid === state.activeUuid);
if (tab) document.title = `${i18nName(tab.script)} - Script Editor`;
if (activeTabScriptName) document.title = `${activeTabScriptName} - Script Editor`;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state.activeUuid]);
}, [state.activeUuid, activeTabScriptName]);
@CodFrm
CodFrm merged commit 05ee6bd into scriptscat:main Jul 19, 2026
9 of 10 checks passed
@cyfung1031
cyfung1031 deleted the claude/script-title-sync-bug-54f342 branch October 10, 2026 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 🔥 重要但是不紧急的内容

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants