Skip to content
This repository was archived by the owner on Apr 30, 2026. It is now read-only.

Commit 9123ef3

Browse files
committed
Refactor
- Add `chrome-extension-async` - Add `src/libs` - Add `lastFocusedWindowId` to record lst focused window - FIx debug popup issue
1 parent af2ded6 commit 9123ef3

4 files changed

Lines changed: 63 additions & 28 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"test": "yarn lint && yarn build"
1414
},
1515
"dependencies": {
16+
"chrome-extension-async": "^3.2.4",
1617
"jquery": "^3.2.1",
1718
"mobx": "^3.3.1",
1819
"mobx-react": "^4.3.3",

src/js/background.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import 'chrome-extension-async'
2+
import { getActiveTab, setLastFocusedWindowId } from 'libs'
3+
14
const getURL = ({ url }) => new window.URL(url)
25

36
const reloadTab = (tab) => chrome.tabs.reload(tab.id)
47

58
const methodMap = {
6-
getData: (message, { url }, sendResponse) => {
9+
getData: async (message, { url }, sendResponse) => {
710
const { host, protocol, origin } = url
8-
chrome.storage.sync.get(origin, (data) => {
9-
const customjs = data[origin]
10-
sendResponse({ customjs, host, protocol })
11-
})
11+
const data = await chrome.store.sync.get(origin)
12+
const customjs = data[origin]
13+
sendResponse({ customjs, host, protocol })
1214
},
1315
setData: (message, { url }) => chrome.storage.sync.set(
1416
{ [url.origin]: message.customjs }
@@ -17,32 +19,34 @@ const methodMap = {
1719
goTo: (message, { tab }) => chrome.tabs.update(tab.id, { url: message.link })
1820
}
1921

20-
const onMessage = (message, sender, sendResponse) => chrome.tabs.query(
21-
{ active: true, currentWindow: true },
22-
(tabs) => {
23-
if (tabs.length <= 0) {
24-
// TODO: handle this in UI
25-
throw new Error('No active tab! This is impossible')
26-
}
27-
const [ tab ] = tabs
28-
const url = getURL(tab)
29-
const { method, reload } = message
30-
31-
const func = methodMap[method]
32-
if (func && typeof func === 'function') {
33-
func(message, { tab, url }, sendResponse)
34-
} else {
35-
console.error(`Unknown method: ${method}`)
36-
sendResponse({ src: '', config: {} })
37-
}
38-
39-
if (reload) {
40-
reloadTab(tab)
41-
}
22+
const onMessage = async (message, sender, sendResponse) => {
23+
const tab = await getActiveTab()
24+
const url = getURL(tab)
25+
const { method, reload } = message
26+
27+
const func = methodMap[method]
28+
if (func && typeof func === 'function') {
29+
func(message, { tab, url }, sendResponse)
30+
} else {
31+
console.error(`Unknown method: ${method}`)
32+
sendResponse({ src: '', config: {} })
4233
}
43-
)
34+
35+
if (reload) {
36+
reloadTab(tab)
37+
}
38+
}
39+
40+
const onFocusChanged = (windowId) => {
41+
if (windowId < 0) {
42+
return
43+
}
44+
setLastFocusedWindowId(windowId)
45+
}
4446

4547
chrome.runtime.onMessage.addListener((...args) => {
4648
onMessage(...args)
4749
return true
4850
})
51+
52+
chrome.windows.onFocusChanged.addListener(onFocusChanged)

src/js/libs/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const setLastFocusedWindowId = (lastFocusedWindowId) => {
2+
chrome.storage.local.set({ lastFocusedWindowId })
3+
}
4+
5+
export const getLastFocusedWindowId = async () => {
6+
const { lastFocusedWindowId } = await chrome.storage.local.get({ lastFocusedWindowId: null })
7+
return lastFocusedWindowId
8+
}
9+
10+
const getQueryInfo = (windowId) => {
11+
if (windowId) {
12+
return { windowId }
13+
}
14+
return { currentWindow: true }
15+
}
16+
17+
export const getActiveTab = async () => {
18+
const windowId = await getLastFocusedWindowId()
19+
const queryInfo = getQueryInfo(windowId)
20+
const tabs = await chrome.tabs.query({ ...queryInfo, active: true })
21+
if (tabs.length <= 0) {
22+
// TODO: handle this in UI
23+
throw new Error('No active tab! This is impossible')
24+
}
25+
return tabs[0]
26+
}

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,10 @@ chownr@^1.0.1:
12731273
version "1.0.1"
12741274
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.0.1.tgz#e2a75042a9551908bebd25b8523d5f9769d79181"
12751275

1276+
chrome-extension-async@^3.2.4:
1277+
version "3.2.4"
1278+
resolved "https://registry.yarnpkg.com/chrome-extension-async/-/chrome-extension-async-3.2.4.tgz#c5b3e206688ca81c903b5e239ff3f9a73a29c47e"
1279+
12761280
cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3:
12771281
version "1.0.4"
12781282
resolved "https://registry.yarnpkg.com/cipher-base/-/cipher-base-1.0.4.tgz#8760e4ecc272f4c363532f926d874aae2c1397de"

0 commit comments

Comments
 (0)