-
Notifications
You must be signed in to change notification settings - Fork 0
fix: recognise the popup when a browser opens it in a tab #2
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
3 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
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 @@ | ||
| // @vitest-environment jsdom | ||
| import { afterEach, expect, it, vi } from 'vitest'; | ||
| import { readerTab } from '../src/popup/App'; | ||
|
|
||
| const POPUP = 'chrome-extension://extension-id/popup.html'; | ||
|
|
||
| /** `tabs.query` filtered the way Chrome filters it: `active`/`currentWindow` | ||
| * narrow the set, and an empty query matches every tab. */ | ||
| function mockTabs(tabs: { url?: string; active?: boolean; lastAccessed?: number }[]) { | ||
| vi.stubGlobal('chrome', { | ||
| runtime: { getURL: (path: string) => `chrome-extension://extension-id/${path}` }, | ||
| tabs: { | ||
| query: vi.fn(async (q: { active?: boolean }) => | ||
| q.active ? tabs.filter((tab) => tab.active) : tabs, | ||
| ), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| afterEach(() => vi.unstubAllGlobals()); | ||
|
|
||
| it('reads the active tab, which is the page a floating popup sits over', async () => { | ||
| mockTabs([{ url: 'https://x.com/home', active: true }]); | ||
| await expect(readerTab()).resolves.toBe('https://x.com/home'); | ||
| }); | ||
|
|
||
| it('reports an unrelated active tab rather than reaching for a background one', async () => { | ||
| // The reader is looking at Hacker News. Answering "x.com" because a tab is | ||
| // open there would show the X panel, and its thread toggle, over a thread | ||
| // they cannot see. Not selecting a site is the correct outcome here. | ||
| mockTabs([ | ||
| { url: 'https://news.ycombinator.com/', active: true }, | ||
| { url: 'https://x.com/home', lastAccessed: 10 }, | ||
| ]); | ||
| await expect(readerTab()).resolves.toBe('https://news.ycombinator.com/'); | ||
| }); | ||
|
|
||
| it('looks past the popup when the popup is itself the active tab', async () => { | ||
| // Kiwi on Android: nowhere to float a panel, so popup.html occupies a tab. | ||
| mockTabs([ | ||
| { url: POPUP, active: true }, | ||
| { url: 'https://x.com/home', lastAccessed: 10 }, | ||
| ]); | ||
| await expect(readerTab()).resolves.toBe('https://x.com/home'); | ||
| }); | ||
|
|
||
| it('prefers the most recently touched supported tab, and ignores the rest', async () => { | ||
| mockTabs([ | ||
| { url: POPUP, active: true }, | ||
| { url: 'https://x.com/home', lastAccessed: 10 }, | ||
| { url: 'https://news.ycombinator.com/', lastAccessed: 99 }, | ||
| { url: 'https://www.youtube.com/watch?v=1', lastAccessed: 42 }, | ||
| ]); | ||
| await expect(readerTab()).resolves.toBe('https://www.youtube.com/watch?v=1'); | ||
| }); | ||
|
|
||
| it('answers with nothing when the popup is alone and no supported tab is open', async () => { | ||
| mockTabs([ | ||
| { url: POPUP, active: true }, | ||
| { url: 'https://news.ycombinator.com/', lastAccessed: 99 }, | ||
| ]); | ||
| await expect(readerTab()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('is not fooled by an extension ID that ours is a prefix of', async () => { | ||
| mockTabs([ | ||
| { url: 'chrome-extension://extension-id-other/popup.html', active: true }, | ||
| { url: 'https://x.com/home', lastAccessed: 10 }, | ||
| ]); | ||
| await expect(readerTab()).resolves.toBe('chrome-extension://extension-id-other/popup.html'); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Restrict the active-tab fast path to supported sites. Bruce found the smoking gun:
readerTabreturns any active non-extension URL before it checkslastAccessed. If an unrelated site is active when Sharp opens,Appreceives that URL, does not select a site, and skips the recent supported-tab fallback. Return the active URL only when it matchesfiltered, then select the most recently accessed supported tab.🤖 Prompt for AI Agents