TS重构
This commit is contained in:
parent
e6b3477c71
commit
a97bc2cd01
5
global.d.ts
vendored
5
global.d.ts
vendored
@ -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 {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
export default class Log {
|
||||
static info(this, ...o) {
|
||||
(this.debug()) && (console.log('[WH]', this.getTime(), ...o))
|
||||
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() {
|
||||
}
|
||||
}
|
||||
@ -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('测试结束');
|
||||
},
|
||||
|
||||
@ -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 {
|
||||
<div class="wh-notify-msg"><p>${ msg }</p></div>
|
||||
</div>`;
|
||||
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;
|
||||
}
|
||||
38
src/class/utils/NotificationUtils.ts
Normal file
38
src/class/utils/NotificationUtils.ts
Normal 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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
src/interface/IWHNotify.ts
Normal file
7
src/interface/IWHNotify.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export default interface IWHNotify {
|
||||
timeout?: number;
|
||||
callback?: Function;
|
||||
sysNotify?: boolean;
|
||||
sysNotifyTag?: string;
|
||||
sysNotifyClick?: Function;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user