🐛 修复 window.focus 窗口聚焦并加强 windowId 校验#1577
Open
cyfung1031 wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist / 检查清单
背景
Tampermonkey 的
window.focus()会激活脚本所在标签页,并将其所在的浏览器窗口置于前台。ScriptCat 原实现只激活标签页;当目标窗口处于后台时,浏览器窗口不会被带到前台,因此与 Tampermonkey 的行为不兼容。另外,部分标签页相关流程可能取得无效的
windowId(例如负数窗口 ID)。若直接传给窗口聚焦或截图 API,可能触发异常或产生不明确的行为。Fixes #1571
本次改动
window.focus()GM APIchrome.tabs.update(tabId, { active: true }),激活脚本所在标签页;windowId为有限且非负的有效值时,调用chrome.windows.update(windowId, { focused: true }),将标签页所在窗口置于前台;windowId不再传入chrome.windows.update()。Agent 标签页与截图流程
tab_tools.ts:聚焦标签页所在窗口前,增加有限且非负的windowId校验;dom.ts:调用chrome.tabs.captureVisibleTab()前校验标签页和windowId,无效时抛出明确错误。测试
window.focus()回归测试,验证标签页激活和浏览器窗口聚焦都会执行。根因
原实现只调用
chrome.tabs.update(tabId, { active: true })。激活标签页不会自动将其所在的后台浏览器窗口置于前台,因此缺少窗口级聚焦调用。同时,仅依赖 truthy 或有限数值判断不足以排除负数窗口 ID,需要统一要求
windowId为有限且非负的值。验证记录
pnpm exec vitest run src/app/service/service_worker/gm_api/gm_api.test.ts --reporter=default— 23 tests passedpnpm run typecheck— passedpnpm exec prettier --check src/app/service/service_worker/gm_api/gm_api.ts src/app/service/service_worker/gm_api/gm_api.test.ts— passedpnpm exec eslint src/app/service/service_worker/gm_api/gm_api.ts src/app/service/service_worker/gm_api/gm_api.test.ts— passedgit diff --check— passedScreenshots / 截图
Not applicable.