TS重构
This commit is contained in:
parent
a5f0a82211
commit
a1d1c06632
12
global.d.ts
vendored
12
global.d.ts
vendored
@ -8,6 +8,7 @@ declare interface Window {
|
||||
jQuery?: JQueryStatic;
|
||||
WHPARAMS?: any;
|
||||
ReactDOM?: any;
|
||||
hasWHQuickFlyOpt?: boolean;
|
||||
|
||||
addRFC(url: URL | string): string;
|
||||
|
||||
@ -39,4 +40,15 @@ declare interface Element {
|
||||
|
||||
declare interface Notification {
|
||||
id?: number;
|
||||
}
|
||||
|
||||
declare interface Array<T> {
|
||||
fest_date_dict?: { [key: string]: { name: string, eff: string } };
|
||||
fest_date_list?: string[];
|
||||
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
declare interface Navigator {
|
||||
userAgentData?: any;
|
||||
}
|
||||
@ -1,6 +1,10 @@
|
||||
import getWhSettingObj from "./getWhSettingObj";
|
||||
import log from "./log";
|
||||
import WHNotify from "./WHNotify";
|
||||
import getRandomInt from "./getRandomInt";
|
||||
import setWhSetting from "./setWhSetting";
|
||||
import audioPlay from "./audioPlay";
|
||||
import getUserState from "./getUserState";
|
||||
|
||||
// 啤酒
|
||||
export default function BuyBeer() {
|
||||
@ -82,7 +86,7 @@ export default function BuyBeer() {
|
||||
return loop;
|
||||
}
|
||||
|
||||
interface BeerMonitorLoop {
|
||||
export interface BeerMonitorLoop {
|
||||
start?: Function;
|
||||
stop?: Function;
|
||||
set_time?: Function;
|
||||
|
||||
23
src/func/utils/WindowActiveState.ts
Normal file
23
src/func/utils/WindowActiveState.ts
Normal file
@ -0,0 +1,23 @@
|
||||
// 返回一个可用查询当前窗口是否激活的函数
|
||||
import uuidv4 from "./uuidv4";
|
||||
|
||||
export default function WindowActiveState() {
|
||||
let glob = window.WHPARAMS;
|
||||
if (glob.isIframe) 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')
|
||||
};
|
||||
}
|
||||
15
src/func/utils/audioPlay.ts
Normal file
15
src/func/utils/audioPlay.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import log from "./log";
|
||||
|
||||
/**
|
||||
* 播放音频
|
||||
* @param {string} url 播放的音频URL
|
||||
* @returns {undefined}
|
||||
*/
|
||||
export default function audioPlay(url: string = 'https://www.torn.com/js/chat/sounds/Warble_1.mp3') {
|
||||
const audio = new Audio(url);
|
||||
audio.addEventListener("canplaythrough", () => {
|
||||
audio.play()
|
||||
.catch(err => log(err))
|
||||
.then();
|
||||
});
|
||||
}
|
||||
12
src/func/utils/setWhSetting.ts
Normal file
12
src/func/utils/setWhSetting.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import getWhSettingObj from "./getWhSettingObj";
|
||||
import WHNotify from "./WHNotify";
|
||||
|
||||
// 插件的配置 setter
|
||||
export default function setWhSetting(key: string, value: any, notify: boolean = true) {
|
||||
const obj = getWhSettingObj()
|
||||
obj[key] = value
|
||||
localStorage.setItem('wh_trans_settings', JSON.stringify(obj))
|
||||
|
||||
// 通知
|
||||
if (notify) WHNotify('已保存设置')
|
||||
}
|
||||
8
src/func/utils/uuidv4.ts
Normal file
8
src/func/utils/uuidv4.ts
Normal file
@ -0,0 +1,8 @@
|
||||
// 返回UUID
|
||||
export default function uuidv4() {
|
||||
if (crypto.randomUUID) return crypto.randomUUID();
|
||||
// @ts-ignore
|
||||
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
||||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||
);
|
||||
}
|
||||
156
src/init.ts
156
src/init.ts
@ -7,6 +7,9 @@ import Device from "./enum/Device";
|
||||
import getPlayerInfo from "./func/utils/getPlayerInfo";
|
||||
import autoFetchJSON from "./func/utils/autoFetchJSON";
|
||||
import priceWatcherHandle from "./func/utils/priceWatcherHandle";
|
||||
import BuyBeer from "./func/utils/BuyBeer";
|
||||
import WindowActiveState from "./func/utils/WindowActiveState";
|
||||
import addStyle from "./func/utils/addStyle";
|
||||
|
||||
// 初始化方法,获取必要全局参数
|
||||
export default function init(glob: Global) {
|
||||
@ -78,7 +81,7 @@ export default function init(glob: Global) {
|
||||
glob.priceWatcher = glob.isIframe ? null : priceWatcherHandle();
|
||||
|
||||
// 抢啤酒
|
||||
glob.beer = buyBeer();
|
||||
glob.beer = BuyBeer();
|
||||
glob.popup_node = null;
|
||||
|
||||
// 当窗口关闭时关闭所有还存在的通知
|
||||
@ -93,4 +96,155 @@ export default function init(glob: Global) {
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 记录当前窗口唯一id
|
||||
glob.isWindowActive = WindowActiveState();
|
||||
|
||||
addStyle(`
|
||||
.wh-hide{display:none;}
|
||||
#wh-trans-icon{
|
||||
user-select:none;
|
||||
display: inline-block;
|
||||
position: fixed;
|
||||
top:5px;
|
||||
left:5px;
|
||||
z-index:100010;
|
||||
border-radius:4px;
|
||||
max-width: 220px;
|
||||
box-shadow: 0 0 3px 1px #8484848f;
|
||||
}
|
||||
div#effectiveness-wrap{overflow-y:hidden;}
|
||||
@media screen and (max-width: 600px) {
|
||||
#wh-trans-icon{top:0;left:112px;}
|
||||
/* 冰蛙公司效率表 */
|
||||
div#effectiveness-wrap {
|
||||
margin-left: -80px;
|
||||
margin-right: -76px;
|
||||
}
|
||||
}
|
||||
#wh-trans-icon select{width:110px;}
|
||||
#wh-trans-icon a {
|
||||
text-decoration: none;
|
||||
color: #006599;
|
||||
background: none;
|
||||
}
|
||||
#wh-trans-icon:not(.wh-icon-expanded):hover {background: #f8f8f8;}
|
||||
#wh-trans-icon button{
|
||||
margin:0;
|
||||
padding:0;
|
||||
border:0;
|
||||
cursor:pointer;
|
||||
}
|
||||
#wh-inittimer{margin-top:6px;color:#b0b0b0;}
|
||||
#wh-gSettings div{margin: 4px 0;}
|
||||
#wh-trans-icon .wh-container{
|
||||
margin:0;
|
||||
padding:0 16px 16px;
|
||||
border:0;
|
||||
}
|
||||
#wh-trans-icon-btn{
|
||||
height:16px;
|
||||
width:16px;
|
||||
background: url('data:image/svg+xml;utf8,<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M160 144a32 32 0 0 0-32 32V864a32 32 0 0 0 32 32h688a32 32 0 0 0 32-32V176a32 32 0 0 0-32-32H160z m0-64h688a96 96 0 0 1 96 96V864a96 96 0 0 1-96 96H160a96 96 0 0 1-96-96V176a96 96 0 0 1 96-96z"/><path d="M482.176 262.272h59.616v94.4h196v239.072h-196v184.416h-59.616v-184.416H286.72v-239.04h195.456V262.24z m-137.504 277.152h137.504v-126.4H344.64v126.4z m197.12 0h138.048v-126.4H541.76v126.4z"/></svg>') no-repeat center;
|
||||
padding:16px !important;
|
||||
}
|
||||
#wh-trans-icon .wh-container{display:none;}
|
||||
#wh-trans-icon.wh-icon-expanded .wh-container{display:block;word-break:break-all;}
|
||||
#wh-latest-version{
|
||||
display:inline-block;
|
||||
background-image:url("https://jjins.github.io/t2i/version.png?${performance.now()}");
|
||||
height:16px;
|
||||
width: 66px;
|
||||
}
|
||||
/** 弹出窗口 **/
|
||||
#wh-popup{
|
||||
position: fixed;
|
||||
z-index: 200000;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: #00000090;
|
||||
color:#333;
|
||||
}
|
||||
div#wh-popup::after {
|
||||
content: '点击空白处关闭';
|
||||
display: block;
|
||||
color: #ffffffdb;
|
||||
text-align: center;
|
||||
font-size: 14px;
|
||||
line-height: 22px;
|
||||
}
|
||||
#wh-popup-container{
|
||||
max-width: 568px;
|
||||
margin: 5em auto 0;
|
||||
background: #d7d7d7;
|
||||
min-height: 120px;
|
||||
box-shadow: 0 0 5px 1px #898989;
|
||||
border-radius: 4px;
|
||||
}
|
||||
#wh-popup-title p{
|
||||
padding: 1em 0;
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
text-align: center;
|
||||
}
|
||||
/** 弹出窗口的内容 **/
|
||||
#wh-popup-cont{
|
||||
padding: 0 1em 1em;
|
||||
max-height: 30em;
|
||||
overflow-y: auto;
|
||||
font-size:14px;
|
||||
line-height: 16px;
|
||||
}
|
||||
#wh-popup-cont .gSetting > div{
|
||||
display: inline-block;
|
||||
width: 47%;
|
||||
margin: 2px 0;
|
||||
}
|
||||
#wh-popup-cont .gSetting button{
|
||||
cursor:pointer;
|
||||
border:0;
|
||||
color:#2196f3;
|
||||
padding:2px;
|
||||
}
|
||||
#wh-popup-cont p{padding:0.25em 0;}
|
||||
#wh-popup-cont a{color:red;text-decoration:none;}
|
||||
#wh-popup-cont li{margin:4px 0;}
|
||||
#wh-popup-cont h4{margin:0;padding: 0.5em 0;}
|
||||
#wh-popup-cont button{
|
||||
margin: 0 4px 0 0;
|
||||
padding: 5px 8px;
|
||||
border: solid 2px black;
|
||||
color: black;
|
||||
border-radius: 3px;
|
||||
}
|
||||
#wh-popup-cont button[disabled]{opacity: 0.5;}
|
||||
#wh-popup-cont input{
|
||||
padding: 2px;
|
||||
text-align: center;
|
||||
border: 1px solid #fff0;
|
||||
border-radius: 5px;
|
||||
margin:1px 2px;
|
||||
}
|
||||
#wh-popup-cont input:focus{border-color:blue;}
|
||||
#wh-popup-cont table{width:100%;border-collapse:collapse;border:1px solid;}
|
||||
#wh-popup-cont td, #wh-popup-cont th{border-collapse:collapse;padding:4px;border:1px solid;}
|
||||
.wh-display-none{display:none !important;}
|
||||
#wh-gym-info-cont{
|
||||
background-color: #363636;
|
||||
color: white;
|
||||
padding: 8px;
|
||||
font-size: 15px;
|
||||
border-radius: 4px;
|
||||
text-shadow: 0 0 2px black;
|
||||
background-image: linear-gradient(90deg,transparent 50%,rgba(0,0,0,.07) 0);
|
||||
background-size: 4px;
|
||||
line-height: 20px;
|
||||
}
|
||||
#wh-gym-info-cont button{
|
||||
cursor:pointer;
|
||||
}
|
||||
`);
|
||||
|
||||
}
|
||||
@ -1,11 +1,15 @@
|
||||
import Device from "../enum/Device";
|
||||
import {BeerMonitorLoop} from "../func/utils/BuyBeer";
|
||||
|
||||
export default interface Global {
|
||||
isWindowActive?(): boolean;
|
||||
popup_node?: Element;
|
||||
beer?: BeerMonitorLoop;
|
||||
notifies?: NotifyWrapper;
|
||||
priceWatcher?: { status: boolean };
|
||||
fstock?: { get: () => Promise<any> };
|
||||
player_info?: PlayerInfo;
|
||||
device?: Device;
|
||||
device?: Device;
|
||||
isPDA?: boolean;
|
||||
PDA_APIKey?: string;
|
||||
isIframe?: boolean;
|
||||
|
||||
1433
src/userscript.ts
1433
src/userscript.ts
File diff suppressed because it is too large
Load Diff
1255
src/zhongIcon.ts
Normal file
1255
src/zhongIcon.ts
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user