-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.ts
More file actions
112 lines (107 loc) · 3.08 KB
/
background.ts
File metadata and controls
112 lines (107 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import type { CommentEvent, CommentSpot } from '@/lib/enhancer'
import { type DraftStats, statsFor } from '@/lib/enhancers/draft-stats'
import type { GetTableRowsResponse, ToBackgroundMessage } from '@/lib/messages'
import {
CLOSE_MESSAGE_PORT,
isContentToBackgroundMessage,
isGetOpenSpotsMessage,
isSwitchToTabMessage,
KEEP_PORT_OPEN,
} from '@/lib/messages'
export interface Tab {
tabId: number
windowId: number
}
export interface CommentStorage {
tab: Tab
spot: CommentSpot
drafts: [number, string][]
sentOn: number | null
trashedOn: number | null
}
interface Draft {
content: string
time: number
stats: DraftStats
}
export interface CommentTableRow {
spot: CommentSpot
latestDraft: Draft
isOpenTab: boolean
isSent: boolean
isTrashed: boolean
}
export const openSpots = new Map<string, CommentStorage>()
export function handleCommentEvent(message: CommentEvent, sender: any): boolean {
if (
(message.type === 'ENHANCED' || message.type === 'DESTROYED') &&
sender.tab?.id &&
sender.tab?.windowId
) {
if (message.type === 'ENHANCED') {
const commentState: CommentStorage = {
drafts: [],
sentOn: null,
spot: message.spot,
tab: {
tabId: sender.tab.id,
windowId: sender.tab.windowId,
},
trashedOn: null,
}
openSpots.set(message.spot.unique_key, commentState)
} else if (message.type === 'DESTROYED') {
openSpots.delete(message.spot.unique_key)
} else {
throw new Error(`Unhandled comment event type: ${message.type}`)
}
}
return CLOSE_MESSAGE_PORT
}
export function handlePopupMessage(
message: any,
_sender: any,
sendResponse: (response: any) => void,
): typeof CLOSE_MESSAGE_PORT | typeof KEEP_PORT_OPEN {
if (isGetOpenSpotsMessage(message)) {
const rows: CommentTableRow[] = Array.from(openSpots.values()).map((storage) => {
const [time, content] = storage.drafts.at(-1)!
const row: CommentTableRow = {
isOpenTab: true,
isSent: storage.sentOn != null,
isTrashed: storage.trashedOn != null,
latestDraft: {
content,
stats: statsFor(content),
time,
},
spot: storage.spot,
}
return row
})
const response: GetTableRowsResponse = { rows }
sendResponse(response)
return KEEP_PORT_OPEN
} else if (isSwitchToTabMessage(message)) {
browser.windows
.update(message.windowId, { focused: true })
.then(() => {
return browser.tabs.update(message.tabId, { active: true })
})
.catch((error) => {
console.error('Error switching to tab:', error)
})
return CLOSE_MESSAGE_PORT
} else {
throw new Error(`Unhandled popup message type: ${message?.type || 'unknown'}`)
}
}
export default defineBackground(() => {
browser.runtime.onMessage.addListener((message: ToBackgroundMessage, sender, sendResponse) => {
if (isContentToBackgroundMessage(message)) {
return handleCommentEvent(message, sender)
} else {
return handlePopupMessage(message, sender, sendResponse)
}
})
})