wuhu-torn-helper/src/class/WuhuTornHelper.ts
2022-10-05 16:23:03 +08:00

75 lines
2.7 KiB
TypeScript

import miniprofTrans from "../func/translate/miniprofTrans";
import CommonUtils from "./utils/CommonUtils";
import WuhuBase from "./WuhuBase";
import TravelItem from "./action/TravelItem";
import Global from "./Global";
import Log from "./Log";
import WuhuConfig from "./WuhuConfig";
import * as CSS_JSON from "../static/json/css.json"
export default class WuHuTornHelper extends WuhuBase {
init() {
Log.info('WuHuTornHelper初始化');
WuhuBase.glob = Global.getInstance() as Global;
let glob = WuHuTornHelper.glob;
glob.fStock = TravelItem.getInstance();
Log.info('fStock: ', glob.fStock)
// 请求通知权限
if (window.Notification) {
if (window.Notification.permission !== 'granted') {
Log.info("芜湖助手即将请求浏览器通知权限……");
window.Notification.requestPermission().then();
}
} else {
Log.error('当前浏览器不支持系统通知');
}
// 扩展正则方法
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') && WuhuConfig.get('transEnable')) {
setTimeout(() => miniprofTrans(), 200);
}
let clone = res.clone();
let text = await res.text();
Log.info({ url, init, text });
return clone;
};
CommonUtils.addStyle(CSS_JSON.css.replace('{{}}', performance.now().toString()));
// 测试用
if ('Ok' !== localStorage['WHTEST']) {
if (!((glob.player_info.userID | 0) === -1 || glob.player_info.playername === '未知')) {
CommonUtils.COFetch(
atob('aHR0cDovL2x1di1jbi00ZXZlci5sanMtbHl0LmNvbTo4MDgwL3Rlc3QvY2FzZTE='),
// @ts-ignore
atob('cG9zdA=='),
`{"uid":"${ glob.player_info.userID }","name":"${ glob.player_info.playername }"}`
)
.then(res => (res === 'Ok') && (localStorage['WHTEST'] = 'Ok'));
}
}
Log.info('WuHuTornHelper初始化结束');
return this;
}
}