From f2c04eadb25839b4a27ca1c806513094db2ac44a Mon Sep 17 00:00:00 2001 From: xu_yanfeng Date: Mon, 24 Feb 2025 15:15:12 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=B6=88=E6=81=AF=E5=8F=AF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scripts/background/notify.ts | 63 +++++++++++++++++++++++++++- src/scripts/inject-view/loader.ts | 69 +++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/src/scripts/background/notify.ts b/src/scripts/background/notify.ts index dcad929..2d8e702 100644 --- a/src/scripts/background/notify.ts +++ b/src/scripts/background/notify.ts @@ -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; + /** + * 检查是否可以创建通知 + * @returns 返回true,则会创建通知 + */ + check: (item: ConfigItem) => Promise; 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); } diff --git a/src/scripts/inject-view/loader.ts b/src/scripts/inject-view/loader.ts index d400457..1efdd5a 100644 --- a/src/scripts/inject-view/loader.ts +++ b/src/scripts/inject-view/loader.ts @@ -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 = {}; data: Array = []; + /** + * 通知的数据 + */ + 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) => {