mirror of
https://github.com/tidys/cc-inspector-chrome
synced 2025-04-12 05:01:03 +00:00
通知消息可配置
This commit is contained in:
parent
4518ab94e9
commit
f2c04eadb2
@ -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);
|
||||
}
|
||||
|
@ -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) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user