-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.ts
273 lines (239 loc) · 8.5 KB
/
background.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { Storage } from "@plasmohq/storage"
import { fetchRoomData } from "~utils/platforms"
const storage = new Storage()
const ALARM_NAME = "checkLiveStatus"
// 添加错误处理函数
function handleError(error: Error) {
if (error.message === "Extension context invalidated.") {
return
}
console.error(error)
}
async function checkLiveStatus() {
try {
const muteNotification =
(await storage.get<boolean>("mute_notification")) ?? false // 默认为 false
if (muteNotification) return
const { rooms } = await fetchRoomData()
const lastStatus =
(await storage.get<Record<string, boolean>>("last_status")) || {}
const hiddenRooms = (await storage.get<string[]>("hidden_rooms")) || []
const newStatus: Record<string, boolean> = { ...lastStatus } // 保留上一次的状态
// 收集新开播的主播
const newLiveStreamers = []
for (const room of rooms) {
// 更新状态并检查是否需要通知
const oldStatus = lastStatus[room.roomId]
newStatus[room.roomId] = room.isOpen
if (!oldStatus && room.isOpen) {
if (!hiddenRooms.includes(room.roomId)) {
newLiveStreamers.push(room)
}
}
}
// 先更新状态,避免通知失败导致状态不更新
await storage.set("last_status", newStatus)
// 如果有新开播的主播,发送合并通知
if (newLiveStreamers.length > 0) {
try {
const title =
newLiveStreamers.length === 1
? `${newLiveStreamers[0].streamerName} 开播了`
: `${newLiveStreamers.length} 位主播开播了`
const message =
newLiveStreamers.length === 1
? newLiveStreamers[0].roomName
: newLiveStreamers.map((room) => room.streamerName).join("、")
// 获取并更新最近开播主播信息
const recentData = await storage.get<{
rooms: Record<string, { platform: string; url: string }>
timestamp: number
}>("recent_live_streamers")
const currentTime = Date.now()
// 如果存在未过期的数据(6小时内),则合并
const mergedRooms =
!recentData || currentTime - recentData.timestamp > 6 * 60 * 60 * 1000
? newLiveStreamers.reduce(
(acc, room) => ({
...acc,
[`${room.platform}_${room.roomId}`]: {
platform: room.platform,
url: room.url
}
}),
{}
)
: {
...recentData.rooms,
...newLiveStreamers.reduce(
(acc, room) => ({
...acc,
[`${room.platform}_${room.roomId}`]: {
platform: room.platform,
url: room.url
}
}),
{}
)
}
// 存储合并后的开播主播信息
await storage.set("recent_live_streamers", {
rooms: mergedRooms,
timestamp: currentTime
})
const notificationId =
newLiveStreamers.length === 1
? `live_${newLiveStreamers[0].platform}_${newLiveStreamers[0].roomId}`
: `live_multiple`
await chrome.notifications.create(notificationId, {
type: "basic",
iconUrl: newLiveStreamers[0].avatar,
title,
message,
priority: 2
})
} catch (error) {
// 通知失败不影响状态更新,只记录错误
handleError(error as Error)
}
}
} catch (error) {
handleError(error as Error)
}
}
async function updateLiveCheck() {
try {
const muteNotification = await storage.get<boolean>("mute_notification")
const checkIntervalMinutes =
(await storage.get<number>("check_interval")) || 3 // 默认3分钟
// 清除现有的 alarm
await chrome.alarms.clear(ALARM_NAME)
if (!muteNotification) {
// 立即执行一次检查
await checkLiveStatus()
// 创建新的 alarm
chrome.alarms.create(ALARM_NAME, {
periodInMinutes: checkIntervalMinutes
})
}
} catch (error) {
handleError(error as Error)
}
}
// 获取 Twitch auth token
async function getTwitchAuthToken() {
try {
const urls = ["https://www.twitch.tv", "https://twitch.tv"]
for (const url of urls) {
const cookie = await chrome.cookies.get({
url,
name: "auth-token"
})
if (cookie) {
return cookie.value
}
}
return null
} catch (error) {
handleError(error as Error)
return null
}
}
// 监听 alarm 事件
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === ALARM_NAME) {
checkLiveStatus().catch(handleError)
}
})
// 监听存储变化
chrome.storage.onChanged.addListener((changes) => {
// 当通知设置或检查间隔改变时更新检查
if (changes.mute_notification || changes.check_interval) {
updateLiveCheck().catch(handleError)
}
})
// 监听通知点击
chrome.notifications.onClicked.addListener(async (notificationId) => {
try {
console.log("通知被点击:", notificationId)
if (notificationId.startsWith("live_")) {
const recentData = await storage.get<{
rooms: Record<string, { platform: string; url: string }>
timestamp: number
}>("recent_live_streamers")
console.log("获取到的存储数据:", recentData)
const currentTime = Date.now()
// 检查数据是否存在且未过期(6小时)
const isExpired =
!recentData || currentTime - recentData.timestamp > 6 * 60 * 60 * 1000
console.log("数据是否过期:", isExpired)
// 插件标签页 URL
const extensionTabUrl = `chrome-extension://${chrome.runtime.id}/tabs/index.html`
if (notificationId === "live_multiple") {
console.log("多人开播通知,跳转到标签页")
chrome.tabs.create({ url: extensionTabUrl })
} else {
// 单个主播开播,从通知ID解析平台和房间ID
const parts = notificationId.replace("live_", "").split("_")
// 第一个部分是平台,剩余部分组合成房间ID
const platform = parts[0]
const roomId = parts.slice(1).join("_")
const roomKey = `${platform}_${roomId}`
console.log("解析的房间信息:", { platform, roomId, roomKey })
let shouldRedirectToExtension = true
try {
// 如果存储的数据已过期,则重新获取
if (isExpired) {
console.log("数据已过期,重新获取")
const { rooms } = await fetchRoomData()
console.log("重新获取的房间数据:", rooms)
const room = rooms.find(
(r) => r.platform === platform && r.roomId === roomId
)
console.log("找到的房间:", room)
if (room?.url) {
console.log("跳转到直播间:", room.url)
await chrome.tabs.create({ url: room.url })
shouldRedirectToExtension = false
}
} else {
// 使用存储的数据
const room = recentData.rooms[roomKey]
console.log("从存储中找到的房间:", room)
if (room?.url) {
console.log("跳转到直播间:", room.url)
await chrome.tabs.create({ url: room.url })
shouldRedirectToExtension = false
}
}
} catch (error) {
console.error("跳转直播间时出错:", error)
shouldRedirectToExtension = true
}
// 如果没有找到房间信息,跳转到插件标签页
if (shouldRedirectToExtension) {
console.log("未找到房间信息或出错,跳转到标签页")
chrome.tabs.create({ url: extensionTabUrl })
}
}
}
} catch (error) {
console.error("处理通知点击时出错:", error)
handleError(error as Error)
// 发生错误时也跳转到插件标签页
chrome.tabs.create({
url: `chrome-extension://${chrome.runtime.id}/tabs/index.html`
})
}
})
// 监听来自content script的消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === "CLOSE_CURRENT_TAB" && sender.tab) {
chrome.tabs.remove(sender.tab.id)
} else if (message.type === "GET_TWITCH_AUTH") {
getTwitchAuthToken().then(sendResponse)
return true // 保持消息通道开启,等待异步响应
}
})
// 初始化
updateLiveCheck().catch(handleError)