2022-09-09 18:03:47 +08:00

96 lines
2.8 KiB
TypeScript

import '../global'
import log from "./func/utils/log";
import getWhSettingObj from "./func/utils/getWhSettingObj";
import miniprofTrans from "./func/translate/miniprofTrans";
import Global from "./interface/GlobalVars";
import Device from "./enum/Device";
import getPlayerInfo from "./func/utils/getPlayerInfo";
import autoFetchJSON from "./func/utils/autoFetchJSON";
import priceWatcherHandle from "./func/utils/priceWatcherHandle";
// 初始化方法,获取必要全局参数
export default function init(glob: Global) {
glob.window = window;
window.WHPARAMS = glob;
let UWCopy = null;
if (window.hasOwnProperty('unsafeWindow')) {
UWCopy = window.unsafeWindow;
try {
window = UWCopy;
} catch {
}
}
glob.UWCopy = UWCopy;
// 脚本版本
glob.version = '$$WUHU_DEV_VERSION$$';
// iframe运行
glob.isIframe = self !== top;
// PDA
glob.PDA_APIKey = '###PDA-APIKEY###';
glob.isPDA = glob.PDA_APIKey.slice(-1) !== '#';
// 请求通知权限
if (window.Notification) {
Notification.requestPermission().then();
}
// 扩展String正则方法
String.prototype.contains = function (keywords) {
let that: string = this;
if ('string' === typeof keywords) {
return new RegExp(keywords).test(that);
} else {
return keywords.test(that);
}
};
// 监听fetch
const ori_fetch = window.fetch;
window.fetch = async (url: string, init: RequestInit) => {
if (url.contains('newsTickers')) {
// 阻止获取新闻横幅
return new Response('{}');
}
const res = await ori_fetch(url, init);
// mini profile 翻译
if (url.includes('profiles.php?step=getUserNameContextMenu') && getWhSettingObj()['transEnable']) {
setTimeout(() => miniprofTrans(), 200);
}
let clone = res.clone();
let text = await res.text();
log.info({url, init, text});
return clone;
};
glob.device = window.innerWidth >= 1000
? Device.PC : window.innerWidth <= 600 ? Device.MOBILE : Device.TABLET;
// 玩家信息
glob.player_info = getPlayerInfo();
// 海外库存对象
glob.fstock = autoFetchJSON('https://yata.yt/api/v1/travel/export/');
// 价格监视实例对象
glob.priceWatcher = glob.isIframe ? null : priceWatcherHandle();
// 抢啤酒
glob.beer = buyBeer();
glob.popup_node = null;
// 当窗口关闭时关闭所有还存在的通知
glob.notifies = {count: 0};
window.addEventListener(
'beforeunload',
() => {
if (glob.notifies.count !== 0) {
for (let i = 0; i < glob.notifies.count; i++) {
(glob.notifies[i] !== null) && (glob.notifies[i].close())
}
}
}
);
}