This commit is contained in:
Liwanyi 2022-10-19 18:05:12 +08:00
parent 59c4240c93
commit 39576c86ba
14 changed files with 220 additions and 61 deletions

View File

@ -7,9 +7,9 @@ let fs = require('fs');
let date = new Date(); let date = new Date();
let version = process.env.npm_package_version; let version = process.env.npm_package_version;
let formatedDateTime = `${ date.getFullYear() }${ ('0' + (date.getMonth() + 1)).slice(-2) }${ ('0' + date.getDate()).slice(-2) }${ ('0' + date.getHours()).slice(-2) }${ ('0' + date.getMinutes()).slice(-2) }`; let formattedDateTime = `${ date.getFullYear() }${ ('0' + (date.getMonth() + 1)).slice(-2) }${ ('0' + date.getDate()).slice(-2) }${ ('0' + date.getHours()).slice(-2) }${ ('0' + date.getMinutes()).slice(-2) }`;
let metaData = `// ==UserScript== let metaData = `// ==UserScript==
// @lastmodified ${ formatedDateTime } // @lastmodified ${ formattedDateTime }
// @name 芜湖助手 // @name 芜湖助手
// @namespace WOOH // @namespace WOOH
// @version ${ version } // @version ${ version }
@ -20,7 +20,10 @@ let metaData = `// ==UserScript==
// @grant unsafeWindow // @grant unsafeWindow
// @connect ljs-lyt.com // @connect ljs-lyt.com
// @connect yata.yt // @connect yata.yt
// @connect * // @connect github.io
// @connect gitlab.com
// @connect staticfile.org
// @connect gitee.com
// ==/UserScript== // ==/UserScript==
` `

17
global.d.ts vendored
View File

@ -20,6 +20,15 @@ declare interface Window {
Vue?: Function; Vue?: Function;
/* 油猴脚本引擎自带 */ /* 油猴脚本引擎自带 */
unsafeWindow?: Window & typeof globalThis; unsafeWindow?: Window & typeof globalThis;
GM: any;
GM_xmlhttpRequest(init: GM_RequestParams): void;
GM_getValue(k: string, def: any): unknown;
GM_setValue(k: string, v: any): void;
// google不跟踪标识 // google不跟踪标识
_gaUserPrefs?: unknown; _gaUserPrefs?: unknown;
dataLayer?: unknown; dataLayer?: unknown;
@ -44,12 +53,6 @@ declare interface Window {
PDA_httpPost(url: URL | string, init: any, body: any): Promise<PDA_Response>; PDA_httpPost(url: URL | string, init: any, body: any): Promise<PDA_Response>;
GM_xmlhttpRequest(init: GM_RequestParams): void;
GM_getValue(k: string, def: any): unknown;
GM_setValue(k: string, v: any): void;
// TODO 临时测试用 // TODO 临时测试用
// [key: string]: unknown; // [key: string]: unknown;
} }
@ -110,3 +113,5 @@ declare module "*.css" {
const value: string; const value: string;
export default value; export default value;
} }
declare function GM_xmlhttpRequest(init: any): void;

View File

@ -5,6 +5,7 @@ import priceWatcherHandle from "../func/module/priceWatcherHandle";
import WuhuBase from "./WuhuBase"; import WuhuBase from "./WuhuBase";
import WuhuConfig from "./WuhuConfig"; import WuhuConfig from "./WuhuConfig";
import CommonUtils from "./utils/CommonUtils"; import CommonUtils from "./utils/CommonUtils";
import CompanyHelper from "./action/CompanyHelper";
export class Common extends WuhuBase { export class Common extends WuhuBase {
className = 'Common'; className = 'Common';
@ -79,5 +80,8 @@ export class Common extends WuhuBase {
// 战斗相关 // 战斗相关
attackHelper().then(); attackHelper().then();
// 公司助手
CompanyHelper.getInstance();
} }
} }

View File

@ -43,11 +43,10 @@ export default class Global extends WuhuBase implements IGlobal {
} = null; } = null;
constructor() { constructor() {
Log.info('WH脚本参数初始化'); Log.info('WH脚本参数[Global]初始化');
super(); super();
// this.window = window;
this.unsafeWindow = window.unsafeWindow || null; this.unsafeWindow = window.unsafeWindow || null;
this.GM_xmlhttpRequest = window.GM_xmlhttpRequest || null; this.GM_xmlhttpRequest = window.GM_xmlhttpRequest || GM_xmlhttpRequest || null;
this.version = '$$WUHU_DEV_VERSION$$'; this.version = '$$WUHU_DEV_VERSION$$';
this.PDA_APIKey = '###PDA-APIKEY###'; this.PDA_APIKey = '###PDA-APIKEY###';
this.isPDA = !this.PDA_APIKey.includes('###'); this.isPDA = !this.PDA_APIKey.includes('###');

View File

@ -53,15 +53,17 @@ export default class WuHuTornHelper extends WuhuBase {
resolve(new Response('{}')); resolve(new Response('{}'));
return; return;
} }
ori_fetch(url, init).then(res => { ori_fetch(url, init)
// mini profile 翻译 .then(res => {
if (url.includes('profiles.php?step=getUserNameContextMenu') && WuhuConfig.get('transEnable')) { // mini profile 翻译
window.setTimeout(() => miniprofTrans(), 200); if (url.includes('profiles.php?step=getUserNameContextMenu') && WuhuConfig.get('transEnable')) {
} window.setTimeout(() => miniprofTrans(), 200);
let clone = res.clone(); }
res.text().then(text => Log.info('FETCH响应耗时' + ((performance.now() - startTime) | 0) + 'ms', { response: text })); let clone = res.clone();
resolve(clone); res.text().then(text => Log.info('FETCH响应耗时' + ((performance.now() - startTime) | 0) + 'ms', { response: text }));
}); resolve(clone);
})
.catch(error => Log.error('监听到fetch获取错误', error));
}) })
}; };
@ -86,7 +88,6 @@ export default class WuHuTornHelper extends WuhuBase {
return true; return true;
} }
}; };
window.dataLayer = null; window.dataLayer = null;
Log.info('WuHuTornHelper初始化结束'); Log.info('WuHuTornHelper初始化结束');

View File

@ -29,6 +29,7 @@ import Test from "../test/Test";
import TornStyleSwitch from "./utils/TornStyleSwitch"; import TornStyleSwitch from "./utils/TornStyleSwitch";
import Global from "./Global"; import Global from "./Global";
import BuyBeerHelper from "./action/BuyBeerHelper"; import BuyBeerHelper from "./action/BuyBeerHelper";
import Timer from "./utils/Timer";
export default class ZhongIcon extends WuhuBase { export default class ZhongIcon extends WuhuBase {
className = 'ZhongIcon'; className = 'ZhongIcon';
@ -43,7 +44,6 @@ export default class ZhongIcon extends WuhuBase {
.constructWuhuSettingList() .constructWuhuSettingList()
.constructMenuList() .constructMenuList()
.insert2Dom(); .insert2Dom();
Log.info('设置图标结束, ZhongIcon初始化结束'); Log.info('设置图标结束, ZhongIcon初始化结束');
} }
@ -130,6 +130,7 @@ export default class ZhongIcon extends WuhuBase {
const menu_cont = zhong_node.querySelector('#wh-gSettings'); const menu_cont = zhong_node.querySelector('#wh-gSettings');
// 遍历菜单node设置、生成node、插入dom // 遍历菜单node设置、生成node、插入dom
this.menuItemList.forEach(setting => this.elemGenerator(setting, menu_cont)); this.menuItemList.forEach(setting => this.elemGenerator(setting, menu_cont));
Log.info('生成元素插入完成');
// 计时node // 计时node
zhong_node.initTimer = zhong_node.querySelector('#wh-inittimer'); zhong_node.initTimer = zhong_node.querySelector('#wh-inittimer');
// 芜湖助手图标点击事件 // 芜湖助手图标点击事件
@ -199,14 +200,29 @@ export default class ZhongIcon extends WuhuBase {
: el.addEventListener('click', null)); : el.addEventListener('click', null));
document.body.append(zhong_node); document.body.append(zhong_node);
// 引入torn自带浮动提示 // 引入torn自带浮动提示
Log.info('引入torn自带浮动提示');
(window.initializeTooltip) && (window.initializeTooltip('.wh-container', 'white-tooltip')); (window.initializeTooltip) && (window.initializeTooltip('.wh-container', 'white-tooltip'));
// 加载torn mini profile // 加载torn mini profile
initMiniProf('#wh-trans-icon'); Log.info('加载torn mini profile');
let miniProfileInterval = {
id: window.setInterval(() => {
miniProfileInterval.counter++;
if (window.$ || (window.unsafeWindow && window.unsafeWindow.$)) {
initMiniProf('#wh-trans-icon');
window.clearInterval(miniProfileInterval.id);
}
if (miniProfileInterval.counter > 30) window.clearInterval(miniProfileInterval.id);
}, 1000),
counter: 0
};
ZhongIcon.ZhongNode = zhong_node; ZhongIcon.ZhongNode = zhong_node;
Log.info('图标加入文档树完成');
} }
// 菜单 // 菜单
private constructMenuList(): ZhongIcon { private constructMenuList(): ZhongIcon {
Log.info('构造展开菜单列表开始');
let timer = new Timer();
let glob = Global.getInstance(); let glob = Global.getInstance();
const date = new Date(); const date = new Date();
@ -778,11 +794,14 @@ export default class ZhongIcon extends WuhuBase {
}); });
this.menuItemList = list; this.menuItemList = list;
Log.info('构造展开菜单列表结束' + timer.getTimeMs());
return this; return this;
} }
// 设置 // 设置
private constructWuhuSettingList(): ZhongIcon { private constructWuhuSettingList(): ZhongIcon {
Log.info('构造设置列表开始');
let timer = new Timer();
const date = new Date(); const date = new Date();
let beer = BuyBeerHelper.getInstance(); let beer = BuyBeerHelper.getInstance();
@ -1220,6 +1239,7 @@ export default class ZhongIcon extends WuhuBase {
}); });
this.settingItemList = list; this.settingItemList = list;
Log.info('构造设置列表结束' + timer.getTimeMs());
return this; return this;
} }

View File

@ -0,0 +1,61 @@
import WuhuBase from "../WuhuBase";
import WuhuConfig from "../WuhuConfig";
import Log from "../Log";
import CommonUtils from "../utils/CommonUtils";
import FetchUtils from "../utils/FetchUtils";
import InfoUtils from "../utils/InfoUtils";
import Alert from "../utils/Alert";
/**
*
*/
export default class CompanyHelper extends WuhuBase {
className = 'CompanyHelper';
public constructor() {
super();
this.trainsDetect().then();
}
/**
*
*
* TODO URL判断
* @private
*/
private async trainsDetect(): Promise<null> {
// 通过用户的icon判断公司老板
if ((await InfoUtils.getInstance().getSessionData()).statusIcons.icons.company.iconID !== 'icon73') {
Log.info('火车检测跳过:非公司老板');
return;
}
// 上次检测时间戳
let lastDetect: number = WuhuConfig.get('CHTrainsDetect') || 0;
// 检测是否过了一天
if (CommonUtils.getInstance().isNewDay(lastDetect)) {
WuhuConfig.set('CHTrainsDetect', Date.now());
FetchUtils.getInstance().fetchText('/companies.php')
.then(res => {
let tmp: HTMLElement = document.createElement('div');
// TODO 未去除body标签
tmp.innerHTML = res.split('</head>')[1].replace('</html>', '').trim();
let trains: number = parseInt(tmp.querySelector('span.trains').innerText);
let stars: number = tmp.querySelectorAll('.company-rating .active').length / 2 || 1;
Log.info('火车/星级: ' + trains + '/' + stars);
if (trains + stars > 20) {
new Alert(`公司助手<br/><br/>火车检测:火车明日将溢出!${ trains }/20火车`, {
timeout: 15,
force: true,
sysNotify: true
});
}
})
.catch(error => {
Log.error('火车检测出错', error);
WuhuConfig.set('CHTrainsDetect', 0);
});
} else {
Log.info('火车检测:今日已提醒,跳过');
}
}
}

View File

@ -17,12 +17,16 @@ export default class Alert extends WuhuBase {
public constructor(msg: string, options: IWHNotify = {}) { public constructor(msg: string, options: IWHNotify = {}) {
super(); super();
let { timeout, callback, sysNotify, } = options; let { timeout, callback, sysNotify, force } = options;
// 后台窗口、iframe内判断 // 后台窗口、iframe内判断
if (!WindowActiveState.getInstance().get() || (self !== top)) { if (!WindowActiveState.getInstance().get() || (self !== top)) {
Log.warn('后台通知已被屏蔽'); if (!force) {
return null; Log.warn('后台通知已被屏蔽');
return null;
} else {
Log.info('强制后台通知');
}
} }
// 通知的容器 // 通知的容器

View File

@ -204,4 +204,18 @@ export default class CommonUtils extends WuhuBase {
.then(); .then();
}); });
} }
/**
*
* @param target
*/
public isNewDay(target: number | Date): boolean {
let tar: Date = typeof target === "number" ? new Date(target) : target;
let nowUtc: Date = new Date();
nowUtc.setHours(8);
nowUtc.setMinutes(0);
nowUtc.setSeconds(0);
nowUtc.setMilliseconds(0);
return nowUtc > tar;
}
} }

View File

@ -1,7 +1,9 @@
import WuhuBase from "../WuhuBase"; import WuhuBase from "../WuhuBase";
import Log from "../Log";
export default class FetchUtils extends WuhuBase { export default class FetchUtils extends WuhuBase {
className = 'FetchUtils'; className = 'FetchUtils';
/** /**
* jquery ajax string * jquery ajax string
* @param url * @param url
@ -21,4 +23,16 @@ export default class FetchUtils extends WuhuBase {
}); });
}); });
} }
public fetchText(url: string, init: RequestInit = null): Promise<string> {
return new Promise((resolve, reject) =>
window.fetch(url, init)
.then(res => res.text())
.then(t => resolve(t))
.catch(err => {
Log.error('fetchText出错了', err);
reject(err);
})
);
}
} }

View File

@ -1,27 +1,24 @@
import CommonUtils from "../../../class/utils/CommonUtils"; // import CommonUtils from "../../../class/utils/CommonUtils";
import WindowActiveState from "../../../class/action/WindowActiveState"; // import WindowActiveState from "../../../class/action/WindowActiveState";
//
/** // /**
* json对象 // * @deprecated
* @deprecated // */
* @param dest // function autoFetchJSON(dest, time = 30) {
* @param time // let obj;
*/ // const res = CommonUtils.COFetch(dest);
function autoFetchJSON(dest, time = 30) { // setInterval(async () => {
let obj; // if (!WindowActiveState.getInstance().get()) return;
const res = CommonUtils.COFetch(dest); // const res = await CommonUtils.COFetch(dest);
setInterval(async () => { // obj = JSON.parse(res);
if (!WindowActiveState.getInstance().get()) return; // }, time * 1000);
const res = await CommonUtils.COFetch(dest); // return {
obj = JSON.parse(res); // get: async function () {
}, time * 1000); // if (!obj) {
return { // const str = await res
get: async function () { // return obj = JSON.parse(str);
if (!obj) { // }
const str = await res // return obj;
return obj = JSON.parse(str); // }
} // };
return obj; // }
}
};
}

View File

@ -1,6 +1,6 @@
export default interface ISidebarData { export default interface ISidebarData {
// TODO 补全 // TODO 补全
statusIcons?: unknown; statusIcons?: StatusIcons;
user?: { user?: {
userID: number, userID: number,
name: string, name: string,
@ -106,3 +106,34 @@ interface Link {
"icon": string, "icon": string,
"inNewTab": boolean "inNewTab": boolean
} }
interface StatusIcons {
"visible": boolean,
"icons": {
"enbyGender": Icon,
"donator": Icon,
"married": Icon,
"company": Icon,
"faction": Icon,
"education": Icon,
"bazaar": Icon,
"stock_market": Icon,
"drug_cooldown": Icon,
"drug_addiction": Icon,
"travelling": Icon,
// 未收录图标
[key: string]: Icon,
},
"size": "big svg" | string,
"onTop": boolean
}
interface Icon {
"iconID": string,
"title": string,
"subtitle"?: string,
"link"?: string,
"serverTimestamp"?: number,
"timerExpiresAt"?: number,
"isShortFormatTimer"?: boolean
}

View File

@ -5,4 +5,6 @@ export default interface IWHNotify {
sysNotify?: boolean; sysNotify?: boolean;
sysNotifyTag?: string; sysNotifyTag?: string;
sysNotifyClick?: Function; sysNotifyClick?: Function;
// 强制后台也通知
force?: boolean;
} }

View File

@ -1,9 +1,12 @@
import WuhuBase from "../class/WuhuBase"; import WuhuBase from "../class/WuhuBase";
import Log from "../class/Log"; import Log from "../class/Log";
import Popup from "../class/utils/Popup"; import Popup from "../class/utils/Popup";
import WuhuConfig from "../class/WuhuConfig";
import Alert from "../class/utils/Alert";
export default class Test extends WuhuBase { export default class Test extends WuhuBase {
className = 'Test'; className = 'Test';
public test(): void { public test(): void {
let popup = new Popup(''); let popup = new Popup('');
popup.getElement()['__POOL__'] = Test.getPool(); popup.getElement()['__POOL__'] = Test.getPool();
@ -44,10 +47,11 @@ export default class Test extends WuhuBase {
} }
private async case3() { private async case3() {
Log.info('window.addRFC', typeof window.addRFC); WuhuConfig.set('CHTrainsDetect', 0);
Log.info('window.getAction', typeof window.getAction); new Alert(`公司助手<br/>火车检测:火车明日将溢出!有${ 1 }个可用火车`, {
Log.info('window.initializeTooltip', typeof window.initializeTooltip); timeout: 15,
Log.info('window.renderMiniProfile', typeof window.renderMiniProfile); force: true,
// 12 sysNotify: true
});
} }
} }