// ==UserScript== // @name 电影资源网去广告 // @description 电影资源网去广告,支持多个类似资源网站,仅测试:电影港、电影天堂 // @version 0.0.3 // @author FanZhenHua // @match *://*/* // @grant GM_addStyle // @license MIT // ==/UserScript== /** * 通用的 MutationObserver 函数,用于在元素出现时应用样式 * @param {string} selector - 要匹配的元素选择器 */ function observeAndApplyStyle(selector) { // 创建一个MutationObserver实例,传入回调函数 const observer = new MutationObserver((mutationsList) => { // 遍历所有DOM变化 for (const mutation of mutationsList) { // 只处理子节点变化类型 if (mutation.type === "childList") { // 查找所有匹配选择器的元素 const elements = document.querySelectorAll(selector); elements.forEach((element) => { // 检查元素是否包含IMG子元素 const hasImgChild = Array.from(element.children).some((child) => child.tagName === "IMG"); if (hasImgChild) { // 使用GM_addStyle添加CSS,隐藏匹配选择器的元素 GM_addStyle(`${selector} { display: none !important }`); } }); } } }); // 配置观察选项:监听子节点变化和深层子树 const config = { childList: true, subtree: true }; // 开始观察document.body observer.observe(document.body, config); } /** * 观察包含特定href的a标签,并隐藏其父级div * @param {string} hrefValue - a标签href中需要包含的值 */ function observeAnchorWithHref(hrefValue) { const observer = new MutationObserver((mutationsList) => { for (const mutation of mutationsList) { if (mutation.type === "childList") { // 查找所有包含特定href的a标签 const anchors = document.querySelectorAll(`a[href*="${hrefValue}"]`); anchors.forEach(anchor => { // 获取父级元素 const parent = anchor.parentElement; // 检查父级是否为div if (parent && parent.tagName === "DIV") { // 使用唯一ID避免重复添加相同的CSS规则 const styleId = `block-div-${hrefValue.replace(/[^a-z0-9]/gi, '_')}`; // 检查样式是否已添加 if (!document.getElementById(styleId)) { const style = document.createElement('style'); style.id = styleId; style.textContent = `#${parent.id} { display: none !important }`; document.head.appendChild(style); } else { // 如果样式已存在,直接应用 parent.style.display = 'none'; } } }); } } }); const config = { childList: true, subtree: true }; observer.observe(document.body, config); } function main() { // let weburl = window.location.href; // if (weburl.indexOf("www.dygangs.net") != -1) { observeAndApplyStyle(".jjjjasdasd"); // 左右两侧 observeAndApplyStyle("divz"); // 右下角弹窗1 // 新增:观察包含evewan.com链接的a标签并隐藏其父级div observeAnchorWithHref("evewan.com"); // } } main();