2023-04-03 18:00:41 +08:00

69 lines
2.9 KiB
TypeScript

import TornStyleBlock from "../utils/TornStyleBlock";
import Alert from "../utils/Alert";
import TornStyleSwitch from "../utils/TornStyleSwitch";
import { Injectable } from "../../container/Injectable";
import ClassName from "../../container/ClassName";
import LocalConfigWrapper from "../LocalConfigWrapper";
@Injectable()
@ClassName('PTHelper')
export default class PTHelper {
private readonly observer;
private readonly usersPointSell;
constructor(
private readonly localConfigWrapper: LocalConfigWrapper,
) {
this.observer = new MutationObserver(e => {
for (const t of e) {
t.addedNodes.forEach(e => 'LI' === (e as HTMLElement).tagName && this.removeConfirm(e))
}
});
this.usersPointSell = document.querySelector('.users-point-sell');
let block = new TornStyleBlock('PT一键购买').insert2Dom();
let switcher = new TornStyleSwitch('开启');
block.append(switcher.getBase());
let toggle = switcher.getInput();
toggle.checked = this.localConfigWrapper.config.ptQuickBuy;
if (toggle.checked) {
new Alert('一键购买已开启');
for (const index in this.usersPointSell.children) {
'LI' === this.usersPointSell.children[index].tagName && this.removeConfirm(this.usersPointSell.children[index])
}
this.observer.observe(this.usersPointSell, { childList: true })
}
toggle.addEventListener('change', () => {
this.localConfigWrapper.config.ptQuickBuy = toggle.checked;
if (toggle.checked) {
for (const index in this.usersPointSell.children) {
'LI' === this.usersPointSell.children[index].tagName && this.removeConfirm(this.usersPointSell.children[index])
}
this.observer.observe(this.usersPointSell, { childList: true });
new Alert('一键购买已开启');
} else {
for (const index in this.usersPointSell.children) {
'LI' === this.usersPointSell.children[index].tagName && this.rollbackConfirm(this.usersPointSell.children[index])
}
this.observer.disconnect();
new Alert('一键购买已关闭');
}
});
}
private removeConfirm(elem): void {
let el = elem.firstElementChild;
el.classList.add('yes');
let old_href = el.getAttribute('href');
let new_href = old_href.replace(/=buy/, '=buy1').replace(/&points=\d{1,9}$/, '');
el.setAttribute('href', new_href);
}
private rollbackConfirm(elem): void {
let el = elem.firstElementChild;
el.classList.remove('yes');
let old_href = el.getAttribute('href');
let new_href = old_href.replace(/=buy1/, '=buy');
el.setAttribute('href', new_href);
}
}