2022-10-17 15:19:18 +08:00

66 lines
2.7 KiB
TypeScript

import WuhuBase from "../WuhuBase";
import TornStyleBlock from "../utils/TornStyleBlock";
import WuhuConfig from "../WuhuConfig";
import Alert from "../utils/Alert";
import TornStyleSwitch from "../utils/TornStyleSwitch";
export default class PTHelper extends WuhuBase {
className = 'PTHelper';
private readonly observer;
private readonly usersPointSell;
constructor() {
super();
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 = WuhuConfig.get('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', () => {
WuhuConfig.set('ptQuickBuy', toggle.checked, false);
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);
}
}