From 146edaeb4cfbed52541727b3ec1edc4e8d4a3128 Mon Sep 17 00:00:00 2001 From: daidr Date: Fri, 21 Apr 2023 14:52:33 +0800 Subject: [PATCH] feat: risk control --- src/background/index.ts | 784 ++++++++++++++++++++-------------------- 1 file changed, 394 insertions(+), 390 deletions(-) diff --git a/src/background/index.ts b/src/background/index.ts index 336d8d0..432a4b5 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,4 +1,4 @@ -import { onMessage, sendMessage } from 'webext-bridge' +import { onMessage, sendMessage } from "webext-bridge"; import { action, alarms, @@ -6,8 +6,8 @@ import { notifications, runtime, tabs, -} from 'webextension-polyfill' -import type { Notifications } from 'webextension-polyfill' +} from "webextension-polyfill"; +import type { Notifications } from "webextension-polyfill"; import type { IAlertSetting, IAlertStatus, @@ -15,7 +15,7 @@ import type { IRoleDataItem, IUserData, IUserDataItem, -} from '~/types' +} from "~/types"; import { calcRoleDataLocally, createVerification, @@ -26,13 +26,22 @@ import { readDataFromStorage, verifyVerification, writeDataToStorage, -} from '~/utils/utils' -import { isFirefox } from '~/env' -import { clearHoYoLABCookie, clearMiHoYoCookie, getHoYoLABCookie, getMiHoYoCookie } from '~/utils/cookie' -import { initResponseRules, resetRules, setExtraHeaders } from '~/utils/networkHook' +} from "~/utils/utils"; +import { isFirefox } from "~/env"; +import { + clearHoYoLABCookie, + clearMiHoYoCookie, + getHoYoLABCookie, + getMiHoYoCookie, +} from "~/utils/cookie"; +import { + initResponseRules, + resetRules, + setExtraHeaders, +} from "~/utils/networkHook"; // 一分钟 -const INTERVAL_TIME = 1 +const INTERVAL_TIME = 1; // 角色的默认提醒设定 const defaultAlertSetting: IAlertSetting = { @@ -40,78 +49,78 @@ const defaultAlertSetting: IAlertSetting = { resinThreshold: 155, realmCurrency: false, transformer: false, -} +}; // 角色默认提醒状态 const defaultAlertStatus: IAlertStatus = { - resin: '', - realmCurrency: '', - transformer: '', -} + resin: "", + realmCurrency: "", + transformer: "", +}; // 通知图标路径 const notificationIconList = { - resin: runtime.getURL('/assets/notifications/icon_resin.png'), + resin: runtime.getURL("/assets/notifications/icon_resin.png"), realmCurrency: runtime.getURL( - '/assets/notifications/icon_realm_currency.png', + "/assets/notifications/icon_realm_currency.png" ), - transformer: runtime.getURL('/assets/notifications/icon_transformer.png'), -} + transformer: runtime.getURL("/assets/notifications/icon_transformer.png"), +}; // 随机生成 10 位字符串作为通知 id const randomNotificationId = () => { - return Math.random().toString(36).slice(2, 10) -} + return Math.random().toString(36).slice(2, 10); +}; const getNotificationMap = async (): Promise> => { - return JSON.parse(await readDataFromStorage('notificationMap', '{}')) -} + return JSON.parse(await readDataFromStorage("notificationMap", "{}")); +}; const getNotificationItem = async ( - uid: string, + uid: string ): Promise => { - const map = await getNotificationMap() - return map[uid] || false -} + const map = await getNotificationMap(); + return map[uid] || false; +}; const setNotificationItem = async ( uid: string, - item: IAlertStatus, + item: IAlertStatus ): Promise => { - const map = await getNotificationMap() - map[uid] = item - await writeDataToStorage('notificationMap', JSON.stringify(map)) -} + const map = await getNotificationMap(); + map[uid] = item; + await writeDataToStorage("notificationMap", JSON.stringify(map)); +}; // type: 0 resin; 1 realmCurrency; 2 transformer const showNotification = async ( alertStatus: IAlertStatus, type: 0 | 1 | 2, - scope: any, + scope: any ) => { if (!isFirefox) { // chromium 系浏览器 if (type === 0) { const notificationData: Notifications.CreateNotificationOptions = { - type: 'basic', + type: "basic", iconUrl: notificationIconList.resin, - title: i18n.getMessage('options_Alert_Notify_Title'), - message: i18n.getMessage('options_Alert_Notify_Resin', [scope.resin]), + title: i18n.getMessage("options_Alert_Notify_Title"), + message: i18n.getMessage("options_Alert_Notify_Resin", [scope.resin]), contextMessage: `${scope.name}(${scope.uid}) - ${i18n.getMessage( - scope.server, + scope.server )}`, - } + }; - if (alertStatus.resin === '') { + if (alertStatus.resin === "") { // 创建通知 - alertStatus.resin = randomNotificationId() - notifications.create(alertStatus.resin, notificationData) + alertStatus.resin = randomNotificationId(); + notifications.create(alertStatus.resin, notificationData); } else { // 更新通知 // eslint-disable-next-line @typescript-eslint/no-unused-vars const _ret = await notifications.update( alertStatus.resin, - notificationData, - ) + notificationData + ); // if (!_ret) { // alertStatus.resin = randomNotificationId() @@ -120,148 +129,148 @@ const showNotification = async ( } } else if (type === 1) { const notificationData: Notifications.CreateNotificationOptions = { - type: 'basic', + type: "basic", iconUrl: notificationIconList.realmCurrency, - title: i18n.getMessage('options_Alert_Notify_Title'), - message: i18n.getMessage('options_Alert_Notify_RealmCurrency'), + title: i18n.getMessage("options_Alert_Notify_Title"), + message: i18n.getMessage("options_Alert_Notify_RealmCurrency"), contextMessage: `${scope.name}(${scope.uid}) - ${i18n.getMessage( - scope.server, + scope.server )}`, - } + }; - if (alertStatus.realmCurrency === '') { + if (alertStatus.realmCurrency === "") { // 创建通知 - alertStatus.realmCurrency = randomNotificationId() - notifications.create(alertStatus.realmCurrency, notificationData) + alertStatus.realmCurrency = randomNotificationId(); + notifications.create(alertStatus.realmCurrency, notificationData); } } else if (type === 2) { const notificationData: Notifications.CreateNotificationOptions = { - type: 'basic', + type: "basic", iconUrl: notificationIconList.transformer, - title: i18n.getMessage('options_Alert_Notify_Title'), - message: i18n.getMessage('options_Alert_Notify_Transformer'), + title: i18n.getMessage("options_Alert_Notify_Title"), + message: i18n.getMessage("options_Alert_Notify_Transformer"), contextMessage: `${scope.name}(${scope.uid}) - ${i18n.getMessage( - scope.server, + scope.server )}`, - } + }; - if (alertStatus.transformer === '') { + if (alertStatus.transformer === "") { // 创建通知 - alertStatus.transformer = randomNotificationId() - notifications.create(alertStatus.transformer, notificationData) + alertStatus.transformer = randomNotificationId(); + notifications.create(alertStatus.transformer, notificationData); } } } else { // firefox 浏览器 if (type === 0) { const notificationData: Notifications.CreateNotificationOptions = { - type: 'basic', + type: "basic", iconUrl: notificationIconList.resin, - title: i18n.getMessage('options_Alert_Notify_Title'), - message: i18n.getMessage('options_Alert_Notify_Resin_Firefox', [ + title: i18n.getMessage("options_Alert_Notify_Title"), + message: i18n.getMessage("options_Alert_Notify_Resin_Firefox", [ `${scope.name}(${scope.uid})`, scope.resin, ]), - } + }; - if (alertStatus.resin === '') { + if (alertStatus.resin === "") { // 创建通知 - alertStatus.resin = randomNotificationId() - notifications.create(alertStatus.resin, notificationData) + alertStatus.resin = randomNotificationId(); + notifications.create(alertStatus.resin, notificationData); } } else if (type === 1) { const notificationData: Notifications.CreateNotificationOptions = { - type: 'basic', + type: "basic", iconUrl: notificationIconList.realmCurrency, - title: i18n.getMessage('options_Alert_Notify_Title'), - message: i18n.getMessage('options_Alert_Notify_RealmCurrency_Firefox', [ + title: i18n.getMessage("options_Alert_Notify_Title"), + message: i18n.getMessage("options_Alert_Notify_RealmCurrency_Firefox", [ `${scope.name}(${scope.uid})`, ]), - } + }; - if (alertStatus.realmCurrency === '') { + if (alertStatus.realmCurrency === "") { // 创建通知 - alertStatus.realmCurrency = randomNotificationId() - notifications.create(alertStatus.realmCurrency, notificationData) + alertStatus.realmCurrency = randomNotificationId(); + notifications.create(alertStatus.realmCurrency, notificationData); } } else if (type === 2) { const notificationData: Notifications.CreateNotificationOptions = { - type: 'basic', + type: "basic", iconUrl: notificationIconList.transformer, - title: i18n.getMessage('options_Alert_Notify_Title'), - message: i18n.getMessage('options_Alert_Notify_Transformer_Firefox', [ + title: i18n.getMessage("options_Alert_Notify_Title"), + message: i18n.getMessage("options_Alert_Notify_Transformer_Firefox", [ `${scope.name}(${scope.uid})`, ]), - } + }; - if (alertStatus.transformer === '') { + if (alertStatus.transformer === "") { // 创建通知 - alertStatus.transformer = randomNotificationId() - notifications.create(alertStatus.transformer, notificationData) + alertStatus.transformer = randomNotificationId(); + notifications.create(alertStatus.transformer, notificationData); } } } -} +}; const removeNotification = (alertStatus: IAlertStatus, type: 0 | 1 | 2) => { if (type === 0) { - if (alertStatus.resin !== '') { - notifications.clear(alertStatus.resin) - alertStatus.resin = '' + if (alertStatus.resin !== "") { + notifications.clear(alertStatus.resin); + alertStatus.resin = ""; } } else if (type === 1) { - if (alertStatus.realmCurrency !== '') { - notifications.clear(alertStatus.realmCurrency) - alertStatus.realmCurrency = '' + if (alertStatus.realmCurrency !== "") { + notifications.clear(alertStatus.realmCurrency); + alertStatus.realmCurrency = ""; } } else if (type === 2) { - if (alertStatus.transformer !== '') { - notifications.clear(alertStatus.transformer) - alertStatus.transformer = '' + if (alertStatus.transformer !== "") { + notifications.clear(alertStatus.transformer); + alertStatus.transformer = ""; } } -} +}; // selected uid -let selectedUid = '' +let selectedUid = ""; -initResponseRules() +initResponseRules(); const getSelectedUid = async () => { - return await readDataFromStorage('selectedRole', '') -} + return await readDataFromStorage("selectedRole", ""); +}; const getRefreshInterval = async () => { - return await readDataFromStorage('refreshInterval', 30) -} + return await readDataFromStorage("refreshInterval", 30); +}; const setRefreshInterval = async (interval: number) => { - await writeDataToStorage('refreshInterval', interval) -} + await writeDataToStorage("refreshInterval", interval); +}; const getBadgeVisibility = async () => { - return await readDataFromStorage('badgeVisibility', true) -} + return await readDataFromStorage("badgeVisibility", true); +}; const setBadgeVisibility = async (visibility: boolean) => { - await writeDataToStorage('badgeVisibility', visibility) -} + await writeDataToStorage("badgeVisibility", visibility); +}; const addNewRoleToList = async function ( oversea: boolean, roleInfo: IRoleDataItem, - cookie: string, + cookie: string ) { // 取出原始 roleList const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); // 判断是否已经存在 const isExist = originRoleList.some((item) => { - return item.uid === roleInfo.game_uid - }) + return item.uid === roleInfo.game_uid; + }); // 构造一个 roleItem const roleItem: IUserDataItem = { @@ -271,43 +280,42 @@ const addNewRoleToList = async function ( nickname: roleInfo.nickname, level: roleInfo.level, serverRegion: roleInfo.region, - serverType: oversea ? 'os' : 'cn', + serverType: oversea ? "os" : "cn", cookie, data: {} as IUserData, updateTimestamp: -1, - } + }; // 如果不存在,则添加 if (!isExist) { - originRoleList.push(roleItem) + originRoleList.push(roleItem); } else { // 如果存在,则更新 const index = originRoleList.findIndex((item) => { - return item.uid === roleInfo.game_uid - }) - originRoleList.splice(index, 1, roleItem) + return item.uid === roleInfo.game_uid; + }); + originRoleList.splice(index, 1, roleItem); } // 更新 roleList - await writeDataToStorage('roleList', originRoleList) -} + await writeDataToStorage("roleList", originRoleList); +}; let alertSettings: IAlertSetting = defaultAlertSetting; (async function () { alertSettings = await readDataFromStorage( - 'alertSetting', - defaultAlertSetting, - ) -})() + "alertSetting", + defaultAlertSetting + ); +})(); const doAlertCheck = async function (roleInfo: IUserDataItem) { - if (!roleInfo.enabledAlert) - return + if (!roleInfo.enabledAlert) return; - const _notificationStatus: IAlertStatus - = (await getNotificationItem(roleInfo.uid)) - || (JSON.parse(JSON.stringify(defaultAlertStatus)) as IAlertStatus) + const _notificationStatus: IAlertStatus = + (await getNotificationItem(roleInfo.uid)) || + (JSON.parse(JSON.stringify(defaultAlertStatus)) as IAlertStatus); // 树脂检查 if (alertSettings.resin) { @@ -317,108 +325,108 @@ const doAlertCheck = async function (roleInfo: IUserDataItem) { uid: roleInfo.uid, server: roleInfo.serverRegion, resin: roleInfo.data.current_resin, - }) + }); } else { - removeNotification(_notificationStatus, 0) + removeNotification(_notificationStatus, 0); } } // 洞天宝钱检查 if (alertSettings.realmCurrency) { if ( - roleInfo.data.current_home_coin === roleInfo.data.max_home_coin - && roleInfo.data.current_home_coin > 0 + roleInfo.data.current_home_coin === roleInfo.data.max_home_coin && + roleInfo.data.current_home_coin > 0 ) { showNotification(_notificationStatus, 1, { name: roleInfo.nickname, uid: roleInfo.uid, server: roleInfo.serverRegion, - }) + }); } else { - removeNotification(_notificationStatus, 1) + removeNotification(_notificationStatus, 1); } } // 参量质变仪检查 if (alertSettings.transformer) { if ( - roleInfo.data.transformer.obtained - && roleInfo.data.transformer.recovery_time.reached + roleInfo.data.transformer.obtained && + roleInfo.data.transformer.recovery_time.reached ) { showNotification(_notificationStatus, 2, { name: roleInfo.nickname, uid: roleInfo.uid, server: roleInfo.serverRegion, - }) + }); } else { - removeNotification(_notificationStatus, 2) + removeNotification(_notificationStatus, 2); } } // 保存通知状态 - await setNotificationItem(roleInfo.uid, _notificationStatus) -} + await setNotificationItem(roleInfo.uid, _notificationStatus); +}; const getLatestUpdatedTime = function (role: IUserDataItem) { - return role.updateTimestamp -} + return role.updateTimestamp; +}; const refreshData = async function ( uiOnly = false, fromPopup = false, - forceRefresh = false, + forceRefresh = false ) { // 取出刷新时间间隔 (原始数据为分钟,这里转换为毫秒) // 如果 fromPopup 为 true,则间隔时间设置为 2 分钟 let refreshInterval = fromPopup ? 60 * 1000 - : (await getRefreshInterval()) * 60 * 1000 * 2 + : (await getRefreshInterval()) * 60 * 1000 * 2; // 如果刷新间隔大于二十分钟,则进行扰动 if (refreshInterval > 20 * 60 * 1000) - refreshInterval = refreshInterval + getRandomTimeOffset() + refreshInterval = refreshInterval + getRandomTimeOffset(); // 是否显示 badge - const badgeVisibility = await getBadgeVisibility() + const badgeVisibility = await getBadgeVisibility(); // 取出原始 roleList const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); // 取出启用的 role const enabledRoleList = originRoleList.filter((item) => { - return item.isEnabled - }) + return item.isEnabled; + }); // 如果当前还没有 selectedUid 则获取一个 - let _selectedUid = selectedUid || (await getSelectedUid()) + let _selectedUid = selectedUid || (await getSelectedUid()); if (enabledRoleList.length > 0) { // 查看选中uid是否已经启用 if (!enabledRoleList.find((item) => item.uid === _selectedUid)) { // 未启用则选中第一个 - _selectedUid = enabledRoleList[0]?.uid + _selectedUid = enabledRoleList[0]?.uid; } } - let hasUpdatedBadge = false + let hasUpdatedBadge = false; // 遍历启用的 role for (const role of enabledRoleList) { // 如果当前时间 - 上次更新时间 < 刷新时间间隔 或 uiOnly==true,则使用缓存 - const useCache - = (getLatestUpdatedTime(role) - && Date.now() - getLatestUpdatedTime(role) < refreshInterval) - || uiOnly - const data - = useCache && !forceRefresh + const useCache = + (getLatestUpdatedTime(role) && + Date.now() - getLatestUpdatedTime(role) < refreshInterval) || + uiOnly; + const data = + useCache && !forceRefresh ? role.data : await getRoleDataByCookie( - role.serverType === 'os', - role.cookie, - role.uid, - role.serverRegion, - setExtraHeaders, - resetRules, - ) + role.serverType === "os", + role.cookie, + role.uid, + role.serverRegion, + setExtraHeaders, + resetRules + ); if (Number.isInteger(data)) { // error code @@ -427,397 +435,393 @@ const refreshData = async function ( // risk control // 尝试自动解决 - const __ret = await autoGeetestChallenge(role.uid) - if (__ret) - return + // const __ret = await autoGeetestChallenge(role.uid) + // if (__ret) + // return // 获取失败,写入错误信息 - role.isError = true - role.errorMessage = '触发风控' - role.updateTimestamp = Date.now() - return + role.isError = true; + role.errorMessage = "触发风控"; + // role.updateTimestamp = Date.now() + break; } } - } else if (data && typeof data === 'object') { + } else if (data && typeof data === "object") { // 更新 roleList const roleIndex = originRoleList.findIndex((item) => { - return item.uid === role.uid - }) + return item.uid === role.uid; + }); const _newItem = { ...role, data, isError: false, - errorMessage: '', + errorMessage: "", updateTimestamp: useCache ? role.updateTimestamp : Date.now(), - } + }; - originRoleList.splice(roleIndex, 1, _newItem) + originRoleList.splice(roleIndex, 1, _newItem); - const _copyItem = calcRoleDataLocally(_newItem) + const _copyItem = calcRoleDataLocally(_newItem); - !uiOnly && (await doAlertCheck(_copyItem)) + !uiOnly && (await doAlertCheck(_copyItem)); if (!badgeVisibility && !hasUpdatedBadge) { // 如果设置不显示 badge,则不更新 - action.setBadgeText({ text: '' }) - hasUpdatedBadge = true + action.setBadgeText({ text: "" }); + hasUpdatedBadge = true; } if (!hasUpdatedBadge) { - let shouldUpdateBadge = false + let shouldUpdateBadge = false; if (_selectedUid && _selectedUid === _copyItem.uid) - shouldUpdateBadge = true // 更新 _selectedUid 当前的 resin 数据到 badge + shouldUpdateBadge = true; // 更新 _selectedUid 当前的 resin 数据到 badge if (shouldUpdateBadge && _copyItem?.data?.current_resin) { - action.setBadgeText({ text: `${_copyItem.data.current_resin}` }) - action.setBadgeBackgroundColor({ color: '#6F9FDF' }) - hasUpdatedBadge = true + action.setBadgeText({ text: `${_copyItem.data.current_resin}` }); + action.setBadgeBackgroundColor({ color: "#6F9FDF" }); + hasUpdatedBadge = true; } } } else { // 获取失败,写入错误信息 - role.isError = true - role.errorMessage = '获取数据失败' + role.isError = true; + role.errorMessage = "获取数据失败"; // role.updateTimestamp = Date.now() } } // 更新 roleList - !uiOnly && (await writeDataToStorage('roleList', originRoleList)) - !uiOnly && refreshData(true) -} + !uiOnly && (await writeDataToStorage("roleList", originRoleList)); + !uiOnly && refreshData(true); +}; // 定时器,定时获取玩家数据 const initAlarm = async (interval_time = INTERVAL_TIME) => { - await alarms.clear('refresh_data') - alarms.create('refresh_data', { periodInMinutes: interval_time }) -} + await alarms.clear("refresh_data"); + alarms.create("refresh_data", { periodInMinutes: interval_time }); +}; -initAlarm() +initAlarm(); alarms.onAlarm.addListener((alarmInfo) => { - if (alarmInfo.name === 'refresh_data') - refreshData() -}) + if (alarmInfo.name === "refresh_data") refreshData(); +}); -refreshData() +refreshData(); // 传入 AlertStatus,对存在的通知进行清理 const clearNotifications = async function (alertStatus: IAlertStatus) { - if (alertStatus.resin !== '') { - await notifications.clear(alertStatus.resin) - alertStatus.resin = '' + if (alertStatus.resin !== "") { + await notifications.clear(alertStatus.resin); + alertStatus.resin = ""; } - if (alertStatus.realmCurrency !== '') { - await notifications.clear(alertStatus.realmCurrency) - alertStatus.realmCurrency = '' + if (alertStatus.realmCurrency !== "") { + await notifications.clear(alertStatus.realmCurrency); + alertStatus.realmCurrency = ""; } - if (alertStatus.transformer !== '') { - await notifications.clear(alertStatus.transformer) - alertStatus.transformer = '' + if (alertStatus.transformer !== "") { + await notifications.clear(alertStatus.transformer); + alertStatus.transformer = ""; } -} +}; -onMessage('get_alert_setting', async () => { +onMessage("get_alert_setting", async () => { return await readDataFromStorage( - 'alertSetting', - defaultAlertSetting, - ) -}) + "alertSetting", + defaultAlertSetting + ); +}); onMessage< { - resin: boolean - resinThreshold: number - transformer: boolean - realmCurrency: boolean + resin: boolean; + resinThreshold: number; + transformer: boolean; + realmCurrency: boolean; }, - 'set_alert_setting' ->('set_alert_setting', async ({ data: alertSetting }) => { - await writeDataToStorage('alertSetting', alertSetting) -}) - -onMessage('get_role_list', async () => { - return await readDataFromStorage('roleList', []) -}) - -onMessage('get_selected_role', async () => { - return await getSelectedUid() -}) - -onMessage('refresh_request', async () => { - await refreshData(false, true, false) - return true -}) - -onMessage('refresh_request_force', async () => { - await refreshData(false, true, true) - return true -}) - -onMessage<{ uid: string }, 'set_selected_role'>( - 'set_selected_role', + "set_alert_setting" +>("set_alert_setting", async ({ data: alertSetting }) => { + await writeDataToStorage("alertSetting", alertSetting); +}); + +onMessage("get_role_list", async () => { + return await readDataFromStorage("roleList", []); +}); + +onMessage("get_selected_role", async () => { + return await getSelectedUid(); +}); + +onMessage("refresh_request", async () => { + await refreshData(false, true, false); + return true; +}); + +onMessage("refresh_request_force", async () => { + await refreshData(false, true, true); + return true; +}); + +onMessage<{ uid: string }, "set_selected_role">( + "set_selected_role", async ({ data: { uid } }) => { - selectedUid = uid // update cache - await writeDataToStorage('selectedRole', uid) - refreshData(true) - }, -) + selectedUid = uid; // update cache + await writeDataToStorage("selectedRole", uid); + refreshData(true); + } +); -onMessage<{ uid: string; status: boolean }, 'set_role_status'>( - 'set_role_status', +onMessage<{ uid: string; status: boolean }, "set_role_status">( + "set_role_status", async ({ data: { uid, status } }) => { const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); const index = originRoleList.findIndex((item) => { - return item.uid === uid - }) - const _notificationStatus: IAlertStatus - = (await getNotificationItem(uid)) - || (JSON.parse(JSON.stringify(defaultAlertStatus)) as IAlertStatus) + return item.uid === uid; + }); + const _notificationStatus: IAlertStatus = + (await getNotificationItem(uid)) || + (JSON.parse(JSON.stringify(defaultAlertStatus)) as IAlertStatus); - originRoleList[index].isEnabled = status + originRoleList[index].isEnabled = status; // 重置角色提醒状态 - clearNotifications(_notificationStatus) - await writeDataToStorage('roleList', originRoleList) - await setNotificationItem(uid, _notificationStatus) + clearNotifications(_notificationStatus); + await writeDataToStorage("roleList", originRoleList); + await setNotificationItem(uid, _notificationStatus); // 刷新一次数据(仅刷新ui,例如badge/notification) - refreshData(true) - }, -) + refreshData(true); + } +); -onMessage<{ uid: string; status: boolean }, 'set_role_alert_status'>( - 'set_role_alert_status', +onMessage<{ uid: string; status: boolean }, "set_role_alert_status">( + "set_role_alert_status", async ({ data: { uid, status } }) => { const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); const index = originRoleList.findIndex((item) => { - return item.uid === uid - }) - const _notificationStatus: IAlertStatus - = (await getNotificationItem(uid)) - || (JSON.parse(JSON.stringify(defaultAlertStatus)) as IAlertStatus) + return item.uid === uid; + }); + const _notificationStatus: IAlertStatus = + (await getNotificationItem(uid)) || + (JSON.parse(JSON.stringify(defaultAlertStatus)) as IAlertStatus); - originRoleList[index].enabledAlert = status + originRoleList[index].enabledAlert = status; // 重置角色提醒状态 - clearNotifications(_notificationStatus) - await writeDataToStorage('roleList', originRoleList) - await setNotificationItem(uid, _notificationStatus) - }, -) + clearNotifications(_notificationStatus); + await writeDataToStorage("roleList", originRoleList); + await setNotificationItem(uid, _notificationStatus); + } +); -onMessage<{ uid: string }, 'delete_role_request'>( - 'delete_role_request', +onMessage<{ uid: string }, "delete_role_request">( + "delete_role_request", async ({ data: { uid } }) => { // 取出原始 roleList const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); // 删除 roleUid const newRoleList = originRoleList.filter((item) => { - return item.uid !== uid - }) + return item.uid !== uid; + }); // 更新 roleList - await writeDataToStorage('roleList', newRoleList) + await writeDataToStorage("roleList", newRoleList); - return true - }, -) + return true; + } +); -onMessage<{ oversea: boolean }, 'request_cookie_read'>( - 'request_cookie_read', +onMessage<{ oversea: boolean }, "request_cookie_read">( + "request_cookie_read", async ({ data: { oversea } }) => { - let cookie = '' + let cookie = ""; // 根据服务器类型获取对应 cookie - if (oversea) - cookie = await getHoYoLABCookie() - else cookie = await getMiHoYoCookie() + if (oversea) cookie = await getHoYoLABCookie(); + else cookie = await getMiHoYoCookie(); // cookie 获取失败,返回 false - if (cookie === '') - return -1 + if (cookie === "") return -1; const result = await getRoleInfoByCookie( oversea, cookie, setExtraHeaders, - resetRules, - ) + resetRules + ); if (result) { - for (const item of result) await addNewRoleToList(oversea, item, cookie) - await refreshData() + for (const item of result) await addNewRoleToList(oversea, item, cookie); + await refreshData(); // 清空 cookie if (oversea) { // 清空 hoyolab cookie - await clearHoYoLABCookie() + await clearHoYoLABCookie(); } else { // 清空 mihoyo cookie - await clearMiHoYoCookie() + await clearMiHoYoCookie(); } - return result.length + return result.length; } else { - return -1 + return -1; } - }, -) + } +); -onMessage<{ uid: string }, 'create_verification'>( - 'create_verification', +onMessage<{ uid: string }, "create_verification">( + "create_verification", async ({ data: { uid } }) => { - return await _createVerification(uid) - }, -) + return await _createVerification(uid); + } +); async function _createVerification(uid: string) { const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); const index = originRoleList.findIndex((item) => { - return item.uid === uid - }) - const cookie = originRoleList[index].cookie - const oversea = originRoleList[index].serverType === 'os' + return item.uid === uid; + }); + const cookie = originRoleList[index].cookie; + const oversea = originRoleList[index].serverType === "os"; - return await createVerification(oversea, cookie, setExtraHeaders, resetRules) + return await createVerification(oversea, cookie, setExtraHeaders, resetRules); } -onMessage<{ uid: string }, 'request_captcha_bg'>( - 'request_captcha_bg', +onMessage<{ uid: string }, "request_captcha_bg">( + "request_captcha_bg", async ({ data: { uid } }) => { // open captcha tab const curtab = await tabs.create({ - url: 'https://paimon-webext.daidr.me/captcha.html', - }) + url: "https://paimon-webext.daidr.me/captcha.html", + }); // wait for curtab loaded await new Promise((resolve, reject) => { const check = async () => { if (curtab.id === undefined) { - reject(new Error('tab id is undefined')) - return + reject(new Error("tab id is undefined")); + return; } - const tab = await tabs.get(curtab.id) - if (tab.status === 'complete') - resolve(true) - else setTimeout(check, 100) - } - check() - }) + const tab = await tabs.get(curtab.id); + if (tab.status === "complete") resolve(true); + else setTimeout(check, 100); + }; + check(); + }); - console.log('tab loaded') + console.log("tab loaded"); // send message to captcha tab const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); const index = originRoleList.findIndex((item) => { - return item.uid === uid - }) - const cookie = originRoleList[index].cookie - const oversea = originRoleList[index].serverType === 'os' + return item.uid === uid; + }); + const cookie = originRoleList[index].cookie; + const oversea = originRoleList[index].serverType === "os"; const verification = await createVerification( oversea, cookie, setExtraHeaders, - resetRules, - ) + resetRules + ); if (verification && curtab.id) await sendMessage( - 'request_captcha', + "request_captcha", { verification, uid, tabId: curtab.id }, - { tabId: curtab.id, context: 'content-script' }, - ) - }, -) + { tabId: curtab.id, context: "content-script" } + ); + } +); -onMessage('finish_captcha', async ({ data: { tabId, uid, geetest } }) => { - const _ret = await _verifyVerification(uid, geetest) - tabs.remove(tabId) +onMessage("finish_captcha", async ({ data: { tabId, uid, geetest } }) => { + const _ret = await _verifyVerification(uid, geetest); + tabs.remove(tabId); - return _ret -}) + return _ret; +}); async function _verifyVerification(uid: string, geetest: ICaptchaRequest) { const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); const index = originRoleList.findIndex((item) => { - return item.uid === uid - }) + return item.uid === uid; + }); - const cookie = originRoleList[index].cookie - const oversea = originRoleList[index].serverType === 'os' + const cookie = originRoleList[index].cookie; + const oversea = originRoleList[index].serverType === "os"; const result = await verifyVerification( oversea, cookie, geetest, setExtraHeaders, - resetRules, - ) + resetRules + ); - getRoleInfoByCookie(oversea, cookie, setExtraHeaders, resetRules) - refreshData(false, false, true) - return result + getRoleInfoByCookie(oversea, cookie, setExtraHeaders, resetRules); + refreshData(false, false, true); + return result; } -onMessage('read_settings', async () => { +onMessage("read_settings", async () => { const settings = { refreshInterval: await getRefreshInterval(), badgeVisibility: await getBadgeVisibility(), - } - return settings -}) + }; + return settings; +}); onMessage( - 'write_settings', + "write_settings", async ({ data: { refreshInterval, badgeVisibility } }) => { - await setRefreshInterval(refreshInterval) - await setBadgeVisibility(badgeVisibility) - await refreshData() - }, -) + await setRefreshInterval(refreshInterval); + await setBadgeVisibility(badgeVisibility); + await refreshData(); + } +); async function autoGeetestChallenge(uid: string) { const originRoleList = await readDataFromStorage( - 'roleList', - [], - ) + "roleList", + [] + ); const index = originRoleList.findIndex((item) => { - return item.uid === uid - }) - const oversea = originRoleList[index].serverType === 'os' + return item.uid === uid; + }); + const oversea = originRoleList[index].serverType === "os"; - const challenge = await _createVerification(uid) + const challenge = await _createVerification(uid); if (challenge) { const _validate = await getGeetestChallenge( oversea, challenge.challenge, challenge.gt, setExtraHeaders, - resetRules, - ) + resetRules + ); if (_validate) { const _ret = await _verifyVerification(uid, { geetest_challenge: challenge.challenge, geetest_validate: _validate, geetest_seccode: `${_validate}|jordan`, - }) - return _ret + }); + return _ret; } } - return false + return false; }