This commit is contained in:
Liwanyi 2023-04-04 12:02:14 +08:00
parent d707d983fb
commit e623d6fd14
11 changed files with 66 additions and 260 deletions

View File

@ -1,7 +1,8 @@
import { Injectable } from "../container/Injectable"; import { Injectable } from "../container/Injectable";
import ClassName from "../container/ClassName"; import ClassName from "../container/ClassName";
import Logger from "./Logger"; import Logger from "./Logger";
import defaultConfig, { Config } from "./config/defaultConfig"; import defaultConfig, { Config, isNotified } from "./config/defaultConfig";
import MsgWrapper from "./utils/MsgWrapper";
@Injectable() @Injectable()
@ClassName('LocalConfigWrapper') @ClassName('LocalConfigWrapper')
@ -9,19 +10,25 @@ export default class LocalConfigWrapper {
constructor( constructor(
private readonly logger: Logger, private readonly logger: Logger,
private readonly msgWrapper: MsgWrapper,
) { ) {
} }
public get config(): Config { public get config(): Config {
let setLocal = this.setLocal; const _this = this;
return new Proxy(this.Local, { return new Proxy(this.Local, {
get(target: Config, prop: string) { get(target: Config, prop: string) {
return target[prop] || defaultConfig[prop]; return target[prop] ?? defaultConfig[prop];
}, },
set(target: Config, prop: string, value: any): boolean { set(target: Config, prop: string, value: any): boolean {
let config = target; let config = target;
let preVal = config[prop];
config[prop] = value; config[prop] = value;
setLocal(config); _this.setLocal(config);
let msg = `[${ prop }]值变更 ${ preVal }->${ value }`;
_this.logger.info(msg);
if (isNotified(prop))
_this.msgWrapper.create(msg);
return true; return true;
} }
}) })

View File

@ -3,7 +3,6 @@ import { missionDict } from "../dictionary/translation";
import getTaskHint from "../func/translate/getTaskHint"; import getTaskHint from "../func/translate/getTaskHint";
import CommonUtils from "./utils/CommonUtils"; import CommonUtils from "./utils/CommonUtils";
import Log from "./Log"; import Log from "./Log";
import WuhuConfig from "./WuhuConfig";
import SHOP_BEER_STATIC_ITEM_HTML from "../../static/html/buyBeer/shop_beer_static_item.html"; 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 ADD_BEER_HEAD_HTML from "../../static/html/buyBeer/add_beer_head.html";
import QUICK_CRIMES_HTML from "../../static/html/quick_crimes.html"; import QUICK_CRIMES_HTML from "../../static/html/quick_crimes.html";
@ -53,7 +52,8 @@ export default class UrlRouter {
_base.append(reloadSwitch.getBase()).insert2Dom(); _base.append(reloadSwitch.getBase()).insert2Dom();
reloadSwitch.getInput().addEventListener('change', () => { reloadSwitch.getInput().addEventListener('change', () => {
if (reloadSwitch.getInput().checked) window.location.replace(window.location.href); if (reloadSwitch.getInput().checked) window.location.replace(window.location.href);
WuhuConfig.set('SolveGoogleScriptPendingIssue', reloadSwitch.getInput().checked, true); this.localConfigWrapper.config.SolveGoogleScriptPendingIssue = reloadSwitch.getInput().checked;
// WuhuConfig.set('SolveGoogleScriptPendingIssue', reloadSwitch.getInput().checked, true);
}); });
_base.append(document.createElement('br')); _base.append(document.createElement('br'));

View File

@ -1,146 +0,0 @@
import WuhuBase from "./WuhuBase";
import Alert from "./utils/Alert";
import Log from "./Log";
/**
* @deprecated
*/
export default class WuhuConfig extends WuhuBase {
/**
*
* @param key
*/
// public static get(key: string | string[]) {
// let localPool = this.getLocal();
// if (typeof key === 'string') {
// return localPool[key];
// } else {
// let ret: string[] = [];
// key.forEach(k => {
// ret.push(localPool[k])
// });
// return ret;
// }
// }
public static set(key: string, val: any, isNotify = false, callback: Function = () => null) {
let config = WuhuConfig.getLocal();
let prev = config[key];
config[key] = val;
localStorage.setItem('wh_trans_settings', JSON.stringify(config));
if (isNotify) new Alert('已保存设置')
new Promise(() => callback()).then();
Log.info(`值变更:[${ key }] ${ JSON.stringify({ from: prev, to: val }) }`);
return val;
}
/**
*
*/
// public static setDefaults(): void {
// let count = 0;
// [
// // 开启翻译
// { key: 'transEnable', val: false },
// { key: 'transNew', val: true },
// // 快速犯罪
// { key: 'quickCrime', val: true },
// // 任务助手
// { key: 'missionHint', val: true },
// // 小镇攻略
// { key: 'xmasTownWT', val: true },
// // 小镇提醒
// { key: 'xmasTownNotify', val: true },
// // 起飞爆e
// { key: 'energyAlert', val: true },
// // 飞行闹钟
// { key: 'trvAlarm', val: true },
// // 啤酒提醒
// { key: '_15Alarm', val: true },
// // 捡垃圾助手
// { key: 'cityFinder', val: false },
// // 叠E保护
// { key: 'SEProtect', val: false },
// // PT一键购买
// { key: 'ptQuickBuy', val: false },
// // 光速拔刀 6-关闭
// { key: 'quickAttIndex', val: 2 },
// // 光速跑路 0-leave 1-mug 2-hos 3-关闭
// { key: 'quickFinishAtt', val: 3 },
// // 自动开打和结束
// { key: 'autoStartFinish', val: false },
// /**
// * @deprecated 废弃
// */
// { key: 'attRelocate', val: true },
// // 攻击自刷新 0-无间隔 1-5s 6-关闭
// { key: 'attReload', val: 6 },
// // 价格监视
// { key: 'priceWatcher', val: { xan: -1, pt: -1 } },
// // 开发者模式
// { key: 'isDev', val: false },
// // 啤酒提醒时间
// { key: '_15AlarmTime', val: 50 },
// // 4条转跳
// { key: 'barsRedirect', val: true },
// // 浮动存钱框
// { key: 'floatDepo', val: true },
// // 公司转跳存钱
// { key: 'companyRedirect', val: true },
// // 收起公司冰蛙效率表
// { key: 'companyBWCollapse', val: true },
// /**
// * @deprecated
// */
// { key: 'removeScripts', val: true },
// // 海外警告
// { key: 'abroadWarning', val: true },
// // 落地转跳
// { key: 'landedRedirect', val: '' },
// // 任何位置一键存钱
// { key: 'companyDepositAnywhere', val: false },
// // 火车提醒时间
// { key: 'CHTrainsDetect', val: 0 },
// // 火车提醒开关
// { key: 'CHTrainsDetectSwitch', val: true },
// // 隐藏个人资料头像
// { key: 'HideProfileImg', val: false },
// // 显示曾用名
// { key: 'ShowNameHistory', val: true },
// // 盯梢模式强度 0-550 1-950 2-1450 ms
// { key: 'WatchTargetFreq', val: 1 },
// // 隐藏侧边栏
// { key: 'HideSidebar', val: false },
// // 添加隐藏边栏按钮
// { key: 'HideSidebarBtn', val: true },
// // 搜索页占位区
// { key: 'SearchPagePlaceholder', val: true },
// // 解决一直转圈(加载中)的问题
// { key: 'SolveGoogleScriptPendingIssue', val: false },
// // 图标坐标
// { key: 'IconPosition', val: {} },
// // 记住图标位置
// { key: 'SaveIconPosition', val: false },
// // 现金变动提醒
// { key: 'CashChangeAlert', val: false },
// // 收集数据以改进翻译质量
// { key: 'CollectPlayerData', val: true },
// // 迷你资料卡显示上次行动时间
// { key: 'ShowMiniProfLastAct', val: true },
//
// /**
// * @deprecated
// */
// { key: 'dangerZone', val: false },
// ].forEach(df => {
// if (typeof WuhuConfig.get(df.key) !== typeof df.val) {
// WuhuConfig.set(df.key, df.val);
// count++;
// }
// });
// Log.info('设置默认值,新:' + count);
// }
}

View File

@ -1,4 +1,3 @@
import WuhuConfig from "../WuhuConfig";
import Log from "../Log"; import Log from "../Log";
import InfoUtils from "../utils/InfoUtils"; import InfoUtils from "../utils/InfoUtils";
import Alert from "../utils/Alert"; import Alert from "../utils/Alert";
@ -107,11 +106,16 @@ export default class BuyBeerHelper implements BeerMonitorLoop, ResponseInject {
public skip_today(): void { public skip_today(): void {
const date = new Date(); const date = new Date();
WuhuConfig.set('_15_alarm_ignore', [date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()], false); this.localConfigWrapper.config._15_alarm_ignore = [date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()];
// WuhuConfig.set('_15_alarm_ignore', [date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()], false);
// 通知 // 通知
const notify = new Alert(`明早8点前将不再提醒 <button id="wh-rd-btn-${ this.mathUtils.getRandomInt(0, 100) }">取消</button>`); const notify = new Alert(`明早8点前将不再提醒 <button id="wh-rd-btn-${ this.mathUtils.getRandomInt(0, 100) }">取消</button>`);
// 通知中的取消按钮 // 通知中的取消按钮
notify.getElement().querySelector('.wh-notify-msg button').addEventListener('click', () => WuhuConfig.set('_15_alarm_ignore', undefined, true)); notify.getElement().querySelector('.wh-notify-msg button')
.addEventListener(
'click',
() => this.localConfigWrapper.config._15_alarm_ignore.length = 0
);
} }
public setTimeHandler(): void { public setTimeHandler(): void {

View File

@ -1,4 +1,3 @@
import WuhuConfig from "../WuhuConfig";
import CommonUtils from "../utils/CommonUtils"; import CommonUtils from "../utils/CommonUtils";
import TornStyleBlock from "../utils/TornStyleBlock"; import TornStyleBlock from "../utils/TornStyleBlock";
import TornStyleSwitch from "../utils/TornStyleSwitch"; import TornStyleSwitch from "../utils/TornStyleSwitch";
@ -41,7 +40,7 @@ export default class ProfileHelper implements ResponseInject {
this.block.append(hideImgSwitch.getBase()); this.block.append(hideImgSwitch.getBase());
hideImgSwitch.getInput().addEventListener('change', () => { hideImgSwitch.getInput().addEventListener('change', () => {
document.body.classList.toggle('wh-hide_profile_img'); document.body.classList.toggle('wh-hide_profile_img');
WuhuConfig.set('HideProfileImg', hideImgSwitch.getInput().checked, true); this.localConfigWrapper.config.HideProfileImg = hideImgSwitch.getInput().checked;
}); });
if (this.localConfigWrapper.config.ShowNameHistory) { if (this.localConfigWrapper.config.ShowNameHistory) {
globVars.responseHandlers.push((url, body) => this.responseHandler(url, body)); globVars.responseHandlers.push((url, body) => this.responseHandler(url, body));

View File

@ -1,5 +1,4 @@
import TornStyleBlock from "../utils/TornStyleBlock"; import TornStyleBlock from "../utils/TornStyleBlock";
import WuhuConfig from "../WuhuConfig";
import TornStyleSwitch from "../utils/TornStyleSwitch"; import TornStyleSwitch from "../utils/TornStyleSwitch";
import { Injectable } from "../../container/Injectable"; import { Injectable } from "../../container/Injectable";
import ClassName from "../../container/ClassName"; import ClassName from "../../container/ClassName";
@ -21,7 +20,7 @@ export default class StackHelper {
input.onchange = e => { input.onchange = e => {
let target = e.target as HTMLInputElement; let target = e.target as HTMLInputElement;
document.body.classList.toggle('wh-gym-stack'); document.body.classList.toggle('wh-gym-stack');
WuhuConfig.set('SEProtect', target.checked, true); this.localConfigWrapper.config.SEProtect = target.checked;
}; };
} }

View File

@ -1,98 +1,10 @@
const defaultConfig = { const notifyKey = Symbol('ConfigNotifyKey');
// 开启翻译
transEnable: false,
transNew: true,
// 快速犯罪
quickCrime: true,
// 任务助手
missionHint: true,
// 小镇攻略
xmasTownWT: true,
// 小镇提醒
xmasTownNotify: true,
// 起飞爆e
energyAlert: true,
// 飞行闹钟
trvAlarm: true,
// 啤酒提醒
_15Alarm: true,
// 啤酒提醒时间
_15AlarmTime: 50,
// 啤酒提醒忽略当天
_15_alarm_ignore: '',
// 捡垃圾助手
cityFinder: false,
// 叠E保护
SEProtect: false,
// PT一键购买
ptQuickBuy: false,
// 光速拔刀 6-关闭
quickAttIndex: 2,
// 光速跑路 0-leave 1-mug 2-hos 3-关闭
quickFinishAtt: 3,
// 自动开打和结束
autoStartFinish: false,
// 攻击自刷新 0-无间隔 1-5s 6-关闭
attReload: 6,
// 价格监视
priceWatcher: { xan: -1, pt: -1 },
// 开发者模式
isDev: false,
// 4条转跳
barsRedirect: true,
// 浮动存钱框
floatDepo: true,
// 公司转跳存钱
companyRedirect: true,
// 收起公司冰蛙效率表
companyBWCollapse: true,
// 海外警告
abroadWarning: true,
// 落地转跳
landedRedirect: '',
// 任何位置一键存钱
companyDepositAnywhere: false,
// 火车提醒时间
CHTrainsDetect: 0,
// 火车提醒开关
CHTrainsDetectSwitch: true,
// 隐藏个人资料头像
HideProfileImg: false,
// 显示曾用名
ShowNameHistory: true,
// 盯梢模式强度 0-550 1-950 2-1450 ms
WatchTargetFreq: 1,
// 隐藏侧边栏
HideSidebar: false,
// 添加隐藏边栏按钮
HideSidebarBtn: true,
// 搜索页占位区
SearchPagePlaceholder: true,
// 解决一直转圈(加载中)的问题
SolveGoogleScriptPendingIssue: false,
// 图标坐标
IconPosition: {} as { x: number, y: number },
// 记住图标位置
SaveIconPosition: false,
// 现金变动提醒
CashChangeAlert: false,
// 收集数据以改进翻译质量
CollectPlayerData: true,
// 迷你资料卡显示上次行动时间
ShowMiniProfLastAct: true,
// 自定义css
CustomCss: '',
/** @deprecated */
attRelocate: false,
};
// } as const;
// export default defaultConfig;
class DefaultConfigType { class DefaultConfigType {
// 开启翻译 // 开启翻译
transEnable = false; transEnable = false;
@Notified()
transNew = true; transNew = true;
// 快速犯罪 // 快速犯罪
quickCrime = true; quickCrime = true;
@ -108,12 +20,16 @@ class DefaultConfigType {
trvAlarm = true; trvAlarm = true;
// 啤酒提醒 // 啤酒提醒
_15Alarm = true; _15Alarm = true;
_15_alarm_ignore = false;
@Notified()
_15_alarm_ignore: number[] = [];
// 啤酒提醒时间 // 啤酒提醒时间
_15AlarmTime = 50; _15AlarmTime = 50;
// 捡垃圾助手 // 捡垃圾助手
cityFinder = false; cityFinder = false;
// 叠E保护 // 叠E保护
@Notified()
SEProtect = false; SEProtect = false;
// PT一键购买 // PT一键购买
ptQuickBuy = false; ptQuickBuy = false;
@ -141,7 +57,9 @@ class DefaultConfigType {
companyBWCollapse = true; companyBWCollapse = true;
// 海外警告 // 海外警告
abroadWarning = true; abroadWarning = true;
// 落地转跳 // 落地转跳
@Notified()
landedRedirect = ''; landedRedirect = '';
// 任何位置一键存钱 // 任何位置一键存钱
companyDepositAnywhere = false; companyDepositAnywhere = false;
@ -149,7 +67,9 @@ class DefaultConfigType {
CHTrainsDetect = 0; CHTrainsDetect = 0;
// 火车提醒开关 // 火车提醒开关
CHTrainsDetectSwitch = true; CHTrainsDetectSwitch = true;
// 隐藏个人资料头像 // 隐藏个人资料头像
@Notified()
HideProfileImg = false; HideProfileImg = false;
// 显示曾用名 // 显示曾用名
ShowNameHistory = true; ShowNameHistory = true;
@ -161,7 +81,9 @@ class DefaultConfigType {
HideSidebarBtn = true; HideSidebarBtn = true;
// 搜索页占位区 // 搜索页占位区
SearchPagePlaceholder = true; SearchPagePlaceholder = true;
// 解决一直转圈(加载中)的问题 // 解决一直转圈(加载中)的问题
@Notified()
SolveGoogleScriptPendingIssue = false; SolveGoogleScriptPendingIssue = false;
// 图标坐标 // 图标坐标
IconPosition: Partial<{ x: number, y: number }> = {}; IconPosition: Partial<{ x: number, y: number }> = {};
@ -173,10 +95,22 @@ class DefaultConfigType {
CollectPlayerData = true; CollectPlayerData = true;
// 迷你资料卡显示上次行动时间 // 迷你资料卡显示上次行动时间
ShowMiniProfLastAct = true; ShowMiniProfLastAct = true;
// 自定义css // 自定义css
@Notified()
CustomCss = ''; CustomCss = '';
} }
export type Config = DefaultConfigType; export type Config = DefaultConfigType;
export default new DefaultConfigType() as Readonly<Config>;
let config = new DefaultConfigType();
export default config as Readonly<Config>;
function Notified(f: boolean = true) {
return Reflect.metadata(notifyKey, f);
}
export function isNotified(propertyKey: string): boolean {
return Reflect.getMetadata(notifyKey, config, propertyKey);
}

View File

@ -1,5 +1,4 @@
import Popup from "../utils/Popup"; import Popup from "../utils/Popup";
import WuhuConfig from "../WuhuConfig";
import ClassName from "../../container/ClassName"; import ClassName from "../../container/ClassName";
import { Injectable } from "../../container/Injectable"; import { Injectable } from "../../container/Injectable";
import LocalConfigWrapper from "../LocalConfigWrapper"; import LocalConfigWrapper from "../LocalConfigWrapper";
@ -19,7 +18,7 @@ export default class CustomCssHandler {
let button = pop.element.querySelector('button'); let button = pop.element.querySelector('button');
textarea.value = this.localConfigWrapper.config.CustomCss || ''; textarea.value = this.localConfigWrapper.config.CustomCss || '';
button.addEventListener('click', () => { button.addEventListener('click', () => {
WuhuConfig.set('CustomCss', textarea.value || '', true); this.localConfigWrapper.config.CustomCss = textarea.value || '';
}); });
} }
} }

View File

@ -5,7 +5,6 @@ import LOADING_IMG_HTML from "../../../static/html/loading_img.html";
import Timer from "./Timer"; import Timer from "./Timer";
import FetchUtils from "./FetchUtils"; import FetchUtils from "./FetchUtils";
import TornStyleSwitch from "./TornStyleSwitch"; import TornStyleSwitch from "./TornStyleSwitch";
import WuhuConfig from "../WuhuConfig";
import { MenuItemConfig } from "../ZhongIcon"; import { MenuItemConfig } from "../ZhongIcon";
import TRAVEL_STATE from "../../enum/TravelState"; import TRAVEL_STATE from "../../enum/TravelState";
import InventoryItemInfo from "../../interface/responseType/InventoryItemInfo"; import InventoryItemInfo from "../../interface/responseType/InventoryItemInfo";
@ -304,7 +303,7 @@ export default class CommonUtils {
(tip) && (switcher.getBase().setAttribute('title', tip)); (tip) && (switcher.getBase().setAttribute('title', tip));
_input.checked = this.localConfigWrapper.config[dictName]; _input.checked = this.localConfigWrapper.config[dictName];
_input.onchange = e => { _input.onchange = e => {
WuhuConfig.set(dictName, _input.checked, true); this.localConfigWrapper.config[dictName] = _input.checked;
if (changeEv) changeEv(e); if (changeEv) changeEv(e);
}; };
new_node.appendChild(switcher.getBase()); new_node.appendChild(switcher.getBase());
@ -339,7 +338,7 @@ export default class CommonUtils {
option.innerHTML = domText; option.innerHTML = domText;
select.appendChild(option); select.appendChild(option);
}); });
select.onchange = e => WuhuConfig.set(dictName, (<HTMLSelectElement>e.target).selectedIndex); select.onchange = e => this.localConfigWrapper.config[dictName] = (<HTMLSelectElement>e.target).selectedIndex;
label.appendChild(text); label.appendChild(text);
label.appendChild(select); label.appendChild(select);
new_node.appendChild(label); new_node.appendChild(label);

View File

@ -0,0 +1,12 @@
import ClassName from "../../container/ClassName";
import { Injectable } from "../../container/Injectable";
import Alert from "./Alert";
import IWHNotify from "../../interface/IWHNotify";
@ClassName('MsgWrapper')
@Injectable()
export default class MsgWrapper {
create(msg: string, options: IWHNotify = {},): Alert {
return new Alert(msg, options);
}
}

View File

@ -1,4 +1,3 @@
import WuhuConfig from "../../class/WuhuConfig";
import Popup from "../../class/utils/Popup"; import Popup from "../../class/utils/Popup";
import LocalConfigWrapper from "../../class/LocalConfigWrapper"; import LocalConfigWrapper from "../../class/LocalConfigWrapper";
import { Container } from "../../container/Container"; import { Container } from "../../container/Container";
@ -28,7 +27,7 @@ export default function landedRedirect() {
buttonFct.innerHTML = '填入帮派金库金库'; buttonFct.innerHTML = '填入帮派金库金库';
buttonTest.innerHTML = '测试链接'; buttonTest.innerHTML = '测试链接';
buttonSave.addEventListener('click', () => WuhuConfig.set('landedRedirect', input.value, true)); buttonSave.addEventListener('click', () => localConfigWrapper.config.landedRedirect = input.value);
buttonCmp.addEventListener('click', () => input.value = 'https://www.torn.com/companies.php#/option=funds'); buttonCmp.addEventListener('click', () => input.value = 'https://www.torn.com/companies.php#/option=funds');
buttonFct.addEventListener('click', () => input.value = 'https://www.torn.com/factions.php?step=your#/tab=armoury'); buttonFct.addEventListener('click', () => input.value = 'https://www.torn.com/factions.php?step=your#/tab=armoury');
buttonTest.addEventListener('click', () => window.open(input.value)); buttonTest.addEventListener('click', () => window.open(input.value));