豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
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
3 changes: 2 additions & 1 deletion docs/tool-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@

**Parameters:**

- **pageIdx** (number) **(required)**: The index of the page to select. Call [`list_pages`](#list_pages) to list pages.
- **bringToFront** (boolean) _(optional)_: Whether to focus the page and bring it to the top.
- **pageIdx** (number) **(required)**: The index of the page to select. Call [`list_pages`](#list_pages) to get available pages.

---

Expand Down
6 changes: 6 additions & 0 deletions src/McpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,16 @@ export class McpContext implements Context {
const oldPage = this.#selectedPage;
if (oldPage) {
oldPage.off('dialog', this.#dialogHandler);
void oldPage.emulateFocusedPage(false).catch(error => {
this.logger('Error turning off focused page emulation', error);
});
}
this.#selectedPage = newPage;
newPage.on('dialog', this.#dialogHandler);
this.#updateSelectedPageTimeouts();
void newPage.emulateFocusedPage(true).catch(error => {
this.logger('Error turning on focused page emulation', error);
});
}

#updateSelectedPageTimeouts() {
Expand Down
10 changes: 8 additions & 2 deletions src/tools/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ export const selectPage = defineTool({
pageIdx: zod
.number()
.describe(
'The index of the page to select. Call list_pages to list pages.',
`The index of the page to select. Call ${listPages.name} to get available pages.`,
),
bringToFront: zod
.boolean()
.optional()
.describe('Whether to focus the page and bring it to the top.'),
},
handler: async (request, response, context) => {
const page = context.getPageByIdx(request.params.pageIdx);
await page.bringToFront();
context.selectPage(page);
response.setIncludePages(true);
if (request.params.bringToFront) {
await page.bringToFront();
}
},
});

Expand Down
17 changes: 17 additions & 0 deletions tests/tools/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ describe('pages', () => {
assert.ok(response.includePages);
});
});
it('selects a page and keeps it focused in the background', async () => {
await withMcpContext(async (response, context) => {
await context.newPage();
assert.strictEqual(context.getPageByIdx(1), context.getSelectedPage());
assert.strictEqual(
await context.getPageByIdx(0).evaluate(() => document.hasFocus()),
false,
);
await selectPage.handler({params: {pageIdx: 0}}, response, context);
assert.strictEqual(context.getPageByIdx(0), context.getSelectedPage());
assert.strictEqual(
await context.getPageByIdx(0).evaluate(() => document.hasFocus()),
true,
);
assert.ok(response.includePages);
});
});
});
describe('navigate_page', () => {
it('navigates to correct page', async () => {
Expand Down