From 74baa6d5cdf7dea3a82c03bea7fbf12a56a6c871 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 14 Dec 2023 23:17:23 +0800 Subject: [PATCH] feat: rules updating alarms --- assets/locales/en/messages.json | 3 +++ assets/locales/zh/messages.json | 3 +++ package.json | 7 ++++++- src/background/index.ts | 3 +++ src/background/rules.ts | 24 ++++++++++++++++++++++++ src/lib/config.ts | 4 ++-- src/lib/rules.ts | 6 +++++- src/options/routes/General.tsx | 1 + 8 files changed, 47 insertions(+), 4 deletions(-) diff --git a/assets/locales/en/messages.json b/assets/locales/en/messages.json index 8d9a20c..705bf96 100644 --- a/assets/locales/en/messages.json +++ b/assets/locales/en/messages.json @@ -124,5 +124,8 @@ }, "updateSuccessful": { "message": "Update successful" + }, + "updateTip": { + "message": "Automatically update every two hours, you can also manually update." } } \ No newline at end of file diff --git a/assets/locales/zh/messages.json b/assets/locales/zh/messages.json index 0c0879d..8ca3fa2 100644 --- a/assets/locales/zh/messages.json +++ b/assets/locales/zh/messages.json @@ -124,5 +124,8 @@ }, "updateSuccessful": { "message": "更新成功" + }, + "updateTip": { + "message": "每两小时自动更新一次,你也可以手动更新" } } \ No newline at end of file diff --git a/package.json b/package.json index 75d17e7..2fc665b 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,11 @@ "notifications", "alarms", "idle" - ] + ], + "browser_specific_settings": { + "gecko": { + "id": "i@diygod.me" + } + } } } diff --git a/src/background/index.ts b/src/background/index.ts index 5ef1a8f..df06b18 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,4 +1,5 @@ import { getRSS, deleteCachedRSS } from "./rss" +import { initSchedule } from "./rules" export {} console.log("HELLO WORLD FROM BGSCRIPTS") @@ -29,6 +30,8 @@ chrome.tabs.onRemoved.addListener((tabId) => { deleteCachedRSS(tabId) }); +initSchedule(); + // chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { // if (sender.tab && sender.tab.active) { // if (msg.text === 'setPageRSS') { diff --git a/src/background/rules.ts b/src/background/rules.ts index abb5843..c7703a7 100644 --- a/src/background/rules.ts +++ b/src/background/rules.ts @@ -1,5 +1,6 @@ import { Storage } from "@plasmohq/storage" import { getRemoteRules } from "~/lib/rules" +import { getConfig } from "~/lib/config" const storage = new Storage({ area: "local" @@ -23,3 +24,26 @@ export const refreshRules = async () => { export const getDisplayedRules = () => storage.get("displayedRules") export const setDisplayedRules = (displayedRules) => storage.set("displayedRules", displayedRules) + +chrome.alarms.onAlarm.addListener((alarm) => { + if (alarm.name === 'refreshRulesAlarm') { + refreshRules(); + } +}); + +export async function initSchedule() { + const config = await getConfig(); + const rules = await storage.get("rules") + if (!rules) { + setTimeout(() => { + refreshRules(); + }, 60 * 1000); + } + + const alarm = await chrome.alarms.get("refreshRulesAlarm"); + if (!alarm) { + chrome.alarms.create('refreshRulesAlarm', { + periodInMinutes: config.refreshTimeout / 60, + }); + } +} diff --git a/src/lib/config.ts b/src/lib/config.ts index 78da0ea..57749b0 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -35,7 +35,7 @@ export const defaultConfig = { bazqux: false, local: true, }, - refreshTimeout: 5 * 60 * 60, + refreshTimeout: 2 * 60 * 60, // typical UA: // Firefox: Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0 // Chromium/Chrome: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36 @@ -46,7 +46,7 @@ export const defaultConfig = { }; export async function getConfig() { - return _.merge({}, defaultConfig, await storage.get("config")); + return _.merge({}, defaultConfig, await storage.get("config")) as typeof defaultConfig; } export async function setConfig(config: Partial) { diff --git a/src/lib/rules.ts b/src/lib/rules.ts index 588af44..077203d 100644 --- a/src/lib/rules.ts +++ b/src/lib/rules.ts @@ -11,7 +11,11 @@ export function parseRules(rules: string, forceJSON?: boolean) { incomeRules = window['lave'.split('').reverse().join('')](rules); console.warn('lave'); } else { - incomeRules = JSON.parse(rules); + try { + incomeRules = JSON.parse(rules); + } catch (error) { + console.error(error); + } } } } diff --git a/src/options/routes/General.tsx b/src/options/routes/General.tsx index 8323dfe..172065c 100644 --- a/src/options/routes/General.tsx +++ b/src/options/routes/General.tsx @@ -96,6 +96,7 @@ function General() {

{chrome.i18n.getMessage("totalNumberOfRules")}: {count}

+

{chrome.i18n.getMessage("updateTip")}