-
Notifications
You must be signed in to change notification settings - Fork 990
refactor: detect DevTools page for each page #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| export function extractUrlLikeFromDevToolsTitle( | ||
| title: string, | ||
| ): string | undefined { | ||
| const match = title.match(new RegExp(`DevTools - (.*)`)); | ||
| return match?.[1] ?? undefined; | ||
| } | ||
|
|
||
| export function urlsEqual(url1: string, url2: string): boolean { | ||
| const normalizedUrl1 = normalizeUrl(url1); | ||
| const normalizedUrl2 = normalizeUrl(url2); | ||
| return normalizedUrl1 === normalizedUrl2; | ||
| } | ||
|
|
||
| /** | ||
| * For the sake of the MCP server, when we determine if two URLs are equal we | ||
| * remove some parts: | ||
| * | ||
| * 1. We do not care about the protocol. | ||
| * 2. We do not care about trailing slashes. | ||
| * 3. We do not care about "www". | ||
| * | ||
| * For example, if the user types "record a trace on foo.com", we would want to | ||
| * match a tab in the connected Chrome instance that is showing "www.foo.com/" | ||
| */ | ||
| function normalizeUrl(url: string): string { | ||
| let result = url.trim(); | ||
|
|
||
| // Remove protocols | ||
| if (result.startsWith('https://')) { | ||
| result = result.slice(8); | ||
| } else if (result.startsWith('http://')) { | ||
| result = result.slice(7); | ||
| } | ||
|
|
||
| // Remove 'www.'. This ensures that we find the right URL regardless of if the user adds `www` or not. | ||
| if (result.startsWith('www.')) { | ||
| result = result.slice(4); | ||
| } | ||
|
|
||
| // Remove trailing slash | ||
| if (result.endsWith('/')) { | ||
| result = result.slice(0, -1); | ||
| } | ||
|
|
||
| return result; | ||
| } |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| import assert from 'node:assert'; | ||
| import {describe, it} from 'node:test'; | ||
|
|
||
| import { | ||
| extractUrlLikeFromDevToolsTitle, | ||
| urlsEqual, | ||
| } from '../src/DevtoolsUtils.js'; | ||
|
|
||
| describe('extractUrlFromDevToolsTitle', () => { | ||
| it('deals with no trailing /', () => { | ||
| assert.strictEqual( | ||
| extractUrlLikeFromDevToolsTitle('DevTools - example.com'), | ||
| 'example.com', | ||
| ); | ||
| }); | ||
| it('deals with a trailing /', () => { | ||
| assert.strictEqual( | ||
| extractUrlLikeFromDevToolsTitle('DevTools - example.com/'), | ||
| 'example.com/', | ||
| ); | ||
| }); | ||
| it('deals with www', () => { | ||
| assert.strictEqual( | ||
| extractUrlLikeFromDevToolsTitle('DevTools - www.example.com/'), | ||
| 'www.example.com/', | ||
| ); | ||
| }); | ||
| it('deals with complex url', () => { | ||
| assert.strictEqual( | ||
| extractUrlLikeFromDevToolsTitle( | ||
| 'DevTools - www.example.com/path.html?a=b#3', | ||
| ), | ||
| 'www.example.com/path.html?a=b#3', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('urlsEqual', () => { | ||
| it('ignores trailing slashes', () => { | ||
| assert.strictEqual( | ||
| urlsEqual('https://google.com/', 'https://google.com'), | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('ignores www', () => { | ||
| assert.strictEqual( | ||
| urlsEqual('https://google.com/', 'https://www.google.com'), | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('ignores protocols', () => { | ||
| assert.strictEqual( | ||
| urlsEqual('https://google.com/', 'http://www.google.com'), | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('does not ignore other subdomains', () => { | ||
| assert.strictEqual( | ||
| urlsEqual('https://google.com/', 'https://photos.google.com'), | ||
| false, | ||
| ); | ||
| }); | ||
| }); |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.