TS重构
This commit is contained in:
parent
5534776e77
commit
28fac2d8c5
6
global.d.ts
vendored
6
global.d.ts
vendored
@ -1,5 +1,11 @@
|
||||
declare interface String {
|
||||
contains(keywords: RegExp | string): boolean;
|
||||
|
||||
/* 翻译 */
|
||||
// 时分秒转换
|
||||
replaceHMS(): string;
|
||||
// 数词转换 a an some
|
||||
numWordTrans(): string;
|
||||
}
|
||||
|
||||
declare interface Window {
|
||||
|
||||
21
src/func/translate/contentTitleLinksTrans.ts
Normal file
21
src/func/translate/contentTitleLinksTrans.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import {titleLinksDict} from "../../dictionary/translation";
|
||||
|
||||
// 页标题右侧按钮
|
||||
export default function contentTitleLinksTrans() {
|
||||
const $links_default = document.querySelectorAll('div.content-title span:nth-child(2)');
|
||||
const $links = $links_default.length === 0
|
||||
? document.querySelectorAll('div[class^="topSection"] span[class*="Title"]')
|
||||
: $links_default;
|
||||
$links.forEach(e => {
|
||||
if (titleLinksDict[e.innerText.trim()]) {
|
||||
e.innerText = titleLinksDict[e.innerText.trim()];
|
||||
} else if (e.id === 'events') {
|
||||
if (titleLinksDict[e.innerText.trim().split(' ')[0]])
|
||||
e.innerText = e.innerText.trim()
|
||||
.replace(
|
||||
e.innerText.trim().split(' ')[0],
|
||||
titleLinksDict[e.innerText.trim().split(' ')[0]]
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
8
src/func/translate/contentTitleLinksTransReact.ts
Normal file
8
src/func/translate/contentTitleLinksTransReact.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import {titleLinksDict} from "../../dictionary/translation";
|
||||
|
||||
export default function contentTitleLinksTransReact(dom = document.querySelectorAll('div[class^="linksContainer___"] span[class^="linkTitle___"]')) {
|
||||
dom.forEach(e => {
|
||||
const links_trans = titleLinksDict[e.innerText.trim()];
|
||||
if (links_trans) e.innerText = links_trans;
|
||||
});
|
||||
}
|
||||
32
src/func/translate/initOB.ts
Normal file
32
src/func/translate/initOB.ts
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* ob
|
||||
* @deprecated TODO 泛用模版有性能问题
|
||||
*/
|
||||
import log from "../utils/log";
|
||||
|
||||
export default function initOB(dom: Document | Element = document, opt = {}, func = () => {
|
||||
}, dev = false, once = false) {
|
||||
//let count = -1;
|
||||
if (dev) {
|
||||
const mo = new MutationObserver((mutation) => {
|
||||
//count++;
|
||||
log.info(mutation)
|
||||
mo.disconnect();
|
||||
func();
|
||||
if (!once) {
|
||||
mo.observe(dom, opt)
|
||||
}
|
||||
});
|
||||
func();
|
||||
mo.observe(dom, opt);
|
||||
} else {
|
||||
//count++;
|
||||
const mo = new MutationObserver(() => {
|
||||
mo.disconnect();
|
||||
func();
|
||||
if (!once) mo.observe(dom, opt)
|
||||
});
|
||||
func();
|
||||
mo.observe(dom, opt)
|
||||
}
|
||||
}
|
||||
39
src/func/translate/showItemInfoTrans.ts
Normal file
39
src/func/translate/showItemInfoTrans.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import {itemEffectDict, itemNameDict, itemPageDict, itemTypeDict} from "../../dictionary/translation";
|
||||
|
||||
// 展开物品详情
|
||||
export default function showItemInfoTrans(dom: HTMLElement = document.querySelector('.show-item-info')) {
|
||||
if (dom) {
|
||||
const $item_info = dom.querySelector('span.info-msg');
|
||||
if ($item_info) {
|
||||
// tt插件
|
||||
const is_tt_modified = !!$item_info.querySelector('.tt-modified');
|
||||
if (is_tt_modified) {
|
||||
console.warn(is_tt_modified)
|
||||
}
|
||||
// 物品名
|
||||
const $item_name = $item_info.querySelector('span.bold');
|
||||
// 去除物品名的the
|
||||
const the_removed = $item_name.innerText.trim().slice(4);
|
||||
// 物品的类别
|
||||
const $item_type = $item_name.nextSibling;
|
||||
// 绿字 物品效果
|
||||
const $item_effect = $item_info.querySelector('div.item-effect');
|
||||
if (itemNameDict[the_removed]) {
|
||||
$item_name.innerText = `${itemNameDict[the_removed]}(${the_removed})`;
|
||||
}
|
||||
if (itemTypeDict[$item_type.nodeValue.trim()]) {
|
||||
$item_type.nodeValue = itemTypeDict[$item_type.nodeValue.trim()];
|
||||
}
|
||||
if ($item_effect && itemEffectDict[$item_effect.innerText.trim()]) {
|
||||
$item_effect.innerText = itemEffectDict[$item_effect.innerText.trim()];
|
||||
}
|
||||
}
|
||||
// 下方的表格
|
||||
const $info_table_title = dom.querySelectorAll('div.title');
|
||||
$info_table_title.forEach((e) => {
|
||||
if (itemPageDict[e.innerText.trim()]) {
|
||||
e.innerText = itemPageDict[e.innerText.trim()];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
12
src/func/translate/titleTrans.ts
Normal file
12
src/func/translate/titleTrans.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import {cityDict, titleDict} from "../../dictionary/translation";
|
||||
|
||||
/**
|
||||
* 页标题翻译
|
||||
*/
|
||||
export default function titleTrans() {
|
||||
let node = $('h4#skip-to-content');
|
||||
const $title = node.length === 0 ? $('h4[class^="title"]') : node;
|
||||
const title = titleDict[$title.text().trim()] || cityDict[$title.text().trim()];
|
||||
if (title && $title.css('display') !== 'none')
|
||||
$title.after($title.clone().text(title)).css('display', 'none');
|
||||
}
|
||||
8
src/func/translate/titleTransReact.ts
Normal file
8
src/func/translate/titleTransReact.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import {titleDict} from "../../dictionary/translation";
|
||||
|
||||
export default function titleTransReact(dom = document.querySelectorAll('h4[class^="title___"]')) {
|
||||
dom.forEach(e => {
|
||||
const title_trans = titleDict[e.innerText.trim()];
|
||||
if (title_trans) e.innerText = title_trans;
|
||||
});
|
||||
}
|
||||
2096
src/func/translate/translateMain.ts
Normal file
2096
src/func/translate/translateMain.ts
Normal file
File diff suppressed because it is too large
Load Diff
10
src/init.ts
10
src/init.ts
@ -12,7 +12,8 @@ import WindowActiveState from "./func/utils/WindowActiveState";
|
||||
import addStyle from "./func/utils/addStyle";
|
||||
|
||||
// 初始化方法,获取必要全局参数
|
||||
export default function init(glob: Global) {
|
||||
export default function init(): Global {
|
||||
let glob: Global = {};
|
||||
glob.window = window;
|
||||
window.WHPARAMS = glob;
|
||||
let UWCopy = null;
|
||||
@ -40,7 +41,7 @@ export default function init(glob: Global) {
|
||||
Notification.requestPermission().then();
|
||||
}
|
||||
|
||||
// 扩展String正则方法
|
||||
// 扩展正则方法
|
||||
String.prototype.contains = function (keywords) {
|
||||
let that: string = this;
|
||||
if ('string' === typeof keywords) {
|
||||
@ -82,6 +83,8 @@ export default function init(glob: Global) {
|
||||
|
||||
// 抢啤酒
|
||||
glob.beer = BuyBeer();
|
||||
|
||||
// 当前的弹出窗口
|
||||
glob.popup_node = null;
|
||||
|
||||
// 当窗口关闭时关闭所有还存在的通知
|
||||
@ -100,6 +103,8 @@ export default function init(glob: Global) {
|
||||
// 记录当前窗口唯一id
|
||||
glob.isWindowActive = WindowActiveState();
|
||||
|
||||
glob.href = window.location.href;
|
||||
|
||||
addStyle(`
|
||||
.wh-hide{display:none;}
|
||||
#wh-trans-icon{
|
||||
@ -247,4 +252,5 @@ cursor:pointer;
|
||||
}
|
||||
`);
|
||||
|
||||
return glob;
|
||||
}
|
||||
@ -2,8 +2,12 @@ import Device from "../enum/Device";
|
||||
import {BeerMonitorLoop} from "../func/utils/BuyBeer";
|
||||
|
||||
export default interface Global {
|
||||
href?: string;
|
||||
$zhongNode?: MyHTMLElement;
|
||||
|
||||
isWindowActive?(): boolean;
|
||||
popup_node?: Element;
|
||||
|
||||
popup_node?: MyHTMLElement;
|
||||
beer?: BeerMonitorLoop;
|
||||
notifies?: NotifyWrapper;
|
||||
priceWatcher?: { status: boolean };
|
||||
@ -16,5 +20,5 @@ export default interface Global {
|
||||
version?: string;
|
||||
window?: Window;
|
||||
UWCopy?: Window & typeof globalThis;
|
||||
startTimestamp: number;
|
||||
// startTimestamp: number;
|
||||
}
|
||||
25
src/main.ts
25
src/main.ts
@ -1,8 +1,23 @@
|
||||
import userscript from "./userscript";
|
||||
import Global from "./interface/GlobalVars";
|
||||
import zhongIcon from "./zhongIcon";
|
||||
import init from "./init";
|
||||
import getWhSettingObj from "./func/utils/getWhSettingObj";
|
||||
import translateMain from "./func/translate/translateMain";
|
||||
|
||||
const glob: Global = {
|
||||
startTimestamp: -1,
|
||||
};
|
||||
(function main() {
|
||||
let started = new Date().getTime();
|
||||
|
||||
userscript(glob);
|
||||
if (document.title.toLowerCase().includes('just a moment')) return;
|
||||
|
||||
let glob = init();
|
||||
|
||||
zhongIcon(glob);
|
||||
|
||||
if (getWhSettingObj()['transEnable']) translateMain(glob.href);
|
||||
|
||||
userscript();
|
||||
|
||||
let runTime = new Date().getTime() - started;
|
||||
glob.$zhongNode.initTimer.innerHTML = `助手加载时间 ${runTime}ms`;
|
||||
})
|
||||
();
|
||||
2346
src/userscript.ts
2346
src/userscript.ts
File diff suppressed because it is too large
Load Diff
1364
src/zhongIcon.ts
1364
src/zhongIcon.ts
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user