import DOMUtils from "@whitesev/domutils"; import { GM_getResourceText, unsafeWindow } from "ViteGM"; import { addStyle, log, utils } from "../env.base"; const CommonUtil = { /** * 移除元素(未出现也可以等待出现) * @param selector 元素选择器 */ waitRemove(...args: string[]) { args.forEach((selector) => { if (typeof selector !== "string") { return; } DOMUtils.waitNodeList>(selector).then((nodeList) => { nodeList.forEach(($el) => $el.remove()); }); }); }, /** * 生成屏蔽CSS元素 * @param args * @example * addBlockCSS("") * addBlockCSS("","") * addBlockCSS(["",""]) */ createBlockCSSNode(...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 DOMUtils.createElement("style", { type: "text/css", innerHTML: `${selectorList.join(",\n")}{display: none !important;}`, }); }, /** * 添加屏蔽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); } }); selectorList = selectorList.map(it=> it.trim()).filter((it) => it !== ""); if(selectorList.length){ 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; }) { const cssText = typeof GM_getResourceText === "function" ? GM_getResourceText(resourceMapData.keyName) : null; if (typeof cssText === "string" && cssText) { return addStyle(cssText); } else { return 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; return new Promise((resolve) => { DOMUtils.onReady(() => { document.head.appendChild($link); resolve($link); }); }); }, /** * 添加