This commit is contained in:
Liwanyi 2022-09-23 18:15:45 +08:00
parent 8591a3b844
commit 6b85937b46
30 changed files with 282 additions and 94 deletions

View File

@ -1,6 +1,6 @@
import BuyBeer, { BeerMonitorLoop } from "../func/utils/BuyBeer";
import Device from "../enum/Device";
import WindowActiveState from "../func/utils/WindowActiveState";
import WindowActiveState from "./action/WindowActiveState";
import WuhuBase from "./WuhuBase";
import IGlobal from "../interface/IGlobal";
import Utils from "./utils/Utils";
@ -43,10 +43,11 @@ export default class Global extends WuhuBase implements IGlobal {
} = null;
// 窗口活动状态
isWindowActive = null;
// isWindowActive = null;
isWindowActive = WindowActiveState.getInstance();
private constructor() {
Log.info('Global初始化');
Log.info('WH脚本参数初始化');
super();
this.window = window;
this.unsafeWindow = window.unsafeWindow || null;
@ -59,7 +60,7 @@ export default class Global extends WuhuBase implements IGlobal {
this.beer = BuyBeer();
this.popup_node = null;
this.notifies = { count: 0 };
this.isWindowActive = WindowActiveState();
// this.isWindowActive = WindowActiveState.getInstance();
this.href = window.location.href;
this.bodyAttrs = {};
@ -71,8 +72,6 @@ export default class Global extends WuhuBase implements IGlobal {
this.GM_xmlhttpRequest = null;
}
}
let initializeTooltip = window;
Log.info({ initializeTooltip })
for (let i = 0; i < document.body.attributes.length; i++) {
let item = document.body.attributes.item(i);
@ -91,13 +90,13 @@ export default class Global extends WuhuBase implements IGlobal {
}
);
Log.info('Global初始化结束');
Log.info('WH脚本参数初始化结束');
}
static getInstance(this): Global {
if (!(<any>this).instance) {
(<any>this).instance = new this();
if (!this.instance) {
this.instance = new this();
}
return (<any>this).instance;
return this.instance;
}
}

View File

@ -6,18 +6,25 @@ export default class WuhuBase {
static glob: IGlobal = null;
static getLocal(): IWHSettings {
return JSON.parse(localStorage.getItem('wh_trans_settings'));
return JSON.parse(localStorage.getItem('wh_trans_settings')) || {};
}
static conditionInterrupt() {
if (
let title: HTMLElement | { innerText: string } = (document.querySelector('#skip-to-content') ||
document.querySelector('[href*="#skip-to-content"]')) as HTMLElement || { innerText: '' };
let condition = (
document.title.toLowerCase().includes('just a moment') ||
document.querySelector('#skip-to-content').innerText.toLowerCase().includes('please validate')
)
throw '芜湖';
title.innerText.toLowerCase().includes('please validate') ||
document.querySelector('div.container div.cf .iAmUnderAttack') !== null
);
if (condition) throw '芜湖';
}
constructor() {
Log.info('创建对象:' + this.constructor.name)
}
static getGlob(): IGlobal {
return this.glob
}
}

View File

