mirror of
https://github.com/tidys/cc-inspector-chrome
synced 2025-04-18 16:08:42 +00:00
增加版本更新的通知
This commit is contained in:
parent
063aa8a12b
commit
622537d3c4
@ -1,6 +1,9 @@
|
|||||||
|
import { gt } from "semver";
|
||||||
import PKG from "../../../cc-plugin.config";
|
import PKG from "../../../cc-plugin.config";
|
||||||
import { ga } from "../../ga";
|
import { ga } from "../../ga";
|
||||||
import { GA_EventName } from "../../ga/type";
|
import { GA_EventName } from "../../ga/type";
|
||||||
|
import { githubMirrorMgr } from "../inject-view/github";
|
||||||
|
export const TipUpdate = "tip-update";
|
||||||
(async () => {
|
(async () => {
|
||||||
interface ConfigItem {
|
interface ConfigItem {
|
||||||
id: string;
|
id: string;
|
||||||
@ -8,14 +11,7 @@ import { GA_EventName } from "../../ga/type";
|
|||||||
closed?: Function;
|
closed?: Function;
|
||||||
title: string;
|
title: string;
|
||||||
message: string;
|
message: string;
|
||||||
/**
|
check: (a: ConfigItem) => Promise<boolean>;
|
||||||
* 距离安装时间多久才会弹出来,单位秒
|
|
||||||
*/
|
|
||||||
afterInstall: number;
|
|
||||||
/**
|
|
||||||
* 距离上次弹出来多久才会弹出来,单位秒
|
|
||||||
*/
|
|
||||||
afterLatestShow: number;
|
|
||||||
buttons?: Array<{ title: string; click?: Function }>;
|
buttons?: Array<{ title: string; click?: Function }>;
|
||||||
}
|
}
|
||||||
function goRate() {
|
function goRate() {
|
||||||
@ -29,24 +25,64 @@ import { GA_EventName } from "../../ga/type";
|
|||||||
ga.fireEventWithParam(GA_EventName.Rate, "go");
|
ga.fireEventWithParam(GA_EventName.Rate, "go");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const KeyHasRate = "has-rate";
|
||||||
const config: ConfigItem[] = [
|
const config: ConfigItem[] = [
|
||||||
{
|
{
|
||||||
id: "rate",
|
id: "rate",
|
||||||
title: "Hi",
|
title: "Hi",
|
||||||
message: "如果不是真爱,你也不会使用这么长时间,求五星好评!",
|
message: "如果不是真爱,你也不会使用这么长时间,求五星好评!",
|
||||||
afterInstall: 60 * 60 * 24 * 3, // 安装3天后
|
|
||||||
afterLatestShow: 60 * 60 * 24 * 1, // 一天一次
|
|
||||||
click: () => {
|
click: () => {
|
||||||
goRate();
|
goRate();
|
||||||
},
|
},
|
||||||
closed: () => {
|
closed: () => {
|
||||||
console.log("closed");
|
console.log("closed");
|
||||||
},
|
},
|
||||||
|
check: async (cfg: ConfigItem) => {
|
||||||
|
const result = await chrome.storage.local.get(KeyHasRate);
|
||||||
|
if (result[KeyHasRate]) {
|
||||||
|
// 已经评价过了
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const KeyInstallTime = "install-time";
|
||||||
|
const KeyLatestShowTime = "latest-show-time";
|
||||||
|
|
||||||
|
let res1 = await chrome.storage.local.get(KeyInstallTime);
|
||||||
|
const time1 = res1[KeyInstallTime];
|
||||||
|
if (!time1) {
|
||||||
|
// 首次安装
|
||||||
|
chrome.storage.local.set({ [KeyInstallTime]: new Date().getTime() });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const diff = (new Date().getTime() - time1) / 1000;
|
||||||
|
const afterInstall = 60 * 60 * 24 * 3; // 安装3天后
|
||||||
|
if (diff <= afterInstall) {
|
||||||
|
// 安装后一段时间不显示
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let canShow = false;
|
||||||
|
const res = await chrome.storage.local.get(KeyLatestShowTime);
|
||||||
|
const time = res[KeyLatestShowTime];
|
||||||
|
if (time) {
|
||||||
|
// 检查距离上次弹出是否超过指定时间
|
||||||
|
const diff = (new Date().getTime() - time) / 1000;
|
||||||
|
const afterLatestShow = 60 * 60 * 24 * 1; // 一天一次
|
||||||
|
canShow = diff > afterLatestShow;
|
||||||
|
} else {
|
||||||
|
// 首次弹出
|
||||||
|
canShow = true;
|
||||||
|
}
|
||||||
|
if (!canShow) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
chrome.storage.local.set({ [KeyLatestShowTime]: new Date().getTime() });
|
||||||
|
return true;
|
||||||
|
},
|
||||||
buttons: [
|
buttons: [
|
||||||
{
|
{
|
||||||
title: "我已评价",
|
title: "我已评价",
|
||||||
click: () => {
|
click: () => {
|
||||||
chrome.storage.local.set({ [HasRate]: true });
|
chrome.storage.local.set({ [KeyHasRate]: true });
|
||||||
ga.fireEventWithParam(GA_EventName.Rate, "has rate");
|
ga.fireEventWithParam(GA_EventName.Rate, "has rate");
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -59,7 +95,25 @@ import { GA_EventName } from "../../ga/type";
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
await githubMirrorMgr.init();
|
||||||
|
const data = await githubMirrorMgr.getData("version.json");
|
||||||
|
if (data) {
|
||||||
|
const info = data as { ver: string };
|
||||||
|
const b = gt(info.ver || "0.0.0", PKG.manifest.version);
|
||||||
|
if (info.ver && b) {
|
||||||
|
config.push({
|
||||||
|
id: "update",
|
||||||
|
title: `${PKG.manifest.name}发现新版本${info.ver || ""}`,
|
||||||
|
message: `点击查看`,
|
||||||
|
click: () => {
|
||||||
|
goRate();
|
||||||
|
},
|
||||||
|
check: async () => {
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
chrome.notifications.onClicked.addListener((id) => {
|
chrome.notifications.onClicked.addListener((id) => {
|
||||||
const ret = config.find((el) => el.id === id);
|
const ret = config.find((el) => el.id === id);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
@ -83,46 +137,17 @@ import { GA_EventName } from "../../ga/type";
|
|||||||
}
|
}
|
||||||
btn.click && btn.click();
|
btn.click && btn.click();
|
||||||
});
|
});
|
||||||
const InstallTime = "install-time";
|
|
||||||
const LatestShowTime = "latest-show-time";
|
|
||||||
const HasRate = "has-rate";
|
|
||||||
|
|
||||||
let res = await chrome.storage.local.get(InstallTime);
|
|
||||||
const time = res[InstallTime];
|
|
||||||
if (!time) {
|
|
||||||
// 首次安装
|
|
||||||
chrome.storage.local.set({ [InstallTime]: new Date().getTime() });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const diff = (new Date().getTime() - time) / 1000;
|
|
||||||
for (let i = 0; i < config.length; i++) {
|
for (let i = 0; i < config.length; i++) {
|
||||||
const { title, afterInstall, buttons, message, id } = config[i];
|
await createNotification(config[i]);
|
||||||
if (diff > afterInstall) {
|
|
||||||
await createNotification(config[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createNotification(config: ConfigItem) {
|
async function createNotification(config: ConfigItem) {
|
||||||
let canShow = false;
|
const b = await config.check(config);
|
||||||
const res = await chrome.storage.local.get(LatestShowTime);
|
if (!b) {
|
||||||
const time = res[LatestShowTime];
|
|
||||||
if (time) {
|
|
||||||
const diff = (new Date().getTime() - time) / 1000;
|
|
||||||
canShow = diff > config.afterLatestShow;
|
|
||||||
} else {
|
|
||||||
// 首次弹出
|
|
||||||
canShow = true;
|
|
||||||
}
|
|
||||||
if (!canShow) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await chrome.storage.local.get(HasRate);
|
const { title, buttons, message, id } = config;
|
||||||
if (result[HasRate]) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
chrome.storage.local.set({ [LatestShowTime]: new Date().getTime() });
|
|
||||||
const { title, afterInstall, buttons, message, id } = config;
|
|
||||||
chrome.notifications.create(
|
chrome.notifications.create(
|
||||||
id,
|
id,
|
||||||
{
|
{
|
||||||
|
@ -11,27 +11,45 @@ export interface MirrorInfo {
|
|||||||
class Config {
|
class Config {
|
||||||
private key = "cc-inspector-ad-config";
|
private key = "cc-inspector-ad-config";
|
||||||
private data: MirrorInfo[] = [];
|
private data: MirrorInfo[] = [];
|
||||||
constructor() {
|
async init() {
|
||||||
const cfg = localStorage.getItem(this.key);
|
try {
|
||||||
if (cfg) {
|
let str: string = "";
|
||||||
try {
|
if (this.isChromeBackgroundEnv()) {
|
||||||
const ret = JSON.parse(cfg) as MirrorInfo[];
|
const ret = await chrome.storage.local.get(this.key);
|
||||||
|
if (ret && ret[this.key]) {
|
||||||
|
str = ret[this.key] as string;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
str = localStorage.getItem(this.key);
|
||||||
|
}
|
||||||
|
if (str) {
|
||||||
|
const ret = JSON.parse(str) as MirrorInfo[];
|
||||||
if (ret) {
|
if (ret) {
|
||||||
ret.forEach((el) => {
|
ret.forEach((el) => {
|
||||||
this.data.push({ name: el.name, time: el.time });
|
this.data.push({ name: el.name, time: el.time });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch {}
|
}
|
||||||
|
} catch {
|
||||||
|
debugger;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
save(name: string, time: number) {
|
private isChromeBackgroundEnv() {
|
||||||
|
return typeof chrome !== "undefined" && typeof chrome.storage !== "undefined" && typeof chrome.storage.local !== "undefined";
|
||||||
|
}
|
||||||
|
async save(name: string, time: number) {
|
||||||
const ret = this.data.find((el) => el.name === name);
|
const ret = this.data.find((el) => el.name === name);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
ret.time = time;
|
ret.time = time;
|
||||||
} else {
|
} else {
|
||||||
this.data.push({ name: name, time: time } as MirrorInfo);
|
this.data.push({ name: name, time: time } as MirrorInfo);
|
||||||
}
|
}
|
||||||
localStorage.setItem(this.key, JSON.stringify(this.data));
|
const saveString = JSON.stringify(this.data);
|
||||||
|
if (this.isChromeBackgroundEnv()) {
|
||||||
|
await chrome.storage.local.set({ [this.key]: saveString });
|
||||||
|
} else {
|
||||||
|
localStorage.setItem(this.key, saveString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
getTime(url: string) {
|
getTime(url: string) {
|
||||||
const ret = this.data.find((el) => el.name === url);
|
const ret = this.data.find((el) => el.name === url);
|
||||||
@ -54,7 +72,7 @@ export class GithubMirror {
|
|||||||
*/
|
*/
|
||||||
name: string = "";
|
name: string = "";
|
||||||
private calcUrl: Function;
|
private calcUrl: Function;
|
||||||
constructor(name: string, cb) {
|
constructor(name: string, cb: Function) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.time = cfg.getTime(name);
|
this.time = cfg.getTime(name);
|
||||||
this.calcUrl = cb;
|
this.calcUrl = cb;
|
||||||
@ -79,7 +97,7 @@ export class GithubMirror {
|
|||||||
}
|
}
|
||||||
private reqFecth(url: string): Promise<Object | null> {
|
private reqFecth(url: string): Promise<Object | null> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.log(`req ad: ${url}`);
|
// console.log(`req ad: ${url}`);
|
||||||
fetch(url)
|
fetch(url)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
return res.json();
|
return res.json();
|
||||||
@ -96,7 +114,13 @@ export class GithubMirror {
|
|||||||
const cfg = new Config();
|
const cfg = new Config();
|
||||||
export class GithubMirrorMgr {
|
export class GithubMirrorMgr {
|
||||||
mirrors: GithubMirror[] = [];
|
mirrors: GithubMirror[] = [];
|
||||||
constructor() {
|
private _init: boolean = false;
|
||||||
|
async init() {
|
||||||
|
if (this._init) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await cfg.init();
|
||||||
|
this._init = true;
|
||||||
// 使用国内gitub镜像来达到下载远程配置文件的目的
|
// 使用国内gitub镜像来达到下载远程配置文件的目的
|
||||||
this.mirrors.push(
|
this.mirrors.push(
|
||||||
new GithubMirror("github", (owner: string, repo: string, branch: string, file: string) => {
|
new GithubMirror("github", (owner: string, repo: string, branch: string, file: string) => {
|
||||||
@ -132,6 +156,9 @@ export class GithubMirrorMgr {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
async getData(file: string): Promise<Object | null> {
|
async getData(file: string): Promise<Object | null> {
|
||||||
|
if (!this._init) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
this.mirrors.sort((a, b) => b.time - a.time);
|
this.mirrors.sort((a, b) => b.time - a.time);
|
||||||
for (let i = 0; i < this.mirrors.length; i++) {
|
for (let i = 0; i < this.mirrors.length; i++) {
|
||||||
const mirror = this.mirrors[i];
|
const mirror = this.mirrors[i];
|
||||||
@ -139,13 +166,17 @@ export class GithubMirrorMgr {
|
|||||||
if (data) {
|
if (data) {
|
||||||
const time = new Date().getTime();
|
const time = new Date().getTime();
|
||||||
mirror.time = time;
|
mirror.time = time;
|
||||||
cfg.save(mirror.name, time);
|
await cfg.save(mirror.name, time);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
getFileUrl(file: string): string {
|
getFileUrl(file: string): string {
|
||||||
|
if (!this._init) {
|
||||||
|
debugger;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -125,6 +125,7 @@ export class AdData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getAdData(): Promise<AdData | null> {
|
export async function getAdData(): Promise<AdData | null> {
|
||||||
|
await githubMirrorMgr.init();
|
||||||
const data = await githubMirrorMgr.getData(`ad-${CCPlugin.manifest.version}.json`);
|
const data = await githubMirrorMgr.getData(`ad-${CCPlugin.manifest.version}.json`);
|
||||||
if (data) {
|
if (data) {
|
||||||
const ad = new AdData();
|
const ad = new AdData();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user