feat: rules updating alarms

This commit is contained in:
DIYgod 2023-12-14 23:17:23 +08:00
parent 7111b22cc5
commit 74baa6d5cd
No known key found for this signature in database
8 changed files with 47 additions and 4 deletions

View File

@ -124,5 +124,8 @@
},
"updateSuccessful": {
"message": "Update successful"
},
"updateTip": {
"message": "Automatically update every two hours, you can also manually update."
}
}

View File

@ -124,5 +124,8 @@
},
"updateSuccessful": {
"message": "更新成功"
},
"updateTip": {
"message": "每两小时自动更新一次,你也可以手动更新"
}
}

View File

@ -61,6 +61,11 @@
"notifications",
"alarms",
"idle"
]
],
"browser_specific_settings": {
"gecko": {
"id": "i@diygod.me"
}
}
}
}

View File

@ -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') {

View File

@ -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,
});
}
}

View File

@ -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<typeof defaultConfig>) {

View File

@ -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);
}
}
}
}

View File

@ -96,6 +96,7 @@ function General() {
<div className="grid w-full items-center gap-2">
<Label>{chrome.i18n.getMessage("rules")}</Label>
<p className="text-zinc-500 text-sm">{chrome.i18n.getMessage("totalNumberOfRules")}: {count}</p>
<p className="text-zinc-500 text-sm">{chrome.i18n.getMessage("updateTip")}</p>
<Button
variant="secondary"
disabled={rulesUpdating}