2022-10-09 11:06:35 +08:00

88 lines
2.9 KiB
TypeScript

import Device from "../enum/Device";
import WuhuBase from "./WuhuBase";
import IGlobal from "../interface/IGlobal";
import Log from "./Log";
import InfoUtils from "./utils/InfoUtils";
import BuyBeerHelper from "./action/BuyBeerHelper";
export default class Global extends WuhuBase implements IGlobal {
GM_xmlhttpRequest: Function = null;
href: string = window.location.href;
// 弹窗
popup_node: MyHTMLElement = null;
// 啤酒助手
beer = null;
// 留存的通知
notifies: NotifyWrapper = null;
// 海外库存
fStock: { get: () => Promise<any> } = null;
// 玩家名和数字id
player_info = null;
// 设备类型
device: Device = null;
// PDA运行环境
isPDA: boolean = false;
// PDA自带apikey
PDA_APIKey: string = null;
// 脚本版本
version: string = null;
// window 副本
window: Window & typeof globalThis = window;
unsafeWindow: Window & typeof globalThis = null;
// document body 属性
bodyAttrs: {
'data-country'?: string;
'data-celebration'?: string;
'data-traveling'?: string;
'data-abroad'?: string;
// [key: string]: string;
} = null;
constructor() {
Log.info('WH脚本参数初始化');
super();
this.window = window;
this.unsafeWindow = window.unsafeWindow || null;
this.GM_xmlhttpRequest = window.GM_xmlhttpRequest || null;
this.version = '$$WUHU_DEV_VERSION$$';
this.PDA_APIKey = '###PDA-APIKEY###';
this.isPDA = !this.PDA_APIKey.includes('###');
this.device = window.innerWidth >= 1000 ? Device.PC : window.innerWidth <= 600 ? Device.MOBILE : Device.TABLET;
this.player_info = InfoUtils.getInstance().getPlayerInfo();
this.beer = BuyBeerHelper.getInstance();
this.popup_node = null;
this.notifies = { count: 0 };
this.href = window.location.href;
this.bodyAttrs = {};
if (this.unsafeWindow) {
try {
window = this.unsafeWindow || this.window;
Log.info('替换window上下文');
} catch {
this.unsafeWindow = null;
this.GM_xmlhttpRequest = null;
}
}
for (let i = 0; i < document.body.attributes.length; i++) {
let item = document.body.attributes.item(i);
this.bodyAttrs[item.name] = item.value;
}
// 当窗口关闭时关闭所有还存在的通知
window.addEventListener(
'beforeunload',
() => {
if (this.notifies.count !== 0) {
for (let i = 0; i < this.notifies.count; i++) {
(this.notifies[i] !== null) && (this.notifies[i].close())
}
}
}
);
Log.info('WH脚本参数初始化结束');
}
}