Skip to content

Commit 5555d78

Browse files
lingyireddaidr
andauthored
Badge support (#28)
* add badge display for selected user * feat: while the selected role is disabled, the first role will be used by default * feat: UI-only refresh support Co-authored-by: daidr <daidr@daidr.me>
1 parent a281117 commit 5555d78

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/background/index.ts

+42-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { onMessage } from 'webext-bridge'
2-
import { alarms, cookies, i18n, notifications, runtime, storage } from 'webextension-polyfill'
2+
import { action, alarms, cookies, i18n, notifications, runtime, storage } from 'webextension-polyfill'
33
import type { Cookies, Notifications } from 'webextension-polyfill'
44
import type { IAlertSetting, IAlertStatus, IRoleDataItem, IUserData, IUserDataItem } from '~/types'
55
import { getRoleDataByCookie, getRoleInfoByCookie } from '~/utils'
@@ -163,6 +163,9 @@ const writeDataToStorage = async function <T>(key: string, data: T) {
163163
await storage.local.set({ [key]: data })
164164
}
165165

166+
// selected uid
167+
let selectedUid = ''
168+
166169
const targetPages = [
167170
'https://api-os-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_global&asource=paimon',
168171
'https://api-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_cn&asource=paimon',
@@ -230,6 +233,10 @@ const readDataFromStorage = async function <T>(key: string, defaultVal: T): Prom
230233
return defaultVal
231234
}
232235

236+
const getSelectedUid = async () => {
237+
return await readDataFromStorage<string>('selectedRole', '')
238+
}
239+
233240
// 获取国服cookie
234241
const getMiHoYoCookie = async function () {
235242
let cookieString = ''
@@ -372,36 +379,61 @@ const doAlertCheck = async function (roleInfo: IUserDataItem) {
372379
}
373380
}
374381

375-
const refreshData = async function () {
382+
const refreshData = async function (uiOnly = false) {
376383
// 取出原始 roleList
377384
const originRoleList = await readDataFromStorage<IUserDataItem[]>('roleList', [])
378385
// 取出启用的 role
379386
const enabledRoleList = originRoleList.filter((item) => {
380387
return item.isEnabled
381388
})
382389

390+
// 如果当前还没有 selectedUid 则获取一个
391+
let _selectedUid = selectedUid || await getSelectedUid()
392+
393+
if (enabledRoleList.length > 0) {
394+
// 查看选中uid是否已经启用
395+
if (!enabledRoleList.find(item => item.uid === _selectedUid)) {
396+
// 未启用则选中第一个
397+
_selectedUid = enabledRoleList[0]?.uid
398+
}
399+
}
400+
383401
const setCookie = async (cookie: string) => {
384402
currentCookie = cookie
385403
await updateRules()
386404
}
387405

406+
let hasUpdatedBadge = false
388407
// 遍历启用的 role
389408
for (const role of enabledRoleList) {
390-
const data = await getRoleDataByCookie(role.serverType === 'os', role.cookie, role.uid, role.serverRegion, setCookie)
409+
const data = uiOnly ? role.data : await getRoleDataByCookie(role.serverType === 'os', role.cookie, role.uid, role.serverRegion, setCookie)
410+
391411
if (data) {
392412
// 更新 roleList
393-
const index = originRoleList.findIndex((item) => {
413+
const roleIndex = originRoleList.findIndex((item) => {
394414
return item.uid === role.uid
395415
})
396416

397-
originRoleList.splice(index, 1, {
417+
originRoleList.splice(roleIndex, 1, {
398418
...role,
399419
data,
400420
isError: false,
401421
errorMessage: '',
402422
updateTimestamp: Date.now(),
403423
})
404424
doAlertCheck(role)
425+
426+
if (!hasUpdatedBadge) {
427+
let shouldUpdateBadge = false
428+
if (_selectedUid && _selectedUid === role.uid)
429+
shouldUpdateBadge = true // 更新 _selectedUid 当前的 resin 数据到 badge
430+
431+
if (shouldUpdateBadge && role?.data?.current_resin) {
432+
action.setBadgeText({ text: `${role.data.current_resin}` })
433+
action.setBadgeBackgroundColor({ color: '#6F9FDF' })
434+
hasUpdatedBadge = true
435+
}
436+
}
405437
}
406438
else {
407439
// 获取失败,写入错误信息
@@ -457,7 +489,7 @@ onMessage('get_role_list', async () => {
457489
})
458490

459491
onMessage('get_selected_role', async () => {
460-
return await readDataFromStorage<string>('selectedRole', '')
492+
return await getSelectedUid()
461493
})
462494

463495
onMessage('refresh_request', async () => {
@@ -466,7 +498,9 @@ onMessage('refresh_request', async () => {
466498
})
467499

468500
onMessage<{ uid: string }, 'set_selected_role'>('set_selected_role', async ({ data: { uid } }) => {
501+
selectedUid = uid // update cache
469502
await writeDataToStorage('selectedRole', uid)
503+
refreshData()
470504
})
471505

472506
onMessage<{ uid: string; status: boolean }, 'set_role_status'>('set_role_status', async ({ data: { uid, status } }) => {
@@ -479,6 +513,8 @@ onMessage<{ uid: string; status: boolean }, 'set_role_status'>('set_role_status'
479513
clearNotifications(originRoleList[index].alertStatus)
480514
originRoleList[index].alertStatus = defaultAlertStatus
481515
await writeDataToStorage('roleList', originRoleList)
516+
// 刷新一次数据(仅刷新ui,例如badge/notification)
517+
refreshData(true)
482518
})
483519

484520
onMessage<{ uid: string; status: boolean }, 'set_role_alert_status'>('set_role_alert_status', async ({ data: { uid, status } }) => {

0 commit comments

Comments
 (0)