This commit is contained in:
Liwanyi 2022-09-26 19:10:40 +08:00
parent e6b3477c71
commit a97bc2cd01
7 changed files with 99 additions and 38 deletions

5
global.d.ts vendored
View File

@ -43,9 +43,12 @@ declare interface Window {
GM_xmlhttpRequest(init: GM_RequestParams); 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; GM_setValue(k: string, v: any): void;
// TODO 临时测试用
[key: string]: unknown;
} }
declare interface GM_RequestParams { declare interface GM_RequestParams {

View File

@ -66,7 +66,22 @@ export default class Global extends WuhuBase implements IGlobal {
if (this.unsafeWindow) { if (this.unsafeWindow) {
try { 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 { } catch {
this.unsafeWindow = null; this.unsafeWindow = null;
this.GM_xmlhttpRequest = null; this.GM_xmlhttpRequest = null;

View File

@ -1,6 +1,8 @@
export default class Log { export default class Log {
static info(this, ...o) { static info(...o) {
(this.debug()) && (console.log('[WH]', this.getTime(), ...o)) if (this.debug()) {
console.log('[WH]', this.getTime(), ...o)
}
} }
static error(...o) { static error(...o) {
@ -30,7 +32,4 @@ export default class Log{
let ms = ('00' + d.getMilliseconds()).slice(-3); let ms = ('00' + d.getMilliseconds()).slice(-3);
return `[${ year }-${ month }-${ date } ${ hours }:${ minutes }:${ seconds }.${ ms }]`; return `[${ year }-${ month }-${ date } ${ hours }:${ minutes }:${ seconds }.${ ms }]`;
} }
private constructor() {
}
} }

View File

@ -16,6 +16,7 @@ import WuhuBase from "./WuhuBase";
import Log from "./Log"; import Log from "./Log";
import Utils from "./utils/Utils"; import Utils from "./utils/Utils";
import WuhuConfig from "./WuhuConfig"; import WuhuConfig from "./WuhuConfig";
import Alert from "./utils/Alert";
export default class ZhongIcon extends WuhuBase { export default class ZhongIcon extends WuhuBase {
static ZhongNode: MyHTMLElement = null; static ZhongNode: MyHTMLElement = null;
@ -967,8 +968,7 @@ color:black;
// Log.info(await Utils.getSidebarData()) // Log.info(await Utils.getSidebarData())
// Log.info(await Utils.getUserState()) // Log.info(await Utils.getUserState())
Log.info(ZhongIcon.getGlob()); new Alert('123', { sysNotify: true });
Log.info(ZhongIcon.glob);
Log.info('测试结束'); Log.info('测试结束');
}, },

View File

@ -1,13 +1,15 @@
import Utils from "./Utils"; import Utils from "./Utils";
import addStyle from "../../func/utils/addStyle"; 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 { export default class Alert extends Utils {
// static hasContainer: boolean = false;
static container: HTMLElement = null; static container: HTMLElement = null;
notify: MyHTMLElement = null; notify: MyHTMLElement = null;
intervalID = -1; intervalID = -1;
constructor(msg: string, options: WHNotifyOpt = {}) { constructor(msg: string, options: IWHNotify = {}) {
super(); super();
let { isWindowActive, notifies } = Alert.glob; 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.glob.isWindowActive.get() || (self !== top)) return null;
// 通知的容器 // 通知的容器
if (!Alert.container) Alert.initContainer(); if (Alert.container === null) Alert.initContainer();
this.notify = Alert.create(msg, timeout);
this.intervalID = this.notify.loopId;
this.callback = callback; this.callback = callback;
Alert.create(this, msg, timeout);
Log.info('创建新通知:', this);
NotificationUtils.push(msg, options);
} }
static create(msg, timeout) { static create(that: Alert, msg, timeout): void {
const date = new Date();
// 通知的唯一id // 通知的唯一id
const uid = '' + Utils.getRandomInt(1000, 9999); const uid = '' + Utils.getRandomInt(1000, 9999);
// 每条通知 // 每条通知
@ -44,6 +47,7 @@ export default class Alert extends Utils {
<div class="wh-notify-msg"><p>${ msg }</p></div> <div class="wh-notify-msg"><p>${ msg }</p></div>
</div>`; </div>`;
this.container.append(new_node); this.container.append(new_node);
// TODO 待定
this.container['msgInnerText'] = new_node.querySelector('.wh-notify-msg').innerText; this.container['msgInnerText'] = new_node.querySelector('.wh-notify-msg').innerText;
// 进度条node // 进度条node
const progressBar: HTMLElement = new_node.querySelector('.wh-notify-bar'); 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); new_node.addEventListener('mouseleave', () => mouse_enter = false);
// 通知进度条 // 通知进度条
let progressCount = 101; 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) { if (mouse_enter) {
progressCount = 101; progressCount = 101;
progressBar.style.width = '100%'; progressBar.style.width = '100%';
@ -68,11 +67,13 @@ export default class Alert extends Utils {
} }
progressCount--; progressCount--;
progressBar.style.width = `${ progressCount }%`; progressBar.style.width = `${ progressCount }%`;
if (progressCount === 0) new_node.remove(); if (progressCount === 0) {
}, timeout * 1000 / 100); that.close();
new_node.querySelector('.wh-notify-close').addEventListener('click', new_node.close); }
new_node.loopId = intervalID; }, timeout * 1000 / 100) as unknown as number;
return new_node; new_node.querySelector('.wh-notify-close').addEventListener('click', that.close);
that.notify = new_node;
Log.info(that.notify)
} }
callback: Function = () => null; callback: Function = () => null;
@ -126,16 +127,14 @@ cursor: pointer;
close() { close() {
this.notify.remove(); this.notify.remove();
// Log.info('父元素:', this.notify.parentElement);
this.notify = null; this.notify = null;
this.callback(); 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;
}

View File

@ -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)
);
}
}
}

View File

@ -0,0 +1,7 @@
export default interface IWHNotify {
timeout?: number;
callback?: Function;
sysNotify?: boolean;
sysNotifyTag?: string;
sysNotifyClick?: Function;
}