Skip to content

Commit f09b73c

Browse files
authored
feat: support window/tab teleport data (#3)
* feat: support window/tab teleport data * chore: update version
1 parent 76cb5f2 commit f09b73c

File tree

5 files changed

+92
-9
lines changed

5 files changed

+92
-9
lines changed

entrypoints/content.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default defineContentScript({
2+
matches: ['*://*/*'],
3+
4+
registration: 'manifest',
5+
6+
main() {
7+
// Receive data transmitted from the tab page being operated
8+
// And send it to the devTools page for rendering
9+
browser.runtime.onMessage.addListener((data) => {
10+
browser.runtime.sendMessage(data)
11+
})
12+
},
13+
})

entrypoints/devtools-panel/hooks/useDevToolsPanel.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { CssDiffsType, SelectedElType } from '../types'
22
import { useClipboard } from '@vueuse/core'
33
import { ElMessage } from 'element-plus'
44
import { useI18n } from 'vue-i18n'
5+
import SM from '../message'
56
import { formatStyle, type FormatStyleValue } from '../utils'
67

78
export function useDevToolsPanel() {
@@ -31,20 +32,40 @@ export function useDevToolsPanel() {
3132
`(${formatStyle.toString()})($0)`,
3233
(result: FormatStyleValue, isException) => {
3334
if (!isException && result != null && isLoadComplete.value) {
34-
saveSelectedEl(result)
35+
const valueType = !selectedEl.length ? 'left' : 'right'
36+
37+
saveSelectedEl({ ...result, valueType })
3538
}
3639
},
3740
)
3841
})
42+
43+
// Receive the <selected element> transmitted from the tab being operated
44+
browser.runtime.onMessage.addListener((data: Array<SelectedElType>) => {
45+
selectedEl.length = 0
46+
47+
if (!data.length) {
48+
cssDiffs.length = 0
49+
return
50+
}
51+
52+
selectedEl.push(...data)
53+
54+
if (selectedEl.length === 2) {
55+
compareSelectedEl()
56+
}
57+
})
3958
})
4059

41-
function saveSelectedEl(result: FormatStyleValue) {
60+
function saveSelectedEl(result: SelectedElType) {
4261
if (selectedEl.length === 2) {
4362
return
4463
}
4564

46-
const valueType = !selectedEl.length ? 'left' : 'right'
47-
selectedEl.push({ ...result, valueType })
65+
selectedEl.push(result)
66+
67+
// Send selected data to other windows/tabs
68+
SM.send(selectedEl)
4869

4970
if (selectedEl.length === 2) {
5071
compareSelectedEl()
@@ -54,6 +75,9 @@ export function useDevToolsPanel() {
5475
function handleClearSelection() {
5576
selectedEl.length = 0
5677
cssDiffs.length = 0
78+
79+
// Send selected data to other windows/tabs
80+
SM.send([])
5781
}
5882

5983
function compareSelectedEl() {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { SelectedElType } from './types'
2+
3+
interface _Window extends chrome.windows.Window {
4+
tabs: Array<chrome.tabs.Tab>
5+
}
6+
7+
class SendMessage {
8+
private currentTabId: number | undefined
9+
constructor() {
10+
this.init()
11+
}
12+
13+
private async init() {
14+
const [currentTab] = await browser.tabs.query({ active: true })
15+
16+
this.currentTabId = currentTab.id!
17+
}
18+
19+
public async send(data: Array<SelectedElType>) {
20+
const windows = await browser.windows.getAll({ populate: true });
21+
22+
(windows as unknown as Array<_Window>).forEach((window) => {
23+
for (const tab of window.tabs) {
24+
if (tab.id !== this.currentTabId) {
25+
// Send selected data to other windows/tabs
26+
browser.tabs.sendMessage(
27+
tab.id!,
28+
data,
29+
)
30+
}
31+
}
32+
})
33+
}
34+
}
35+
36+
const SM = new SendMessage()
37+
38+
export default SM

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
"scripts": {
99
"dev": "wxt",
1010
"dev:firefox": "wxt -b firefox",
11-
"build": "wxt build",
11+
"dev:edge": "wxt -b edge",
12+
"build": "pnpm run --filter . --parallel \"/^build:.*/\"",
13+
"build:chrome": "wxt build -b chrome",
1214
"build:firefox": "wxt build -b firefox",
13-
"zip": "wxt zip",
14-
"zip:firefox": "wxt zip -b firefox",
15+
"build:edge": "wxt build -b edge",
16+
"zip": "pnpm run --filter . --parallel \"/^zip:.*/\"",
17+
"zip:chrome": "wxt zip -b chrome",
18+
"zip:firefox": "wxt zip -b firefox --mv3",
19+
"zip:edge": "wxt zip -b edge",
1520
"compile": "vue-tsc --noEmit",
1621
"postinstall": "wxt prepare"
1722
},

wxt.config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { defineConfig } from 'wxt';
1+
import { defineConfig } from 'wxt'
22

33
// See https://wxt.dev/api/config.html
44
export default defineConfig({
55
extensionApi: 'chrome',
6+
manifest: {
7+
permissions: ['nativeMessaging', 'tabs', 'activeTab', 'scripting'],
8+
},
69
modules: ['@wxt-dev/module-vue'],
7-
});
10+
})

0 commit comments

Comments
 (0)