67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import './global'
|
|
import log from "./function/utils/log";
|
|
import getWhSettingObj from "./function/utils/getWhSettingObj";
|
|
import miniprofTrans from "./function/translate/miniprofTrans";
|
|
|
|
export default function init() {
|
|
const UWCopy: Window & typeof globalThis = window["unsafeWindow"];
|
|
try {
|
|
window = UWCopy || window;
|
|
} catch {
|
|
}
|
|
// 防止脚本重复运行
|
|
if (window["WHTRANS"]) throw new DOMException('已在运行');
|
|
window["WHTRANS"] = true;
|
|
|
|
// 脚本版本
|
|
const version = '$$WUHU_DEV_VERSION$$';
|
|
|
|
// iframe运行
|
|
const isIframe = self !== top;
|
|
const $ = window['jQuery'];
|
|
// PDA
|
|
const PDA_APIKey = '###PDA-APIKEY###';
|
|
const isPDA = PDA_APIKey.slice(-1) !== '#';
|
|
|
|
// 通知权限
|
|
if (window.Notification) {
|
|
Notification.requestPermission().then(status => {
|
|
// 这将使我们能在 Chrome/Safari 中使用 Notification.permission
|
|
if (Notification.permission !== status) {
|
|
// @ts-ignore
|
|
Notification['permission'] = status;
|
|
}
|
|
})
|
|
}
|
|
|
|
// regexp test
|
|
String.prototype.contains = function (keywords: RegExp) {
|
|
let that: String = this;
|
|
if ('string' === typeof keywords) {
|
|
return new RegExp(keywords).test(that);
|
|
}
|
|
if (keywords.test) {
|
|
return keywords.test(that);
|
|
}
|
|
};
|
|
|
|
// region 监听fetch
|
|
const ori_fetch = window.fetch;
|
|
window.fetch = async (url, init) => {
|
|
if (url.contains('newsTickers')) {
|
|
// 阻止获取新闻横幅
|
|
return new Response('{}');
|
|
}
|
|
const res = await ori_fetch(url, init);
|
|
// mini profile 翻译
|
|
if (url.includes('profiles.php?step=getUserNameContextMenu') && getWhSettingObj()['transEnable']) {
|
|
setTimeout(() => miniprofTrans(), 200);
|
|
}
|
|
let clone = res.clone();
|
|
let text = await res.text();
|
|
log({url, init, text});
|
|
return clone;
|
|
};
|
|
// endregion
|
|
return {version, isIframe, $, PDA_APIKey, isPDA};
|
|
} |