import { addStyle, DOMUtils, utils } from "@/env"; import { GM_getResourceText } from "ViteGM"; export const CommonUtil = { /** * 移除元素(未出现也可以等待出现) * @param selector 元素选择器 */ waitRemove(...args: string[]) { args.forEach((selector) => { DOMUtils.waitNodeList>(selector).then((nodeList) => { nodeList.forEach((item) => item.remove()); }); }); }, /** * 添加屏蔽CSS * @param args * @example * addBlockCSS("") * addBlockCSS("","") * addBlockCSS(["",""]) */ addBlockCSS(...args: (string | string[])[]) { let selectorList: string[] = []; if (args.length === 0) { return; } if (args.length === 1 && typeof args[0] === "string" && args[0].trim() === "") { return; } args.forEach((selector) => { if (Array.isArray(selector)) { selectorList = selectorList.concat(selector); } else { selectorList.push(selector); } }); return addStyle(`${selectorList.join(",\n")}{display: none !important;}`); }, /** * 设置GM_getResourceText的style内容 * @param resourceMapData 资源数据 * @example * setGMResourceCSS({ * keyName: "ViewerCSS", * url: "https://example.com/example.css", * }) */ setGMResourceCSS(resourceMapData: { /** 使用@resource定义的名字 */ keyName: string; /** 使用@resource引用的地址 */ url: string; }) { let cssText = typeof GM_getResourceText === "function" ? GM_getResourceText(resourceMapData.keyName) : ""; if (typeof cssText === "string" && cssText) { addStyle(cssText); } else { CommonUtil.loadStyleLink(resourceMapData.url); } }, /** * 添加标签 * @param url * @example * loadStyleLink("https://example.com/example.css") */ async loadStyleLink(url: string) { let $link = document.createElement("link"); $link.rel = "stylesheet"; $link.type = "text/css"; $link.href = url; DOMUtils.onReady(() => { document.head.appendChild($link); }); }, /** * 添加