Skip to content

Commit 775dfe6

Browse files
committed
feat: jump to existing app tab from popup page (sheepzh#653)
1 parent be0e9ff commit 775dfe6

File tree

3 files changed

+58
-42
lines changed

3 files changed

+58
-42
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@
2828
},
2929
"license": "MIT",
3030
"devDependencies": {
31-
"@crowdin/crowdin-api-client": "^1.49.0",
31+
"@crowdin/crowdin-api-client": "^1.51.1",
3232
"@emotion/babel-plugin": "^11.13.5",
3333
"@emotion/css": "^11.13.5",
34-
"@rsdoctor/rspack-plugin": "^1.3.12",
35-
"@rspack/cli": "^1.6.6",
36-
"@rspack/core": "^1.6.6",
37-
"@swc/core": "^1.15.3",
34+
"@rsdoctor/rspack-plugin": "^1.3.16",
35+
"@rspack/cli": "^1.6.8",
36+
"@rspack/core": "^1.6.8",
37+
"@swc/core": "^1.15.7",
3838
"@swc/jest": "^0.2.39",
3939
"@types/chrome": "0.1.32",
4040
"@types/decompress": "^4.2.7",
4141
"@types/jest": "^30.0.0",
42-
"@types/node": "^24.10.1",
42+
"@types/node": "^25.0.3",
4343
"@types/punycode": "^2.1.4",
4444
"@vue/babel-plugin-jsx": "^2.0.1",
4545
"babel-loader": "^10.0.0",
@@ -54,7 +54,7 @@
5454
"postcss": "^8.5.6",
5555
"postcss-loader": "^8.2.0",
5656
"postcss-rtlcss": "^5.7.1",
57-
"puppeteer": "^24.32.0",
57+
"puppeteer": "^24.34.0",
5858
"ts-loader": "^9.5.4",
5959
"ts-node": "^10.9.2",
6060
"tsconfig-paths": "^4.2.0",
@@ -63,10 +63,10 @@
6363
"dependencies": {
6464
"@element-plus/icons-vue": "^2.3.2",
6565
"echarts": "^6.0.0",
66-
"element-plus": "2.12.0",
66+
"element-plus": "2.13.0",
6767
"punycode": "^2.3.1",
68-
"vue": "^3.5.25",
69-
"vue-router": "^4.6.3"
68+
"vue": "^3.5.26",
69+
"vue-router": "^4.6.4"
7070
},
7171
"engines": {
7272
"node": ">=22"

src/api/chrome/tab.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* https://opensource.org/licenses/MIT
66
*/
77

8+
import { IS_MV3 } from '@util/constant/environment'
89
import { handleError } from "./common"
910

1011
export function getTab(id: number): Promise<ChromeTab | undefined> {
@@ -122,4 +123,16 @@ export function onTabUpdated(handler: TabHandler<ChromeTabUpdatedInfo>): void {
122123
handleError("tabUpdated")
123124
handler(tabId, changeInfo, tab)
124125
})
126+
}
127+
128+
export function updateTab(tabId: number, updateProperties: chrome.tabs.UpdateProperties): Promise<ChromeTab | undefined> {
129+
if (IS_MV3) {
130+
return chrome.tabs.update(tabId, updateProperties)
131+
}
132+
return new Promise((resolve) => {
133+
chrome.tabs.update(tabId, updateProperties, (tab) => {
134+
handleError("updateTab")
135+
resolve(tab)
136+
})
137+
})
125138
}
Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
1-
import { createTab } from "@api/chrome/tab"
1+
import { createTab, listTabs, updateTab } from "@api/chrome/tab"
22
import { View } from "@element-plus/icons-vue"
33
import Flex from "@pages/components/Flex"
44
import LangSelect from "@popup/components/Header/LangSelect"
55
import { t } from "@popup/locale"
66
import { IS_ANDROID } from "@util/constant/environment"
77
import { getAppPageUrl } from "@util/constant/url"
88
import { ElLink } from "element-plus"
9-
import { defineComponent } from "vue"
9+
import { FunctionalComponent } from "vue"
1010
import DarkSwitch from "./DarkSwitch"
1111
import Github from "./Github"
1212
import Logo from "./Logo"
1313
import Option from "./Option"
1414
import RateUs from './RateUs'
1515

16-
const Header = defineComponent<{}>(() => {
17-
const handleAllFuncClick = () => {
18-
const appPageUrl = getAppPageUrl()
19-
if (IS_ANDROID) {
20-
location.replace(appPageUrl)
21-
} else {
22-
createTab(appPageUrl)
16+
const openAppPage = async () => {
17+
const appPageUrl = getAppPageUrl()
18+
if (IS_ANDROID) return location.replace(appPageUrl)
19+
try {
20+
const tabs = await listTabs({ currentWindow: true })
21+
// If there are non-highlighted app page tab, jump to it
22+
const tabId2Jump = tabs.find(tab => !!tab.url?.startsWith(appPageUrl) && !tab.highlighted)?.id
23+
if (tabId2Jump) {
24+
await updateTab(tabId2Jump, { highlighted: true })
25+
return
2326
}
27+
} catch (ignored) {
2428
}
29+
// fall back to open new tab
30+
await createTab(appPageUrl)
31+
}
2532

26-
return () => (
27-
<Flex justify="space-between" padding='0 10px' color='text-primary'>
28-
<Logo />
33+
const Header: FunctionalComponent = () => (
34+
<Flex justify="space-between" padding='0 10px' color='text-primary'>
35+
<Logo />
36+
<Flex gap={10}>
2937
<Flex gap={10}>
30-
<Flex gap={10}>
31-
<RateUs />
32-
<ElLink
33-
underline="never"
34-
onClick={handleAllFuncClick}
35-
icon={View}
36-
style={{ gap: '3px' }}
37-
>
38-
{t(msg => msg.base.allFunction)}
39-
</ElLink>
40-
</Flex>
41-
<Flex align="center" gap={8} style={{ fontSize: '30px' }}>
42-
<LangSelect />
43-
<DarkSwitch />
44-
<Option />
45-
<Github />
46-
</Flex>
38+
<RateUs />
39+
<ElLink underline="never" onClick={openAppPage} icon={View} style={{ gap: '3px' }}>
40+
{t(msg => msg.base.allFunction)}
41+
</ElLink>
4742
</Flex>
48-
</Flex >
49-
)
50-
})
43+
<Flex align="center" gap={8} fontSize={30}>
44+
<LangSelect />
45+
<DarkSwitch />
46+
<Option />
47+
<Github />
48+
</Flex>
49+
</Flex>
50+
</Flex>
51+
)
52+
53+
Header.displayName = "PopupHeader"
5154

5255
export default Header

0 commit comments

Comments
 (0)