2023-08-04 16:48:40 +08:00
|
|
|
import { browser } from "webextension-polyfill";
|
2023-07-20 13:45:41 +08:00
|
|
|
import {
|
|
|
|
|
MSG_FETCH,
|
|
|
|
|
DEFAULT_SETTING,
|
|
|
|
|
DEFAULT_RULES,
|
2023-07-31 15:08:51 +08:00
|
|
|
DEFAULT_SYNC,
|
2023-07-20 13:45:41 +08:00
|
|
|
STOKEY_SETTING,
|
|
|
|
|
STOKEY_RULES,
|
2023-07-31 15:08:51 +08:00
|
|
|
STOKEY_SYNC,
|
2023-07-20 13:45:41 +08:00
|
|
|
CACHE_NAME,
|
|
|
|
|
} from "./config";
|
2023-08-04 16:05:14 +08:00
|
|
|
import { fetchData } from "./libs/fetch";
|
2023-07-20 13:45:41 +08:00
|
|
|
import storage from "./libs/storage";
|
|
|
|
|
import { getSetting } from "./libs";
|
2023-07-31 15:08:51 +08:00
|
|
|
import { syncAll } from "./libs/sync";
|
2023-07-20 13:45:41 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 插件安装
|
|
|
|
|
*/
|
|
|
|
|
browser.runtime.onInstalled.addListener(() => {
|
|
|
|
|
console.log("onInstalled");
|
|
|
|
|
storage.trySetObj(STOKEY_SETTING, DEFAULT_SETTING);
|
|
|
|
|
storage.trySetObj(STOKEY_RULES, DEFAULT_RULES);
|
2023-07-31 15:08:51 +08:00
|
|
|
storage.trySetObj(STOKEY_SYNC, DEFAULT_SYNC);
|
2023-07-20 13:45:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 浏览器启动
|
|
|
|
|
*/
|
|
|
|
|
browser.runtime.onStartup.addListener(async () => {
|
|
|
|
|
console.log("onStartup");
|
2023-07-31 03:10:09 +08:00
|
|
|
|
|
|
|
|
// 同步数据
|
2023-07-31 15:08:51 +08:00
|
|
|
await syncAll();
|
2023-07-31 03:10:09 +08:00
|
|
|
|
|
|
|
|
// 清除缓存
|
2023-07-20 13:45:41 +08:00
|
|
|
const { clearCache } = await getSetting();
|
|
|
|
|
if (clearCache) {
|
|
|
|
|
caches.delete(CACHE_NAME);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 监听消息
|
|
|
|
|
*/
|
|
|
|
|
browser.runtime.onMessage.addListener(
|
|
|
|
|
({ action, args }, sender, sendResponse) => {
|
|
|
|
|
switch (action) {
|
|
|
|
|
case MSG_FETCH:
|
2023-08-04 16:05:14 +08:00
|
|
|
fetchData(args.input, args.init, args.opts)
|
2023-07-20 13:45:41 +08:00
|
|
|
.then((data) => {
|
|
|
|
|
sendResponse({ data });
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
sendResponse({ error: error.message });
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
sendResponse({ error: `message action is unavailable: ${action}` });
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
);
|