wuhu-torn-helper/src/ts/class/action/SidebarHelper.ts

109 lines
4.3 KiB
TypeScript

import WuhuBase from "../WuhuBase";
import WuhuConfig from "../WuhuConfig";
import CommonUtils from "../utils/CommonUtils";
import Global from "../Global";
import Device from "../../enum/Device";
import Log from "../Log";
/**
* ### 边栏助手
* - 隐藏按钮
* - 4条转跳
*/
export default class SidebarHelper extends WuhuBase {
className = 'SidebarHelper';
private readonly sidebarRootNode: HTMLElement;
private readonly toggleBtn: HTMLButtonElement;
private isHide: boolean;
constructor() {
super();
this.sidebarRootNode = document.querySelector('#sidebarroot');
this.toggleBtn = document.createElement('button');
this.isHide = !!WuhuConfig.get('HideSidebar');
if (!document.body.classList.contains('without-sidebar') || window.location.href.includes('christmas_town.php')) {
this.initToggleBtn();
this.barRedirect();
} else {
Log.warn('[SidebarHelper] 页面未开启边栏,边栏助手退出');
}
}
// 初始化隐藏按钮
private initToggleBtn(): void {
if (Global.getInstance().device === Device.PC && this.sidebarRootNode && WuhuConfig.get('HideSidebarBtn')) {
this.isHide ? this.hideHandler() : this.showHandler();
let container = document.createElement('div');
container.append(this.toggleBtn);
container.style.width = '0';
container.style.zIndex = '1';
container.style.opacity = '0.75';
this.toggleBtn.classList.add('torn-btn');
this.sidebarRootNode.before(container);
this.toggleBtn.addEventListener('click', () => {
this.isHide ? this.showHandler() : this.hideHandler();
});
}
}
private hideHandler(): void {
this.sidebarRootNode.classList.add('wh-hide');
this.isHide = WuhuConfig.set('HideSidebar', true);
this.toggleBtn.innerHTML = '>>';
}
private showHandler(): void {
this.sidebarRootNode.classList.remove('wh-hide');
this.isHide = WuhuConfig.set('HideSidebar', false);
this.toggleBtn.innerHTML = '<<';
}
// 4条转跳
private barRedirect(): void {
if (WuhuConfig.get('barsRedirect')) {
const eb = document.getElementById('barEnergy') as HTMLAnchorElement;
const nb = document.getElementById('barNerve') as HTMLAnchorElement;
const hb = document.getElementById('barHappy') as HTMLAnchorElement;
const lb = document.getElementById('barLife') as HTMLAnchorElement;
if (eb) {
eb.addEventListener('click', () => location.href = '/gym.php');
eb.href = '/gym.php';
} else {
CommonUtils.elementReady('#barEnergy').then(eb => {
eb.addEventListener('click', () => location.href = '/gym.php');
(eb as HTMLAnchorElement).href = '/gym.php';
});
}
if (nb) {
nb.addEventListener('click', () => location.href = '/crimes.php');
nb.href = '/crimes.php';
} else {
CommonUtils.elementReady('#barNerve').then(nb => {
nb.addEventListener('click', () => location.href = '/crimes.php');
(nb as HTMLAnchorElement).href = '/crimes.php';
});
}
if (hb) {
hb.addEventListener('click', () => location.href = '/item.php#boosters-items');
hb.href = '/item.php#boosters-items';
} else {
CommonUtils.elementReady('#barHappy').then(hb => {
hb.addEventListener('click', () => location.href = '/item.php#boosters-items');
(hb as HTMLAnchorElement).href = '/item.php#boosters-items';
});
}
if (lb) {
lb.addEventListener('click', () => location.href = '/item.php#medical-items');
lb.href = '/item.php#medical-items';
} else {
CommonUtils.elementReady('#barLife').then(lb => {
lb.addEventListener('click', () => location.href = '/item.php#medical-items');
(lb as HTMLAnchorElement).href = '/item.php#medical-items';
});
}
}
}
}