diff --git a/global.d.ts b/global.d.ts index c9d69dd..fdcab28 100644 --- a/global.d.ts +++ b/global.d.ts @@ -43,9 +43,12 @@ declare interface Window { GM_xmlhttpRequest(init: GM_RequestParams); - GM_getValue(k: string, def: any): any; + GM_getValue(k: string, def: any): unknown; GM_setValue(k: string, v: any): void; + + // TODO 临时测试用 + [key: string]: unknown; } declare interface GM_RequestParams { diff --git a/src/class/Global.ts b/src/class/Global.ts index 8406599..958ab22 100644 --- a/src/class/Global.ts +++ b/src/class/Global.ts @@ -66,7 +66,22 @@ export default class Global extends WuhuBase implements IGlobal { if (this.unsafeWindow) { try { - window = this.unsafeWindow; + window.whtest = '原window'; + this.unsafeWindow.whtest = 'unsafeWindow'; + window = this.unsafeWindow || this.window; + Log.info('替换window上下文'); + Log.info({ + setInterval, clearInterval, + 'window.setInterval': window.setInterval, + 'window.clearInterval': window.clearInterval, + 'this.unsafeWindow.setInterval': this.unsafeWindow.setInterval, + 'this.unsafeWindow.clearInterval': this.unsafeWindow.clearInterval, + 'this.window.setInterval': this.window.setInterval, + 'this.window.clearInterval': this.window.clearInterval, + }) + // Log.info(window.whtest) + // Log.info(this.unsafeWindow.whtest) + // Log.info(this.window.whtest) } catch { this.unsafeWindow = null; this.GM_xmlhttpRequest = null; diff --git a/src/class/Log.ts b/src/class/Log.ts index 371bd60..541ab92 100644 --- a/src/class/Log.ts +++ b/src/class/Log.ts @@ -1,6 +1,8 @@ -export default class Log{ - static info(this, ...o) { - (this.debug()) && (console.log('[WH]', this.getTime(), ...o)) +export default class Log { + static info(...o) { + if (this.debug()) { + console.log('[WH]', this.getTime(), ...o) + } } static error(...o) { @@ -30,7 +32,4 @@ export default class Log{ let ms = ('00' + d.getMilliseconds()).slice(-3); return `[${ year }-${ month }-${ date } ${ hours }:${ minutes }:${ seconds }.${ ms }]`; } - - private constructor() { - } } \ No newline at end of file diff --git a/src/class/ZhongIcon.ts b/src/class/ZhongIcon.ts index 58504e0..160d0c9 100644 --- a/src/class/ZhongIcon.ts +++ b/src/class/ZhongIcon.ts @@ -16,6 +16,7 @@ import WuhuBase from "./WuhuBase"; import Log from "./Log"; import Utils from "./utils/Utils"; import WuhuConfig from "./WuhuConfig"; +import Alert from "./utils/Alert"; export default class ZhongIcon extends WuhuBase { static ZhongNode: MyHTMLElement = null; @@ -967,8 +968,7 @@ color:black; // Log.info(await Utils.getSidebarData()) // Log.info(await Utils.getUserState()) - Log.info(ZhongIcon.getGlob()); - Log.info(ZhongIcon.glob); + new Alert('123', { sysNotify: true }); Log.info('测试结束'); }, diff --git a/src/class/utils/Alert.ts b/src/class/utils/Alert.ts index 82449de..d4df0bd 100644 --- a/src/class/utils/Alert.ts +++ b/src/class/utils/Alert.ts @@ -1,13 +1,15 @@ import Utils from "./Utils"; import addStyle from "../../func/utils/addStyle"; +import Log from "../Log"; +import IWHNotify from "../../interface/IWHNotify"; +import NotificationUtils from "./NotificationUtils"; export default class Alert extends Utils { - // static hasContainer: boolean = false; static container: HTMLElement = null; notify: MyHTMLElement = null; intervalID = -1; - constructor(msg: string, options: WHNotifyOpt = {}) { + constructor(msg: string, options: IWHNotify = {}) { super(); let { isWindowActive, notifies } = Alert.glob; @@ -24,14 +26,15 @@ export default class Alert extends Utils { if (!Alert.glob.isWindowActive.get() || (self !== top)) return null; // 通知的容器 - if (!Alert.container) Alert.initContainer(); - this.notify = Alert.create(msg, timeout); - this.intervalID = this.notify.loopId; + if (Alert.container === null) Alert.initContainer(); + this.callback = callback; + Alert.create(this, msg, timeout); + Log.info('创建新通知:', this); + NotificationUtils.push(msg, options); } - static create(msg, timeout) { - const date = new Date(); + static create(that: Alert, msg, timeout): void { // 通知的唯一id const uid = '' + Utils.getRandomInt(1000, 9999); // 每条通知 @@ -44,6 +47,7 @@ export default class Alert extends Utils {

${ msg }

`; this.container.append(new_node); + // TODO 待定 this.container['msgInnerText'] = new_node.querySelector('.wh-notify-msg').innerText; // 进度条node const progressBar: HTMLElement = new_node.querySelector('.wh-notify-bar'); @@ -53,14 +57,9 @@ export default class Alert extends Utils { new_node.addEventListener('mouseleave', () => mouse_enter = false); // 通知进度条 let progressCount = 101; - // 删除通知 - // new_node.close = () => { - // clearInterval(intervalID); - // new_node.remove(); - // callback(); - // }; // 计时器 - let intervalID = window.setInterval(() => { + that.intervalID = window.setInterval(() => { + // Log.info(that.intervalID); if (mouse_enter) { progressCount = 101; progressBar.style.width = '100%'; @@ -68,11 +67,13 @@ export default class Alert extends Utils { } progressCount--; progressBar.style.width = `${ progressCount }%`; - if (progressCount === 0) new_node.remove(); - }, timeout * 1000 / 100); - new_node.querySelector('.wh-notify-close').addEventListener('click', new_node.close); - new_node.loopId = intervalID; - return new_node; + if (progressCount === 0) { + that.close(); + } + }, timeout * 1000 / 100) as unknown as number; + new_node.querySelector('.wh-notify-close').addEventListener('click', that.close); + that.notify = new_node; + Log.info(that.notify) } callback: Function = () => null; @@ -126,16 +127,14 @@ cursor: pointer; close() { this.notify.remove(); + // Log.info('父元素:', this.notify.parentElement); this.notify = null; this.callback(); - clearInterval(this.intervalID); + let id = this.intervalID; + // Log.info('this.intervalID', { id }); + // Alert.glob.window.clearInterval(id); + // Alert.glob.unsafeWindow.clearInterval(id); + // clearInterval(id); + window.clearInterval(id); } -} - -interface WHNotifyOpt { - timeout?: number; - callback?: Function; - sysNotify?: boolean; - sysNotifyTag?: string; - sysNotifyClick?: Function; } \ No newline at end of file diff --git a/src/class/utils/NotificationUtils.ts b/src/class/utils/NotificationUtils.ts new file mode 100644 index 0000000..12e7202 --- /dev/null +++ b/src/class/utils/NotificationUtils.ts @@ -0,0 +1,38 @@ +import Utils from "./Utils"; +import IWHNotify from "../../interface/IWHNotify"; +import Log from "../Log"; + +export default class NotificationUtils extends Utils { + static permission: boolean = window.Notification && window.Notification.permission === 'granted'; + + static push(msg: string, options: IWHNotify = {}) { + Log.info('推送系统通知', { 'permission': this.permission, flag: options.sysNotify }); + let { notifies } = NotificationUtils.glob; + + if (options.sysNotify && this.permission) { + let tmpNode = document.createElement('p'); + tmpNode.innerHTML = msg; + let notify = new Notification('芜湖助手', { + body: Log.getTime() + tmpNode.innerText, + requireInteraction: true, + renotify: true, + tag: '芜湖助手' + Utils.getRandomInt(0, 99), + }); + let id = notifies.count++; + // notify.id = notifies.count++; + notifies[id] = notify; + notify.addEventListener( + 'close', + () => { + options.sysNotifyClick ? options.sysNotifyClick() : null; + notifies[id] = null; + window.focus(); + } + ); + notify.addEventListener( + 'show', + () => setTimeout(() => notify.close(), (options.timeout || 3) * 1000) + ); + } + } +} \ No newline at end of file diff --git a/src/interface/IWHNotify.ts b/src/interface/IWHNotify.ts new file mode 100644 index 0000000..b76ad69 --- /dev/null +++ b/src/interface/IWHNotify.ts @@ -0,0 +1,7 @@ +export default interface IWHNotify { + timeout?: number; + callback?: Function; + sysNotify?: boolean; + sysNotifyTag?: string; + sysNotifyClick?: Function; +} \ No newline at end of file