This commit is contained in:
Liwanyi 2022-10-17 15:19:18 +08:00
parent f37641a955
commit bb770d8e2e
32 changed files with 116 additions and 87 deletions

7
global.d.ts vendored
View File

@ -17,10 +17,13 @@ declare interface Window {
ReactDOM?: any;
hasWHQuickFlyOpt?: boolean;
// 插件运行标识
WHTRANS?: boolean;
WHTRANS?: number;
Vue?: Function;
/* 油猴脚本引擎自带 */
unsafeWindow?: Window & typeof globalThis;
// google不跟踪标识
_gaUserPrefs?: unknown;
dataLayer?: unknown;
eval(exc: string): void;
@ -47,7 +50,7 @@ declare interface Window {
GM_setValue(k: string, v: any): void;
// TODO 临时测试用
[key: string]: unknown;
// [key: string]: unknown;
}
declare interface GM_RequestParams {

View File

@ -10,22 +10,21 @@ import Global from "./Global";
export default class Application {
public static main() {
let started = performance.now();
WuhuBase.PDAExecute();
WuhuBase.conditionInterrupt();
// 初始化
WuHuTornHelper.getInstance();
// 插件图标和设置菜单
ZhongIcon.getInstance();
// 所有页面通用
Common.getInstance().resolve();
// URL匹配
UrlPattern.getInstance().resolve();
// 翻译
if (WuhuConfig.get('transEnable')) translateMain(Global.getInstance().href);
let runTime: number = (performance.now() - started) | 0;
ZhongIcon.ZhongNode.initTimer.innerHTML = `助手加载时间 ${ runTime }ms`;
}
}

View File

@ -7,6 +7,7 @@ import WuhuConfig from "./WuhuConfig";
import CommonUtils from "./utils/CommonUtils";
export class Common extends WuhuBase {
className = 'Common';
resolve() {
let glob = Common.glob;
// 价格监控

View File

@ -9,6 +9,7 @@ import BuyBeerHelper from "./action/BuyBeerHelper";
*
*/
export default class Global extends WuhuBase implements IGlobal {
className = 'Global';
GM_xmlhttpRequest: Function = null;
href: string = window.location.href;
@ -39,13 +40,12 @@ export default class Global extends WuhuBase implements IGlobal {
'data-celebration'?: string;
'data-traveling'?: string;
'data-abroad'?: string;
// [key: string]: string;
} = null;
constructor() {
Log.info('WH脚本参数初始化');
super();
this.window = window;
// this.window = window;
this.unsafeWindow = window.unsafeWindow || null;
this.GM_xmlhttpRequest = window.GM_xmlhttpRequest || null;
this.version = '$$WUHU_DEV_VERSION$$';
@ -60,13 +60,11 @@ export default class Global extends WuhuBase implements IGlobal {
this.bodyAttrs = {};
if (this.unsafeWindow) {
try {
window = this.unsafeWindow || this.window;
Log.info('替换window上下文');
} catch {
this.unsafeWindow = null;
this.GM_xmlhttpRequest = null;
}
Log.info('存在unsafeWindow, 引入部分torn公共方法');
window.addRFC = this.unsafeWindow.addRFC;
window.getAction = this.unsafeWindow.getAction;
window.initializeTooltip = this.unsafeWindow.initializeTooltip;
window.renderMiniProfile = this.unsafeWindow.renderMiniProfile;
}
for (let i = 0; i < document.body.attributes.length; i++) {

View File

@ -13,7 +13,7 @@ export default class Log {
static error(...o): void {
let time = this.getTime();
let flag = '[WH] ERR';
(this.debug()) && (console.error(flag, time, ...o));
console.error(flag, time, ...o);
this.saveLogs(flag, time, ...o);
}

View File

@ -1,13 +1,11 @@
import cityFinder from "../func/module/cityFinder";
import { missionDict } from "../dictionary/translation";
import getTaskHint from "../func/translate/getTaskHint";
import Device from "../enum/Device";
import WuhuBase from "./WuhuBase";
import CommonUtils from "./utils/CommonUtils";
import Log from "./Log";
import WuhuConfig from "./WuhuConfig";
import Alert from "./utils/Alert";
import InfoUtils from "./utils/InfoUtils";
import SHOP_BEER_STATIC_ITEM_HTML from "../static/html/buyBeer/shop_beer_static_item.html";
import ADD_BEER_HEAD_HTML from "../static/html/buyBeer/add_beer_head.html";
import QUICK_CRIMES_HTML from "../static/html/quick_crimes.html";
@ -21,6 +19,7 @@ import BuyBeerHelper from "./action/BuyBeerHelper";
import XZMZ from "./action/XZMZ";
export default class UrlPattern extends WuhuBase {
className = 'UrlPattern';
public resolve(): void {
let href = window.location.href;
// 捡垃圾助手
@ -226,21 +225,21 @@ export default class UrlPattern extends WuhuBase {
}
// 特定
if (InfoUtils.getInstance().getPlayerInfo().userID === 2687093 && CommonUtils.getDeviceType() === Device.PC) {
InfoUtils.getInstance().getSidebarData().then(data => {
let item = document.getElementById('nav-items');
if (item) {
let copy = item.cloneNode(true);
(<HTMLElement>copy.firstChild).style.backgroundColor = '#678c00';
let a = copy.firstChild.firstChild as HTMLAnchorElement;
a.href = '/item.php?temp=1';
let span = a.lastChild as HTMLElement;
span.innerHTML = '物品';
span.style.color = 'white';
item.after(copy);
}
});
}
// if (InfoUtils.getInstance().getPlayerInfo().userID === 2687093 && CommonUtils.getDeviceType() === Device.PC) {
// InfoUtils.getInstance().getSidebarData().then(data => {
// let item = document.getElementById('nav-items');
// if (item) {
// let copy = item.cloneNode(true);
// (<HTMLElement>copy.firstChild).style.backgroundColor = '#678c00';
// let a = copy.firstChild.firstChild as HTMLAnchorElement;
// a.href = '/item.php?temp=1';
// let span = a.lastChild as HTMLElement;
// span.innerHTML = '物品';
// span.style.color = 'white';
// item.after(copy);
// }
// });
// }
// 彩票助手
if (href.includes('loader.php?sid=lottery')) LotteryHelper.getInstance().init();

View File

@ -1,17 +1,10 @@
import IGlobal from "../interface/IGlobal";
import IWHSettings from "../interface/IWHSettings";
import Log from "./Log";
import Provider from "./provider/Provider";
export default class WuhuBase extends Provider {
static glob: IGlobal = null;
protected static className = 'WuhuBase';
constructor() {
super();
// Log.info({ 'constructor': this.constructor, this: this, test: this.getClassName() });
Log.info('创建对象:', this);
}
className = 'WuhuBase';
static getLocal(): IWHSettings {
return JSON.parse(localStorage.getItem('wh_trans_settings')) || {};
@ -28,8 +21,8 @@ export default class WuhuBase extends Provider {
if (condition) throw '芜湖';
}
static PDAExecute() {
if (window.WHTRANS) throw '已运行,退出';
window.WHTRANS = true;
public getClassName() {
return this.className;
}
}

View File

@ -3,6 +3,7 @@ import Alert from "./utils/Alert";
import Log from "./Log";
export default class WuhuConfig extends WuhuBase {
className = 'WuhuConfig';
static get(key: string | string[]) {
let localPool = this.getLocal();
if (typeof key === 'string') return localPool[key];

View File

@ -8,6 +8,7 @@ import WuhuConfig from "./WuhuConfig";
import COMMON_CSS from "../static/css/common.css";
export default class WuHuTornHelper extends WuhuBase {
className = 'WuHuTornHelper';
constructor() {
super();
@ -23,7 +24,7 @@ export default class WuHuTornHelper extends WuhuBase {
window.Notification.requestPermission().then();
}
} else {
Log.error('当前浏览器不支持系统通知');
Log.error('浏览器不支持系统通知');
}
// 扩展正则方法
@ -38,9 +39,9 @@ export default class WuHuTornHelper extends WuhuBase {
// 监听fetch
const ori_fetch = window.fetch;
window.fetch = async (url: string, init: RequestInit) => {
window.fetch = (url: string, init: RequestInit) => {
let startTime = performance.now();
Log.info('FETCH调用[' + url + ']');
Log.info('FETCH调用[' + url + '], init:', init);
return new Promise(resolve => {
if (url.contains('newsTickers')) {
Log.info('阻止获取新闻横幅');
@ -55,16 +56,12 @@ export default class WuHuTornHelper extends WuhuBase {
ori_fetch(url, init).then(res => {
// mini profile 翻译
if (url.includes('profiles.php?step=getUserNameContextMenu') && WuhuConfig.get('transEnable')) {
setTimeout(() => miniprofTrans(), 200);
window.setTimeout(() => miniprofTrans(), 200);
}
let clone = res.clone();
res.text().then(text => Log.info('FETCH响应耗时' + ((performance.now() - startTime) | 0) + 'ms', {
url,
init,
text
}));
res.text().then(text => Log.info('FETCH响应耗时' + ((performance.now() - startTime) | 0) + 'ms', { response: text }));
resolve(clone);
})
});
})
};

View File

@ -31,6 +31,7 @@ import Global from "./Global";
import BuyBeerHelper from "./action/BuyBeerHelper";
export default class ZhongIcon extends WuhuBase {
className = 'ZhongIcon';
public static ZhongNode: MyHTMLElement = null;
private menuItemList: MenuItemConfig[] = null;
private settingItemList: MenuItemConfig[] = null;
@ -46,7 +47,7 @@ export default class ZhongIcon extends WuhuBase {
Log.info('设置图标结束, ZhongIcon初始化结束');
}
private elemGenerator(setting: MenuItemConfig, root_node: Node) {
private elemGenerator(setting: MenuItemConfig, root_node: Element): HTMLElement {
let { tip, domType } = setting;
let new_node = null;
switch (domType) {
@ -120,7 +121,7 @@ export default class ZhongIcon extends WuhuBase {
let zhong_node: MyHTMLElement = document.querySelector('div#wh-trans-icon');
let settings = this.menuItemList;
let { version } = WuhuBase.glob;
if ((self !== top) || !!zhong_node) return zhong_node;
if ((self !== top) || !!zhong_node) return null;
zhong_node = document.createElement('div');
zhong_node.id = 'wh-trans-icon';
zhong_node.classList.add('cont-gray');
@ -135,7 +136,6 @@ export default class ZhongIcon extends WuhuBase {
(<MyHTMLElement>zhong_node.querySelector('#wh-trans-icon-btn')).onclick = () => {
zhong_node.classList.toggle('wh-icon-expanded');
const click_func = e => {
// e.stopImmediatePropagation();
Log.info(e.target);
if (e.target === zhong_node.querySelector('#wh-trans-icon-btn')) return;
if (!zhong_node.contains(e.target)) {
@ -203,7 +203,6 @@ export default class ZhongIcon extends WuhuBase {
// 加载torn mini profile
initMiniProf('#wh-trans-icon');
ZhongIcon.ZhongNode = zhong_node;
// return zhong_node;
}
// 菜单
@ -1198,15 +1197,15 @@ export default class ZhongIcon extends WuhuBase {
domType: 'button', domId: 'wh-otherBtn', domText: '更多设定',
clickFunc: () => {
let pop = new Popup('', '更多设定');
let insertHtml = '<p><button class="torn-btn">清空设置数据</button></p><p><button class="torn-btn">请求通知权限</button></p><p><button class="torn-btn">测试跨域请求</button></p>';
let insertHtml = '<p><button class="torn-btn">清空设置</button></p><p><button class="torn-btn">通知权限</button></p><p><button class="torn-btn">外部数据权限</button></p>';
pop.getElement().insertAdjacentHTML('beforeend', insertHtml);
let [btn1, btn2, btn3] = Array.from(pop.getElement().querySelectorAll('button'));
btn1.addEventListener('click', () => {
localStorage.setItem('wh_trv_alarm', '');
localStorage.setItem('wh_trans_settings', '');
localStorage.setItem('whuuid', '');
localStorage.setItem('wh-gs-storage', '');
localStorage.setItem('WHTEST', '');
localStorage.removeItem('wh_trv_alarm');
localStorage.removeItem('wh_trans_settings');
localStorage.removeItem('whuuid');
localStorage.removeItem('wh-gs-storage');
localStorage.removeItem('WHTEST');
new Alert('已清空,刷新页面');
window.location.reload();
});

View File

@ -8,6 +8,7 @@ import NOTIFY_HTML from "../../static/html/buyBeer/notify.html";
import CommonUtils from "../utils/CommonUtils";
export default class BuyBeerHelper extends WuhuBase implements BeerMonitorLoop {
className = 'BuyBeerHelper';
private isNotifying = false;
private loopId: number = null;

View File

@ -6,6 +6,7 @@ import CommonUtils from "../utils/CommonUtils";
import TornStyleBlock from "../utils/TornStyleBlock";
export default class LotteryHelper extends WuhuBase {
className = 'LotteryHelper';
private loopFlag = true;
private radioDaily: HTMLInputElement = null;

View File

@ -5,6 +5,7 @@ import Alert from "../utils/Alert";
import TornStyleSwitch from "../utils/TornStyleSwitch";
export default class PTHelper extends WuhuBase {
className = 'PTHelper';
private readonly observer;
private readonly usersPointSell;

View File

@ -4,6 +4,7 @@ import WuhuConfig from "../WuhuConfig";
import TornStyleSwitch from "../utils/TornStyleSwitch";
export default class StackHelper extends WuhuBase {
className = 'StackHelper';
constructor() {
super();
let block = new TornStyleBlock('叠E保护').insert2Dom();

View File

@ -8,6 +8,7 @@ import * as FILTER from "../../static/json/for_stock_item_filter.json";
import WindowActiveState from "./WindowActiveState";
export default class TravelItem extends WuhuBase {
className = 'TravelItem';
private readonly apiUrl: string = 'https://yata.yt/api/v1/travel/export/';
private foreignStockInfo: any = null;

View File

@ -2,6 +2,7 @@ import uuidv4 from "../../func/utils/uuidv4";
import WuhuBase from "../WuhuBase";
export default class WindowActiveState extends WuhuBase {
className = 'WindowActiveState';
isFocus = false;
uuid = uuidv4();

View File

@ -6,7 +6,12 @@ import CommonUtils from "../utils/CommonUtils";
import XUNZHAOMUZHUANG_CSS from "../../static/css/xunzhaomuzhuang.css";
import TornStyleBlock from "../utils/TornStyleBlock";
/**
*
* /item.php?temp=4#xunzhaomuzhuang
*/
export default class XZMZ extends WuhuBase {
className = 'XZMZ';
private readonly mainRoleContainer: HTMLElement;
private readonly IDList: number[];
private readonly btn: HTMLButtonElement;
@ -44,8 +49,9 @@ export default class XZMZ extends WuhuBase {
CommonUtils.ajaxFetch({
url: window.addRFC('https://www.torn.com/profiles.php?step=getProfileData&XID=' + id),
method: 'GET'
})
.then((res) => res.json())
}).then((res) =>
res.json()
)
.then((res) => {
this.counter--;
if (res.userStatus.status.type === 'ok') {
@ -79,7 +85,7 @@ export default class XZMZ extends WuhuBase {
$node += '<td style="border: 1px solid darkgray;padding:5px;text-align:center;">' + data.player_id + '</td>'
$node += `<td style="border: 1px solid darkgray;padding:5px;text-align:center;"><a href="/profiles.php?XID=${ data.player_id }">${ data.name }</a></td>`;
$node += '<td style="border: 1px solid darkgray;padding:5px;text-align:center;">' + data.level + '</td>'
$node += '<td style="border: 1px solid darkgray;padding:5px;text-align:center;"><a href="https://www.torn.com/loader.php?sid=attack&user2ID=' + data.player_id + '" target="_blank">攻击</a></td>'
$node += '<td style="border: 1px solid darkgray;padding:5px;text-align:center;"><a href="https://www.torn.com/loader.php?sid=attack&user2ID=' + data.player_id + '" target="_blank" class="torn-btn">攻击</a></td>'
$parentNode.insertAdjacentHTML('beforeend', $node)
}
}

View File

@ -1,7 +1,10 @@
import Log from "../Log";
/**
*
*/
export default class Provider {
protected readonly className: string = 'Provider';
private static instance;
private static readonly pool = {};
@ -13,7 +16,9 @@ export default class Provider {
// return this.instance ||= new this();
if (!this.instance) {
this.instance = new this();
Provider.pool[this.name] = this.instance;
let thatName = this.instance.getClassName() || this.name;
Log.info('新建实例,', thatName, this.instance);
Provider.pool[thatName] = this.instance;
}
return this.instance;
}

View File

@ -1,12 +1,22 @@
import Log from "../Log";
import ZhongIcon from "../ZhongIcon";
export default class Starter {
public static run(T): void {
if (window.WHTRANS) throw '退出, 已运行次数' + window.WHTRANS;
window.WHTRANS = window.WHTRANS === undefined ? 1 : window.WHTRANS++;
let started = performance.now();
try {
T.main();
} catch (error) {
Log.error(error);
Log.error('[Starter] trace: ', JSON.stringify({ error }));
}
} catch (e) {
Log.error('加载出错');
Log.error('[Starter]', e.message || JSON.stringify(e));
}
let runTime: number = (performance.now() - started) | 0;
Log.info(`加载时间${ runTime }ms`);
if (ZhongIcon.ZhongNode && ZhongIcon.ZhongNode.initTimer)
ZhongIcon.ZhongNode.initTimer.innerHTML = `加载时间 ${ runTime }ms`;
}
}

View File

@ -3,6 +3,7 @@ import Log from "../Log";
import ZhongIcon from "../ZhongIcon";
export default class ActionButtonUtils extends WuhuBase {
className = 'ActionButtonUtils';
private hasAdded: boolean = false;
public add(txt: string, func: (ev: Event) => void = () => null): void {

View File

@ -7,6 +7,7 @@ import NOTIFY_HTML from "../../static/html/notify.html";
import WindowActiveState from "../action/WindowActiveState";
export default class Alert extends WuhuBase {
className = 'Alert';
private static container: HTMLElement = null;
private notify: MyHTMLElement = null;

View File

@ -7,9 +7,11 @@ import Alert from "./Alert";
import LOADING_IMG_HTML from "../../static/html/loading_img.html";
export default class CommonUtils extends WuhuBase {
className = 'CommonUtils';
static getScriptEngine() {
let glob = CommonUtils.glob;
return glob.unsafeWindow ? UserScriptEngine.GM : glob.isPDA
return glob.GM_xmlhttpRequest ? UserScriptEngine.GM : glob.isPDA
? UserScriptEngine.PDA : UserScriptEngine.RAW;
}

View File

@ -1,6 +1,7 @@
import WuhuBase from "../WuhuBase";
export default class FetchUtils extends WuhuBase {
className = 'FetchUtils';
/**
* jquery ajax string
* @param url

View File

@ -5,6 +5,7 @@ import Log from "../Log";
import CommonUtils from "./CommonUtils";
export default class InfoUtils extends WuhuBase {
className = 'InfoUtils';
/**
* { playername: string, userID: number }
* @return {PlayerInfo} rs

View File

@ -1,6 +1,7 @@
import WuhuBase from "../WuhuBase";
export default class MDUtils extends WuhuBase {
className = 'MDUtils';
/**
* Markdown
* @param {String} from

View File

@ -1,6 +1,7 @@
import WuhuBase from "../WuhuBase";
export default class MathUtils extends WuhuBase {
className = 'MathUtils';
// 得到一个两数之间的随机整数
public getRandomInt(min: number, max: number): number {
min = Math.ceil(min);

View File

@ -3,6 +3,7 @@ import Log from "../Log";
import WuhuBase from "../WuhuBase";
export default class NotificationUtils extends WuhuBase {
className = 'NotificationUtils';
private permission: boolean = window.Notification && window.Notification.permission === 'granted';
public push(msg: string, options: IWHNotify = {}): void {

View File

@ -3,7 +3,7 @@ import POPUP_HTML from "../../static/html/popup.html";
import Log from "../Log";
export default class Popup extends WuhuBase {
protected className = 'Popup';
className = 'Popup';
private readonly container: HTMLElement = null;
private readonly node: HTMLElement = null;

View File

@ -204,8 +204,9 @@ export default async function travelHelper(): Promise<null> {
}
// 解毒提醒
if (bodyAttrs['data-country'] === 'switzerland') {
let block = new TornStyleBlock('解毒提醒').insert2Dom();
let block = new TornStyleBlock('解毒提醒');
block.setContent('<p><a href="/index.php?page=rehab">❤️ 点击前往解毒</a></p>');
document.querySelector('h4#skip-to-content').before(block.getBase());
// let page_title = document.querySelector('h4#skip-to-content');
// let msg = document.createElement('div');
// msg.innerHTML = `<div class="info-msg border-round">

View File

@ -2,6 +2,7 @@ interface MyHTMLElement extends HTMLElement {
sys_notify?: Notification;
msgInnerText?: string;
close?: () => void;
initTimer?: HTMLElement;
// 对象的其他参数
[key: string]: any;

View File

@ -19,9 +19,9 @@
<th style="border: 1px solid darkgray;padding: 5px;background-color: black;color: white;font-weight: bold;text-align:center;">
攻击
</th>
<th style="border: 1px solid darkgray;padding: 5px;background-color: black;color: white;font-weight: bold;text-align:center;">
BS(M)
</th>
<!-- <th style="border: 1px solid darkgray;padding: 5px;background-color: black;color: white;font-weight: bold;text-align:center;">-->
<!-- BS(M)-->
<!-- </th>-->
</thead>
<tbody id="table-body"></tbody>
</table>

View File

@ -1,17 +1,17 @@
import WuhuBase from "../class/WuhuBase";
import Log from "../class/Log";
import Popup from "../class/utils/Popup";
import TornStyleSwitch from "../class/utils/TornStyleSwitch";
import CommonUtils from "../class/utils/CommonUtils";
export default class Test extends WuhuBase {
className = 'Test';
public test(): void {
let popup = new Popup('test');
let popup = new Popup('');
popup.getElement()['__POOL__'] = Test.getPool();
popup.getElement().append(new TornStyleSwitch('123').getBase());
// this.case1()
// this.case2()
this.case3().then();
// window.fetch('/test').then(res=>res.text()).then(o=>Log.info(o))
}
private case1() {
@ -44,9 +44,10 @@ export default class Test extends WuhuBase {
}
private async case3() {
Log.info(await CommonUtils.ajaxFetch({
url: window.addRFC('https://www.torn.com/profiles.php?step=getProfileData&XID=17003'),
method: 'GET'
}));
Log.info('window.addRFC', typeof window.addRFC);
Log.info('window.getAction', typeof window.getAction);
Log.info('window.initializeTooltip', typeof window.initializeTooltip);
Log.info('window.renderMiniProfile', typeof window.renderMiniProfile);
// 12
}
}