Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions entrypoints/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default defineContentScript({
matches: ['*://*/*'],

registration: 'manifest',

main() {
// Receive data transmitted from the tab page being operated
// And send it to the devTools page for rendering
browser.runtime.onMessage.addListener((data) => {
browser.runtime.sendMessage(data)
})
},
})
32 changes: 28 additions & 4 deletions entrypoints/devtools-panel/hooks/useDevToolsPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CssDiffsType, SelectedElType } from '../types'
import { useClipboard } from '@vueuse/core'
import { ElMessage } from 'element-plus'
import { useI18n } from 'vue-i18n'
import SM from '../message'
import { formatStyle, type FormatStyleValue } from '../utils'

export function useDevToolsPanel() {
Expand Down Expand Up @@ -31,20 +32,40 @@ export function useDevToolsPanel() {
`(${formatStyle.toString()})($0)`,
(result: FormatStyleValue, isException) => {
if (!isException && result != null && isLoadComplete.value) {
saveSelectedEl(result)
const valueType = !selectedEl.length ? 'left' : 'right'

saveSelectedEl({ ...result, valueType })
}
},
)
})

// Receive the <selected element> transmitted from the tab being operated
browser.runtime.onMessage.addListener((data: Array<SelectedElType>) => {
selectedEl.length = 0

if (!data.length) {
cssDiffs.length = 0
return
}

selectedEl.push(...data)

if (selectedEl.length === 2) {
compareSelectedEl()
}
})
})

function saveSelectedEl(result: FormatStyleValue) {
function saveSelectedEl(result: SelectedElType) {
if (selectedEl.length === 2) {
return
}

const valueType = !selectedEl.length ? 'left' : 'right'
selectedEl.push({ ...result, valueType })
selectedEl.push(result)

// Send selected data to other windows/tabs
SM.send(selectedEl)

if (selectedEl.length === 2) {
compareSelectedEl()
Expand All @@ -54,6 +75,9 @@ export function useDevToolsPanel() {
function handleClearSelection() {
selectedEl.length = 0
cssDiffs.length = 0

// Send selected data to other windows/tabs
SM.send([])
}

function compareSelectedEl() {
Expand Down
38 changes: 38 additions & 0 deletions entrypoints/devtools-panel/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { SelectedElType } from './types'

interface _Window extends chrome.windows.Window {
tabs: Array<chrome.tabs.Tab>
}

class SendMessage {
private currentTabId: number | undefined
constructor() {
this.init()
}

private async init() {
const [currentTab] = await browser.tabs.query({ active: true })

this.currentTabId = currentTab.id!
}

public async send(data: Array<SelectedElType>) {
const windows = await browser.windows.getAll({ populate: true });

(windows as unknown as Array<_Window>).forEach((window) => {
for (const tab of window.tabs) {
if (tab.id !== this.currentTabId) {
// Send selected data to other windows/tabs
browser.tabs.sendMessage(
tab.id!,
data,
)
}
}
})
}
}

const SM = new SendMessage()

export default SM
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
"scripts": {
"dev": "wxt",
"dev:firefox": "wxt -b firefox",
"build": "wxt build",
"dev:edge": "wxt -b edge",
"build": "pnpm run --filter . --parallel \"/^build:.*/\"",
"build:chrome": "wxt build -b chrome",
"build:firefox": "wxt build -b firefox",
"zip": "wxt zip",
"zip:firefox": "wxt zip -b firefox",
"build:edge": "wxt build -b edge",
"zip": "pnpm run --filter . --parallel \"/^zip:.*/\"",
"zip:chrome": "wxt zip -b chrome",
"zip:firefox": "wxt zip -b firefox --mv3",
"zip:edge": "wxt zip -b edge",
"compile": "vue-tsc --noEmit",
"postinstall": "wxt prepare"
},
Expand Down
7 changes: 5 additions & 2 deletions wxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { defineConfig } from 'wxt';
import { defineConfig } from 'wxt'

// See https://wxt.dev/api/config.html
export default defineConfig({
extensionApi: 'chrome',
manifest: {
permissions: ['nativeMessaging', 'tabs', 'activeTab', 'scripting'],
},
modules: ['@wxt-dev/module-vue'],
});
})