import DOMUtils from "@whitesev/domutils"; import pops from "@whitesev/pops"; import Utils from "@whitesev/utils"; import { GM_getValue, GM_info, GM_registerMenuCommand, GM_setValue, GM_unregisterMenuCommand, GM_xmlhttpRequest, monkeyWindow, unsafeWindow, } from "ViteGM"; import Qmsg from "qmsg"; import type { QmsgPosition } from "qmsg/dist/src/types/config.js"; import { createApp } from "vue"; import { GM_RESOURCE_MAPPING } from "./GM_Resource_Mapping"; import { Panel } from "./setting/panel"; import { PanelSettingConfig } from "./setting/panel-setting-config"; import { CommonUtil } from "./utils/CommonUtil"; const utils = Utils.noConflict(); const domUtils = DOMUtils.noConflict(); const __pops__ = pops; // const showdown: typeof import("@lib/showdown") = // (monkeyWindow as any).showdown || (unsafeWindow as any).showdown; const log = new utils.Log(GM_info, unsafeWindow.console || monkeyWindow.console); /** * 脚本名 */ const SCRIPT_NAME = GM_info?.script?.name || import.meta.env.SCRIPT_NAME; /** * 手势库 */ const AnyTouch = pops.fn.Utils.AnyTouch(); /** * 是否为调试模式 * * 当`dev`时,自动开启调试模式 */ const DEBUG = import.meta.env.DEV ?? false; // 配置控制台日志 log.config({ debug: false, logMaxCount: DEBUG ? 10000 : 250, autoClearConsole: true, tag: true, }); const getPageMaxZIndex = () => { const deviation = 100; const popsZIndex = pops.fn.InstanceUtils.getPopsMaxZIndex()?.zIndex ?? 0; const pointZIndex = utils.getMaxZIndexNodeInfoFromPoint()[0]?.zIndex ?? 0; const maxZIndex = Math.max(deviation, popsZIndex, pointZIndex); return maxZIndex; }; // 配置Toast Qmsg.config({ isHTML: true, autoClose: true, showClose: false, consoleLogContent(qmsgInst) { const qmsgType = qmsgInst.setting.type; if (qmsgType === "loading") { return false; } const content = qmsgInst.setting.content; if (qmsgType === "warning") { log.warn(content); } else if (qmsgType === "error") { log.error(content); } else { log.info(content); } return false; }, get position() { return Panel.getValue( PanelSettingConfig.qmsg_config_position.key, PanelSettingConfig.qmsg_config_position.defaultValue ); }, get maxNums() { return Panel.getValue( PanelSettingConfig.qmsg_config_maxnums.key, PanelSettingConfig.qmsg_config_maxnums.defaultValue ); }, get showReverse() { return Panel.getValue( PanelSettingConfig.qmsg_config_showreverse.key, PanelSettingConfig.qmsg_config_showreverse.defaultValue ); }, get zIndex() { return getPageMaxZIndex(); }, }); /* 配置pops的默认选项 */ __pops__.GlobalConfig.setGlobalConfig({ zIndex: () => { return getPageMaxZIndex(); }, mask: { // 开启遮罩层 enable: true, // 取消点击遮罩层的事件 clickEvent: { toClose: false, toHide: false, }, }, drag: true, }); /** * 油猴菜单注册器 */ const MenuRegister = new utils.GM_Menu({ GM_getValue, GM_setValue, GM_registerMenuCommand, GM_unregisterMenuCommand, }); const httpx = new utils.Httpx({ xmlHttpRequest: GM_xmlhttpRequest, logDetails: false, }); // 添加请求拦截器 httpx.interceptors.request.use((data) => { if (DEBUG) { log.info("[Httpx-HttpxRequest.request] 请求前的配置", { data }); } return data; }); // 添加响应拦截器 httpx.interceptors.response.use( (response) => { if (DEBUG) { log.info("[Httpx-HttpxRequest.response] 响应结果", { response }); } return response; }, (data) => { log.error("[Httpx-HttpxRequest.response] 响应错误", { data }); if (data.type === "onabort") { Qmsg.warning("请求取消", { consoleLogContent: true }); } else if (data.type === "onerror") { Qmsg.error("请求异常", { consoleLogContent: true }); } else if (data.type === "ontimeout") { Qmsg.error("请求超时", { consoleLogContent: true }); } else { Qmsg.error("其它错误", { consoleLogContent: true }); } return data; } ); const OriginPrototype = { Object: { defineProperty: unsafeWindow.Object.defineProperty, keys: unsafeWindow.Object.keys, values: unsafeWindow.Object.values, }, Function: { apply: unsafeWindow.Function.prototype.apply, call: unsafeWindow.Function.prototype.call, }, Element: { appendChild: unsafeWindow.Element.prototype.appendChild, }, setTimeout: unsafeWindow.setTimeout.bind(unsafeWindow), clearTimeout: unsafeWindow.clearTimeout.bind(unsafeWindow), setInterval: unsafeWindow.setInterval.bind(unsafeWindow), clearInterval: unsafeWindow.clearInterval.bind(unsafeWindow), }; /** * 添加样式 */ const addStyle = domUtils.addStyle.bind(domUtils); /** * 添加屏蔽CSS */ const addBlockCSS = CommonUtil.addBlockCSS.bind(CommonUtil); /** * 元素选择器 */ const $ = DOMUtils.selector.bind(DOMUtils); /** * 多组元素选择器 */ const $$ = DOMUtils.selectorAll.bind(DOMUtils); /** * Vue的根元素的id */ const VUE_ROOT_ID = "vite-app"; /** * 注册vue、element-plus、element-plus/icons-vue * @param rootComponent vue实例 * @param plugins 插件之类的,如vue-router、Element-Plus */ const MountVue = async function (rootComponent: any, plugins: any[] = []) { CommonUtil.setGMResourceCSS(GM_RESOURCE_MAPPING.ElementPlus); await DOMUtils.onReady(); const app = createApp(rootComponent); const $mount = DOMUtils.createElement("div", { id: VUE_ROOT_ID, }); /* 注册图标组件 */ if (import.meta.env.DEV) { const ElementPlusIconsVue = await import("@element-plus/icons-vue"); for (const [key, component] of Object.entries(ElementPlusIconsVue)) { app.component(key, component); } } else { /* ElementPlusIconsVue是var定义的,不在window上 */ // @ts-ignore if (ElementPlusIconsVue != null) { // @ts-ignore for (const [key, component] of Object.entries(ElementPlusIconsVue)) { // @ts-ignore app.component(key, component); } } } document.body.appendChild($mount); plugins.forEach((item) => { app.use(item); }); const mountResult = app.mount($mount); return mountResult; }; /** * cookie管理 - GM_cookie */ const cookieManager = new utils.CookieManagerService({ baseCookieHandler: "GM_cookie", }); if (!cookieManager.isSupportGM_cookie) { // 不支持GM_cookie if (cookieManager.isSupportCookieStore) { // 回退至cookieStore cookieManager.setOptions({ baseCookieHandler: "cookieStore", }); } else { // 回退至document.cookie cookieManager.setOptions({ baseCookieHandler: "document.cookie", }); } } /** * cookie管理 - document.cookie */ const documentCookieManager = new utils.DocumentCookieHandler(); // dev使用 if (import.meta.env.DEV) { const registerObject = { httpx, cookieManager }; Object.keys(registerObject).forEach((key) => { Reflect.set(unsafeWindow, key, registerObject[key as keyof typeof registerObject]); }); } export { $, $$, addStyle, AnyTouch, cookieManager, documentCookieManager, DEBUG, domUtils as DOMUtils, httpx, log, MenuRegister, MountVue, OriginPrototype, __pops__ as pops, SCRIPT_NAME, utils, VUE_ROOT_ID, getPageMaxZIndex, addBlockCSS, };