通知消息可配置

This commit is contained in:
xu_yanfeng 2025-02-24 15:15:12 +08:00
parent 4518ab94e9
commit f2c04eadb2
2 changed files with 131 additions and 1 deletions

View File

@ -3,6 +3,8 @@ import PKG from "../../../cc-plugin.config";
import { ga } from "../../ga";
import { GA_EventName } from "../../ga/type";
import { githubMirrorMgr } from "../inject-view/github";
import { getAdData, NotifyButton } from "../inject-view/loader";
export const TipUpdate = "tip-update";
(async () => {
interface ConfigItem {
@ -11,7 +13,11 @@ export const TipUpdate = "tip-update";
closed?: Function;
title: string;
message: string;
check: (a: ConfigItem) => Promise<boolean>;
/**
*
* @returns true
*/
check: (item: ConfigItem) => Promise<boolean>;
buttons?: Array<{ title: string; click?: Function }>;
}
function goRate() {
@ -97,6 +103,7 @@ export const TipUpdate = "tip-update";
];
try {
await githubMirrorMgr.init();
// 版本检查
const data = await githubMirrorMgr.getData("version.json");
if (data) {
const info = data as { ver: string };
@ -115,6 +122,60 @@ export const TipUpdate = "tip-update";
});
}
}
// 广告分析
const adData = await getAdData();
if (adData && adData.notify.length) {
adData.notify.forEach((el) => {
const KeyIKnow = `i-know-${el.id}`;
config.push({
id: el.id,
title: el.title,
message: el.msg,
buttons: el.buttons.map((btn) => {
const map = {};
map[NotifyButton.IKnow] = "我知道了";
map[NotifyButton.Go] = "前往";
return {
title: map[btn],
click: () => {
if (btn === NotifyButton.Go) {
chrome.tabs.create({ url: el.url });
} else if (btn === NotifyButton.IKnow) {
chrome.storage.local.set({ [`${KeyIKnow}`]: true });
}
},
};
}),
click: () => {
chrome.tabs.create({ url: el.url });
},
check: async () => {
// 检查是否已经过期
const now = new Date().getTime();
if (el.deadTime && now > new Date(el.deadTime).getTime()) {
return false;
}
// 检查是否已经取消了
const result = await chrome.storage.local.get(KeyIKnow);
if (result[KeyIKnow]) {
return false;
}
// 距离上次是否已经过去了指定的时间间隔
const KeyLatestShowTime = `latest-show-${el.id}`;
const res = await chrome.storage.local.get(KeyLatestShowTime);
const time = res[KeyLatestShowTime];
if (time) {
const diff = (new Date().getTime() - time) / 1000 / 60; // 过去了多少分钟
if (diff <= el.duration) {
return false;
}
}
chrome.storage.local.set({ [KeyLatestShowTime]: new Date().getTime() });
return true;
},
});
});
}
} catch (e) {
console.error(e);
}

View File

@ -71,6 +71,64 @@ export class AdItem {
}
}
}
export enum NotifyButton {
/**
*
*/
IKnow = "IKnow",
/**
*
*/
Go = "Go",
}
export class Notify {
/**通知的id用来做点击区分的 */
id: string;
/**
*
*/
title: string;
/**
*
*/
msg: string;
/**
* url
*/
url: string;
/**
*
* @example 2025/02/24 14:00:00
* @example 2025/02/24
*/
deadTime: string;
/**
*
*/
duration: number = 0;
/**
*
*/
buttons: NotifyButton[] = [];
parse(data: Notify) {
this.id = data.id || data.url;
this.title = data.title;
this.msg = data.msg;
this.url = data.url;
this.deadTime = data.deadTime || Date.now().toString();
this.duration = data.duration || 0;
this.buttons = [];
if (data.buttons) {
data.buttons.forEach((btn) => {
[NotifyButton.IKnow, NotifyButton.Go].forEach((btnName) => {
if (btn === btnName) {
this.buttons.push(btnName);
}
});
});
}
}
}
export class AdData {
desc: string = "";
/**
@ -98,6 +156,10 @@ export class AdData {
*/
keys: Record<string, string> = {};
data: Array<AdItem> = [];
/**
*
*/
notify: Notify[] = [];
parse(data: AdData) {
this.desc = data.desc;
this.valid = !!data.valid;
@ -120,6 +182,13 @@ export class AdData {
}
});
}
if (data.notify) {
this.notify = data.notify.map((el) => {
const notify = new Notify();
notify.parse(el);
return notify;
});
}
}
sortByName() {
this.data.sort((a, b) => {