优化起飞功能错误处理,取消网络拦截,避免官方功能被影响,更新版本至1.2.4

This commit is contained in:
wy 2025-04-07 16:03:01 +08:00
parent 4ae5ba1e46
commit 17b58c7924
5 changed files with 129 additions and 113 deletions

View File

@ -1,5 +1,14 @@
# CHANGE # CHANGE
## 1.2.4
2025年04月07日
### 修改
- 优化起飞功能错误处理
- 取消网络拦截,避免官方功能被影响
## 1.2.3 ## 1.2.3
2025年03月11日 2025年03月11日

View File

@ -1,6 +1,6 @@
{ {
"name": "wuhu-torn-helper", "name": "wuhu-torn-helper",
"version": "1.2.3", "version": "1.2.4",
"description": "芜湖助手", "description": "芜湖助手",
"scripts": { "scripts": {
"release": "cross-env NODE_ENV=production rollup -c && node build.mjs", "release": "cross-env NODE_ENV=production rollup -c && node build.mjs",

File diff suppressed because one or more lines are too long

View File

@ -66,100 +66,100 @@ export default class Initializer {
* @param {'fetch'|'xhr'}from * @param {'fetch'|'xhr'}from
* @return {string|unknown} * @return {string|unknown}
*/ */
const intercept = (data: string, url: string, method: 'GET' | 'POST' | string, requestBody: string | unknown, from: 'fetch' | 'xhr') => { // const intercept = (data: string, url: string, method: 'GET' | 'POST' | string, requestBody: string | unknown, from: 'fetch' | 'xhr') => {
let origin = data; // let origin = data;
let ret = { json: null, text: null, isModified: false }; // let ret = { json: null, text: null, isModified: false };
try { // try {
ret.json = JSON.parse(<string>data); // ret.json = JSON.parse(<string>data);
} catch { // } catch {
this.logger.warn('JSON.parse 错误', { data }); // this.logger.warn('JSON.parse 错误', { data });
ret.text = data; // ret.text = data;
} // }
this.logger.info('[' + from + ']响应', { url, method, ret, requestBody }); // this.logger.info('[' + from + ']响应', { url, method, ret, requestBody });
globVars.WH_NET_LOG.push({ url, method, ret, requestBody, from }); // globVars.WH_NET_LOG.push({ url, method, ret, requestBody, from });
globVars.responseHandlers.forEach(handler => { // globVars.responseHandlers.forEach(handler => {
try { // try {
handler(url, ret, { method, requestBody }); // handler(url, ret, { method, requestBody });
} catch (e) { // } catch (e) {
this.logger.error(e.stack || e.message); // this.logger.error(e.stack || e.message);
} // }
}); // });
if (ret.isModified) { // if (ret.isModified) {
return ret.json ? JSON.stringify(ret.json) : ret.text; // return ret.json ? JSON.stringify(ret.json) : ret.text;
} else { // } else {
return origin; // return origin;
} // }
}; // };
// 监听fetch // 监听fetch
((fetch0, window) => { // ((fetch0, window) => {
let originFetch = fetch0; // let originFetch = fetch0;
// 引用解决与其他脚本接管fetch方法引起的兼容性问题 // // 引用解决与其他脚本接管fetch方法引起的兼容性问题
if (glob.unsafeWindow) { // if (glob.unsafeWindow) {
originFetch = glob.unsafeWindow.fetch; // originFetch = glob.unsafeWindow.fetch;
} // }
let fetchHandle: (string, RequestInit) => Promise<Response> = (url: string, init: RequestInit) => { // let fetchHandle: (string, RequestInit) => Promise<Response> = (url: string, init: RequestInit) => {
if (!init) init = { method: 'GET' }; // if (!init) init = { method: 'GET' };
return new Promise(resolve => { // return new Promise(resolve => {
if (url.includes('newsTickers')) { // if (url.includes('newsTickers')) {
this.logger.info('阻止获取新闻横幅'); // this.logger.info('阻止获取新闻横幅');
resolve(new Response('{}', init)); // resolve(new Response('{}', init));
return; // return;
} // }
if (url.includes('google')) { // if (url.includes('google')) {
this.logger.info('阻止google相关请求'); // this.logger.info('阻止google相关请求');
resolve(new Response('{}', init)); // resolve(new Response('{}', init));
return; // return;
} // }
originFetch(url, init) // originFetch(url, init)
.then(res => { // .then(res => {
let clone = res.clone(); // let clone = res.clone();
clone.text().then(text => { // clone.text().then(text => {
let modified = intercept(text, url, init.method, init.body, 'fetch'); // let modified = intercept(text, url, init.method, init.body, 'fetch');
resolve(new Response(modified, init)); // resolve(new Response(modified, init));
return; // return;
}); // });
}) // })
.catch(error => this.logger.error('fetch错误', error.stack || error.message)); // .catch(error => this.logger.error('fetch错误', error.stack || error.message));
}) // })
}; // };
window.fetch = fetchHandle; // window.fetch = fetchHandle;
// @ts-ignore // // @ts-ignore
fetch = fetchHandle; // fetch = fetchHandle;
})(fetch || window.fetch, glob.unsafeWindow || window); // })(fetch || window.fetch, glob.unsafeWindow || window);
// 监听xhr // 监听xhr
(xhr => { // (xhr => {
let originOpen = xhr.open; // let originOpen = xhr.open;
let originSend = xhr.send; // let originSend = xhr.send;
let logger = this.logger; // let logger = this.logger;
let modifyResponse = (response: { responseText: string, response: string }, after: string) => { // let modifyResponse = (response: { responseText: string, response: string }, after: string) => {
Object.defineProperty(response, 'responseText', { writable: true }); // Object.defineProperty(response, 'responseText', { writable: true });
Object.defineProperty(response, 'response', { writable: true }); // Object.defineProperty(response, 'response', { writable: true });
response.responseText = after; // response.responseText = after;
response.response = after; // response.response = after;
}; // };
XMLHttpRequest.prototype.open = function (method, url, async?, u?, p?) { // XMLHttpRequest.prototype.open = function (method, url, async?, u?, p?) {
this.addEventListener('readystatechange', function () { // this.addEventListener('readystatechange', function () {
if (this.readyState !== 4) return; // if (this.readyState !== 4) return;
if (!(this.responseType === '' || this.responseType === 'text')) return // if (!(this.responseType === '' || this.responseType === 'text')) return
let response = this.responseText || this.response; // let response = this.responseText || this.response;
let reqBody = this['reqBody']; // let reqBody = this['reqBody'];
logger.info('xhr this', this); // logger.info('xhr this', this);
if (response) { // if (response) {
let modified = intercept(response, url, method, reqBody, 'xhr'); // let modified = intercept(response, url, method, reqBody, 'xhr');
modifyResponse(this, modified); // modifyResponse(this, modified);
} // }
}, false); // }, false);
originOpen.call(this, method, url, async, u, p); // originOpen.call(this, method, url, async, u, p);
}; // };
XMLHttpRequest.prototype.send = function (body?) { // XMLHttpRequest.prototype.send = function (body?) {
this['reqBody'] = body; // this['reqBody'] = body;
originSend.call(this, body); // originSend.call(this, body);
} // }
})(XMLHttpRequest.prototype); // })(XMLHttpRequest.prototype);
let commonCssStr = COMMON_CSS.replace('{{}}', performance.now().toString()); let commonCssStr = COMMON_CSS.replace('{{}}', performance.now().toString());
this.commonUtils.styleInject(commonCssStr); this.commonUtils.styleInject(commonCssStr);
@ -190,13 +190,13 @@ export default class Initializer {
document.body.classList.add("scrollbar-transparent"); document.body.classList.add("scrollbar-transparent");
// fetch方法处理 // fetch方法处理
globVars.responseHandlers.push( // globVars.responseHandlers.push(
(...args: any[]) => this.fetchEventCallback.responseHandler.apply(this.fetchEventCallback, args) // (...args: any[]) => this.fetchEventCallback.responseHandler.apply(this.fetchEventCallback, args)
); // );
// fetch方法处理-翻译 // fetch方法处理-翻译
globVars.responseHandlers.push( // globVars.responseHandlers.push(
(...args: any[]) => this.translateNew.responseHandler.apply(this.translateNew, args) // (...args: any[]) => this.translateNew.responseHandler.apply(this.translateNew, args)
); // );
// 价格监控 TODO 重构 // 价格监控 TODO 重构
priceWatcherHandle(this.tornPDAUtils.isPDA(), this.tornPDAUtils.APIKey); priceWatcherHandle(this.tornPDAUtils.isPDA(), this.tornPDAUtils.APIKey);

View File

@ -123,24 +123,31 @@ export default class QuickFlyBtnHandler {
public async directFly(destIndex: number, typeIndex: number) { public async directFly(destIndex: number, typeIndex: number) {
// 获取key // 获取key
if(false){ // if(false){
let key; // let key;
try { // try {
const resp = await (await fetch('/travelagency.php')).text(); // const resp = await (await fetch('/travelagency.php')).text();
key = resp.match(/data-key="([0-9]+)"/)[1]; // key = resp.match(/data-key="([0-9]+)"/)[1];
} catch (e) { // } catch (e) {
this.msgWrapper.create('起飞参数获取失败', {}, 'error'); // this.msgWrapper.create('起飞参数获取失败', {}, 'error');
this.logger.error(e.stack); // this.logger.error(e.stack);
throw new Error('起飞参数获取失败'); // throw new Error('起飞参数获取失败');
} // }
} // }
let msg; let msg: string;
try { try {
msg = this.netHighLvlWrapper.doTravelFly(QuickFlyBtnHandler.getDestId(destIndex), null, ['standard', 'airstrip', 'private', 'business'][typeIndex]) msg = await this.netHighLvlWrapper.doTravelFly(QuickFlyBtnHandler.getDestId(destIndex), null, ['standard', 'airstrip', 'private', 'business'][typeIndex])
const response = JSON.parse(msg);
if (!response.success) {
this.msgWrapper.create('起飞失败 ' + response.error, {}, 'error');
this.logger.error('起飞失败 ' + response.error, response.err);
throw new Error('起飞失败 ' + response.error);
}
console.log(msg);
} catch (e) { } catch (e) {
this.msgWrapper.create(msg, {}, 'error'); this.msgWrapper.create('起飞时出现错误 ' + e.message, {}, 'error');
this.logger.error(e.stack); this.logger.error(e.stack);
throw new Error('起飞时出现错误'); throw new Error('起飞时出现错误 ' + e.message);
} }
this.msgWrapper.create('已起飞', {}, 'success'); this.msgWrapper.create('已起飞', {}, 'success');
} }