@ -3,7 +3,7 @@ import miniprofTrans from "../func/translate/miniprofTrans";
import addStyle from "../func/utils/addStyle";
import Utils from "./utils/Utils";
import WuhuBase from "./WuhuBase";
import TravelItem from "./TravelItemFetchLoop";
import TravelItem from "./action/TravelItem";
import Global from "./Global";
import Log from "./Log";
@ -13,7 +13,7 @@ export default class WuHuTornHelper extends WuhuBase {
Log.info('WuHuTornHelper初始化');
WuhuBase.glob = Global.getInstance();
let glob = WuhuBase.glob;
glob.fStock = new TravelItem('https://yata.yt/api/v1/travel/export/');
glob.fStock = TravelItem.getInstance();
Log.info(glob.fStock)
// 请求通知权限

View File

@ -16,7 +16,6 @@ import WuhuBase from "./WuhuBase";
import Log from "./Log";
import Utils from "./utils/Utils";
import WuhuConfig from "./WuhuConfig";
import Alert from "./utils/Alert";
export default class ZhongIcon extends WuhuBase {
static ZhongNode: MyHTMLElement = null;
@ -968,7 +967,8 @@ color:black;
// Log.info(await Utils.getSidebarData())
// Log.info(await Utils.getUserState())
new Alert("123");
Log.info(ZhongIcon.getGlob());
Log.info(ZhongIcon.glob);
Log.info('测试结束');
},

View File

@ -1,15 +1,15 @@
import Utils from "./utils/Utils";
import WuhuBase from "./WuhuBase";
import Utils from "../utils/Utils";
import WuhuBaseAction from "./WuhuBaseAction";
export default class TravelItem extends WuhuBase {
export default class TravelItem extends WuhuBaseAction {
obj: any = null;
res: any = null;
constructor(destination: string) {
private constructor() {
super();
setInterval(async () => {
if (!WuhuBase.glob.isWindowActive()) return;
const res = await Utils.COFetch(destination);
if (!TravelItem.glob.isWindowActive.get()) return;
const res = await Utils.COFetch('https://yata.yt/api/v1/travel/export/');
this.obj = JSON.parse(res);
}, 30 * 1000);
}

View File

@ -0,0 +1,47 @@
import uuidv4 from "../../func/utils/uuidv4";
import WuhuBaseAction from "./WuhuBaseAction";
export default class WindowActiveState extends WuhuBaseAction {
isFocus = false;
uuid = uuidv4();
private constructor() {
super();
if (self !== top) return null;
localStorage.setItem('whuuid', this.uuid);
document.addEventListener('visibilitychange',
() => (document.visibilityState !== 'hidden') && (localStorage.setItem('whuuid', this.uuid))
);
addEventListener('focus', () => this.isFocus = true);
addEventListener('blur', () => this.isFocus = false);
}
get(): boolean {
// 当前窗口获得了焦点 优先级最高
if (this.isFocus) return true;
// 可视性
if (!document.hidden) return true;
// 全部在后台使用唯一id判断
return this.uuid === localStorage.getItem('whuuid')
}
}
// export default function WindowActiveState() {
// if (self !== top) return null;
// const uuid = uuidv4();
// let isFocus = false;
// localStorage.setItem('whuuid', uuid);
// document.addEventListener('visibilitychange', () =>
// (document.visibilityState !== 'hidden') && (localStorage.setItem('whuuid', uuid))
// );
// addEventListener('focus', () => isFocus = true)
// addEventListener('blur', () => isFocus = false)
// return function (): boolean {
// // 当前窗口获得了焦点 优先级最高
// if (isFocus) return true;
// // 可视性
// if (!document.hidden) return true;
// // 全部在后台使用唯一id判断
// return uuid === localStorage.getItem('whuuid')
// };
// }

View File

@ -1,3 +1,15 @@
import WuhuBase from "../WuhuBase";
export default class WuhuBaseAction extends WuhuBase {}
export default class WuhuBaseAction extends WuhuBase {
name: string
protected constructor() {
super();
}
static getInstance(this) {
if (!this.instance)
this.instance = new this();
return this.instance;
}
}

View File

@ -1,12 +1,15 @@
import WuhuBase from "../WuhuBase";
import Log from "../Log";
import Utils from "./Utils";
import addStyle from "../../func/utils/addStyle";
export default class Alert extends WuhuBase{
export default class Alert extends Utils {
static hasContainer: boolean = false;
static container: HTMLElement = null;
notify: HTMLElement = null;
callback: Function = null;
constructor(msg: string, options: WHNotifyOpt = {}) {
super();
let { isWindowActive, notifies } = WuhuBase.glob;
let { isWindowActive, notifies } = Alert.glob;
let {
timeout = 3,
@ -17,7 +20,109 @@ export default class Alert extends WuhuBase{
sysNotifyClick = () => window.focus()
} = options;
if (!isWindowActive() || (self !== top)) return null;
// 后台窗口、iframe内判断
if (!Alert.glob.isWindowActive.get() || (self !== top)) return null;
// 通知的容器
if (!Alert.container) Alert.initContainer();
this.notify = Alert.create(msg);
this.callback = callback;
}
static create(msg) {
const date = new Date();
// 通知的唯一id
const uid = '' + Utils.getRandomInt(1000, 9999);
// 每条通知
const new_node: MyHTMLElement = document.createElement('div');
new_node.id = `wh-notify-${ uid }`;
new_node.classList.add('wh-notify-item');
new_node.innerHTML = `<div class="wh-notify-bar"></div>
<div class="wh-notify-cont">
<div class="wh-notify-close"></div>
<div class="wh-notify-msg"><p>${ msg }</p></div>
</div>`;
this.container.append(new_node);
this.container['msgInnerText'] = new_node.querySelector('.wh-notify-msg').innerText;
// 进度条node
const progressBar: HTMLElement = new_node.querySelector('.wh-notify-bar');
// 是否hover
let mouse_enter = false;
new_node.addEventListener('mouseenter', () => mouse_enter = true, true);
new_node.addEventListener('mouseleave', () => mouse_enter = false);
// 通知进度条
let progressCount = 101;
// 删除通知
new_node.close = () => {
clearInterval(intervalID);
new_node.remove();
callback();
};
// 计时器
let intervalID = window.setInterval(() => {
if (mouse_enter) {
progressCount = 101;
progressBar.style.width = '100%';
return;
}
progressCount--;
progressBar.style.width = `${ progressCount }%`;
if (progressCount === 0) new_node.remove();
}, timeout * 1000 / 100);
new_node.querySelector('.wh-notify-close').addEventListener('click', new_node.close);
return new_node;
}
static initContainer() {
this.container = document.createElement('div');
this.container.id = 'wh-notify';
addStyle(`
#wh-notify {
display: inline-block;
position: fixed;
top: 0;
left: calc(50% - 180px);
width: 360px;
z-index: 9999990;
color:#333;
}
#wh-notify a{
color:red;
text-decoration:none;
}
#wh-notify .wh-notify-item {
/*height: 50px;*/
background: rgb(239 249 255 / 90%);
border-radius: 2px;
margin: 0.5em 0 0 0;
box-shadow: 0 0 5px 0px #959595;
}
#wh-notify .wh-notify-item:hover {
background: rgb(239 249 255 / 98%);
}
#wh-notify .wh-notify-item .wh-notify-bar {
height:2px;
background:#2196f3;
}
#wh-notify .wh-notify-item .wh-notify-close {
float:right;
padding:0;
width:16px;height:16px;
background:url('data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%201024%201024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cpath%20d%3D%22M923%20571H130.7c-27.6%200-50-22.4-50-50s22.4-50%2050-50H923c27.6%200%2050%2022.4%2050%2050s-22.4%2050-50%2050z%22%20fill%3D%22%232196f3%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E') no-repeat center;
background-size:100%;
margin: 6px 6px 0 0;
cursor: pointer;
}
#wh-notify .wh-notify-item .wh-notify-msg {
padding:12px;
}
`);
document.body.append(this.container);
}
close() {
this.notify.remove();
this.notify = null;
}
}

View File

@ -176,4 +176,36 @@ export default class Utils extends WuhuBase {
}
return fetch(url, req_params);
}
/**
* mutation.observe
* @param {String} selector - CSS规则的HTML元素选择器
* @param {Document} content -
* @param {number} timeout -
* @returns {Promise<HTMLElement|null>}
*/
static elementReady(selector: string, content: Document = document, timeout: number = 30000): Promise<HTMLElement> {
return new Promise((resolve, reject) => {
let el = content.querySelector(selector) as HTMLElement;
if (el) {
resolve(el);
return
}
let observer = new MutationObserver((_, observer) => {
content.querySelectorAll(selector).forEach((element) => {
observer.disconnect();
resolve(element as HTMLElement);
});
});
setTimeout(() => {
observer.disconnect();
reject(`等待元素超时! [${ selector }]\n${ content.documentElement.tagName }`);
}, timeout);
observer.observe(content.documentElement, { childList: true, subtree: true });
});
}
static createSystemNotify(): Notification {
return null;
}
}

View File

@ -1,22 +1,22 @@
import getWhSettingObj from "./func/utils/getWhSettingObj";
import elementReady from "./func/utils/elementReady";
import depoHelper from "./func/module/depoHelper";
import travelHelper from "./func/module/travelHelper";
import attackHelper from "./func/module/attackHelper";
import priceWatcherHandle from "./func/module/priceWatcherHandle";
import WuhuBase from "./class/WuhuBase";
import WuhuConfig from "./class/WuhuConfig";
export class Common extends WuhuBase {
static resolve() {
let glob = WuhuBase.glob;
let glob = Common.glob;
// 价格监控
priceWatcherHandle(glob.isPDA, glob.PDA_APIKey);
// 啤酒提醒
if (getWhSettingObj()['_15Alarm']) glob.beer.start();
if (WuhuConfig.get('_15Alarm')) glob.beer.start();
// 点击4条转跳对应的页面
if (getWhSettingObj()['barsRedirect']) {
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;
@ -63,7 +63,7 @@ export class Common extends WuhuBase {
*
* TODO
*/
if (getWhSettingObj()['removeScripts']) {
if (WuhuConfig.get('removeScripts')) {
document.querySelectorAll('script[src*="google"]').forEach(item => item.remove());
document.querySelectorAll('#gtm_tag').forEach(item => item.remove());
document.querySelectorAll('script[src*="chat/gonline"]').forEach(item => item.remove());

View File

@ -3,7 +3,7 @@ import elementReady from "../utils/elementReady";
import getWhSettingObj from "../utils/getWhSettingObj";
import addActionBtn from "../utils/addActionBtn";
import addStyle from "../utils/addStyle";
import log from "../utils/log";
import log from "../utils/@deprecated/log";
import ZhongIcon from "../../class/ZhongIcon";
import WuhuBase from "../../class/WuhuBase";
import WuhuConfig from "../../class/WuhuConfig";

View File

@ -1,7 +1,7 @@
import elementReady from "../utils/elementReady";
import addStyle from "../utils/addStyle";
import toThousands from "../utils/toThousands";
import log from "../utils/log";
import log from "../utils/@deprecated/log";
import Utils from "../../class/utils/Utils";
export default function cityFinder(): void {

View File

@ -1,7 +1,7 @@
import UserScriptEngine from "../../enum/UserScriptEngine";
import WHNotify from "../utils/WHNotify";
import addStyle from "../utils/addStyle";
import log from "../utils/log";
import log from "../utils/@deprecated/log";
import Utils from "../../class/utils/Utils";
// gs loader

View File

@ -1,5 +1,5 @@
import getWhSettingObj from "../utils/getWhSettingObj";
import log from "../utils/log";
import log from "../utils/@deprecated/log";
import toThousands from "../utils/toThousands";
import WHNotify from "../utils/WHNotify";

View File

@ -1,6 +1,6 @@
import popupMsg from "../utils/popupMsg";
import WHNotify from "../utils/WHNotify";
import log from "../utils/log";
import log from "../utils/@deprecated/log";
import Utils from "../../class/utils/Utils";
// 守望者

View File

@ -5,7 +5,7 @@ import WHNotify from "../utils/WHNotify";
import getWhSettingObj from "../utils/getWhSettingObj";
import addActionBtn from "../utils/addActionBtn";
import addStyle from "../utils/addStyle";
import log from "../utils/log";
import log from "../utils/@deprecated/log";
import doQuickFly from "./doQuickFly";
import ZhongIcon from "../../class/ZhongIcon";
import WuhuBase from "../../class/WuhuBase";

View File

@ -1,5 +1,5 @@
import { eventsDict, gymList, ocList } from "../../dictionary/translation";
import log from "../utils/log";
import log from "../utils/@deprecated/log";
export default function eventsTrans(events: JQuery = $('span.mail-link')) {
const index = window.location.href.indexOf('events.php#/step=received') >= 0 ? 1 : 0;

View File

@ -1,4 +1,4 @@
import log from "../utils/log";
import log from "../utils/@deprecated/log";
/**
* observe

View File

@ -34,7 +34,7 @@ import initOB from "./initOB";
import titleTrans from "./titleTrans";
import contentTitleLinksTrans from "./contentTitleLinksTrans";
import showItemInfoTrans from "./showItemInfoTrans";
import log from "../utils/log";
import log from "../utils/@deprecated/log";
import contentTitleLinksTransReact from "./contentTitleLinksTransReact";
import titleTransReact from "./titleTransReact";

View File

@ -1,5 +1,8 @@
import getWhSettingObj from "./getWhSettingObj";
import getWhSettingObj from "../getWhSettingObj";
/**
* @deprecated
*/
function debug() {
try {
return getWhSettingObj()['isDev'] || false;
@ -13,9 +16,21 @@ function debug() {
* @deprecated
*/
const log = {
/**
* @deprecated
*/
error: (...o) => (debug()) && (console.error('[WH]', ...o)),
/**
* @deprecated
*/
info: (...o) => (debug()) && (console.log('[WH]', ...o)),
/**
* @deprecated
*/
debug,
}
/**
* @deprecated
*/
export default log

View File

@ -1,5 +1,5 @@
import getWhSettingObj from "./getWhSettingObj";
import log from "./log";
import log from "./@deprecated/log";
import WHNotify from "./WHNotify";
import setWhSetting from "./setWhSetting";
import audioPlay from "./audioPlay";

View File

@ -1,6 +1,6 @@
import UserScriptEngine from "../../enum/UserScriptEngine";
import getScriptEngine from "./getScriptEngine";
import log from "./log";
import log from "./@deprecated/log";
/**
* @deprecated

View File

@ -25,14 +25,13 @@ export default function WHNotify(msg: string, options: WHNotifyOpt = {}): MyHTML
sysNotifyClick = () => window.focus()
} = options;
if (!isWindowActive() || (self !== top)) return null;
if (!isWindowActive.get() || (self !== top)) return null;
const date = new Date();
// 通知的唯一id
const uid = `${ date.getHours() }${ date.getSeconds() }${ date.getMilliseconds() }${ Utils.getRandomInt(1000, 9999) }`;
// 通知容器id
const node_id = 'wh-notify';
// 通知的容器
let notify_contain: MyHTMLElement = document.querySelector(`#${ node_id }`);
let notify_contain: MyHTMLElement = document.querySelector(`#wh-notify`);
// 添加通知到容器
const add_notify = () => {
// 每条通知
@ -77,9 +76,9 @@ export default function WHNotify(msg: string, options: WHNotifyOpt = {}): MyHTML
// 不存在容器 创建
if (!notify_contain) {
notify_contain = document.createElement('div');
notify_contain.id = node_id;
notify_contain.id = 'wh-notify';
addStyle(`
#${ node_id } {
#wh-notify {
display: inline-block;
position: fixed;
top: 0;
@ -88,25 +87,25 @@ export default function WHNotify(msg: string, options: WHNotifyOpt = {}): MyHTML
z-index: 9999990;
color:#333;
}
#${ node_id } a{
#wh-notify a{
color:red;
text-decoration:none;
}
#${ node_id } .wh-notify-item {
#wh-notify .wh-notify-item {
/*height: 50px;*/
background: rgb(239 249 255 / 90%);
border-radius: 2px;
margin: 0.5em 0 0 0;
box-shadow: 0 0 5px 0px #959595;
}
#${ node_id } .wh-notify-item:hover {
#wh-notify .wh-notify-item:hover {
background: rgb(239 249 255 / 98%);
}
#${ node_id } .wh-notify-item .wh-notify-bar {
#wh-notify .wh-notify-item .wh-notify-bar {
height:2px;
background:#2196f3;
}
#${ node_id } .wh-notify-item .wh-notify-close {
#wh-notify .wh-notify-item .wh-notify-close {
float:right;
padding:0;
width:16px;height:16px;
@ -115,7 +114,7 @@ background-size:100%;
margin: 6px 6px 0 0;
cursor: pointer;
}
#${ node_id } .wh-notify-item .wh-notify-msg {
#wh-notify .wh-notify-item .wh-notify-msg {
padding:12px;
}
`);

View File

@ -1,22 +0,0 @@
// 返回一个可用查询当前窗口是否激活的函数
import uuidv4 from "./uuidv4";
export default function WindowActiveState() {
if (self !== top) return null;
const uuid = uuidv4();
let isFocus = false;
localStorage.setItem('whuuid', uuid);
document.addEventListener('visibilitychange', () =>
(document.visibilityState !== 'hidden') && (localStorage.setItem('whuuid', uuid))
);
addEventListener('focus', () => isFocus = true)
addEventListener('blur', () => isFocus = false)
return function (): boolean {
// 当前窗口获得了焦点 优先级最高
if (isFocus) return true;
// 可视性
if (!document.hidden) return true;
// 全部在后台使用唯一id判断
return uuid === localStorage.getItem('whuuid')
};
}

View File

@ -1,4 +1,4 @@
import log from "./log";
import log from "./@deprecated/log";
/**
* style

View File

@ -1,4 +1,4 @@
import log from "./log";
import log from "./@deprecated/log";
/**
*

View File

@ -10,7 +10,7 @@ function autoFetchJSON(dest, time = 30) {
let obj;
const res = Utils.COFetch(dest);
setInterval(async () => {
if (!WuhuBase.glob.isWindowActive()) return;
if (!WuhuBase.glob.isWindowActive.get()) return;
const res = await Utils.COFetch(dest);
obj = JSON.parse(res);
}, time * 1000);
@ -23,6 +23,4 @@ function autoFetchJSON(dest, time = 30) {
return obj;
}
};
}
export default autoFetchJSON
}

View File

@ -1,9 +1,5 @@
/**
* mutation.observe
* @param {String} selector - CSS规则的HTML元素选择器
* @param {Document} content -
* @param {number} timeout -
* @returns {Promise<HTMLElement|null>}
* @deprecated
*/
export default function elementReady(selector, content = document, timeout: number = 30000): Promise<HTMLElement | null> {
return new Promise((resolve, reject) => {

View File

@ -1,5 +1,5 @@
import elementReady from "./elementReady";
import log from "./log";
import log from "./@deprecated/log";
/**
*

View File

@ -45,5 +45,5 @@ export default interface IGlobal {
};
// 窗口活动状态
isWindowActive?(): boolean;
isWindowActive: { get: () => boolean };
}