You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Greasemonkey选择性移除网站指定付费内容?

嘿,我来帮你搞定这个问题!你想移除那些带“Sponzorirana vijest”标识的付费内容板块对吧?之前用脚本没成功,那咱们换个更靠谱的方式试试,分两种方案给你选:

方法1:用uBlock Origin直接拦截(最简便)

uBlock Origin本身就支持通过CSS选择器精准隐藏元素,完全不用写复杂脚本,几步就能搞定:

  • 点击浏览器右上角的uBlock Origin图标,再点齿轮图标打开设置面板
  • 切换到「我的规则」标签页
  • 在「临时规则」的输入框里添加这条规则:
    monitor.hr##div.news:has(a[href*="marketing/sponzorirana.html"])
    
  • 点击「保存」,刷新目标网站后,那些带“Sponzorirana vijest”的板块就会被直接隐藏了。

简单解释下:div.news是你提到的内容外层容器,:has()选择器专门用来筛选内部包含指定链接的容器,刚好匹配你给出的付费内容结构,精准定位不会误删其他正常内容。

方法2:用Greasemonkey脚本实现

如果还是想用用户脚本的方式,那咱们写个更稳定的版本——之前的脚本大概率是没处理好元素的动态加载时机,或者选择器不够精准:
创建一个新的Greasemonkey脚本,把下面的代码粘贴进去:

// ==UserScript==
// @name         Remove Sponsored Content on Monitor.hr
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  移除所有"Sponzorirana vijest"付费内容板块
// @author       自定义名称
// @match        *://www.monitor.hr/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 定义移除付费内容的核心函数
    function removeSponsoredContent() {
        // 找到所有包含目标链接的.news容器
        const sponsoredSections = document.querySelectorAll('div.news:has(a[href*="marketing/sponzorirana.html"])');
        sponsoredSections.forEach(section => {
            section.remove();
        });
    }

    // 页面初始加载完成后执行一次
    window.addEventListener('load', removeSponsoredContent);

    // 监听页面动态变化(对付AJAX加载的内容)
    const observer = new MutationObserver(() => {
        removeSponsoredContent();
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();

这个脚本做了两件关键的事:

  1. 页面加载完成后立刻扫描并移除目标内容
  2. 用MutationObserver监听页面的动态变化,防止那些通过AJAX异步加载的付费内容漏网

如果你的浏览器不支持:has()选择器(比如旧版Firefox),可以把核心函数换成更兼容的写法:

function removeSponsoredContent() {
    const sponsoredSections = document.querySelectorAll('div.news');
    sponsoredSections.forEach(section => {
        const targetLink = section.querySelector('a[href*="marketing/sponzorirana.html"]');
        // 同时检查链接文本,确保精准匹配
        if (targetLink && targetLink.textContent.trim() === 'Sponzorirana vijest') {
            section.remove();
        }
    });
}

这样通过同时检查链接的URL和文本内容来确认,兼容性更好,也能避免误删。

内容的提问来源于stack exchange,提问作者Cikson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:28:04