How to use Greasemonkey to selectively remove content from a website?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 09:24:16

问题


I have tried removing content (paid content) from this website with uBlock origin, Greasemonkey and Anti-Adblock Killer script.

I have tried running this script but without success.
The ("paid") content I want to remove looks like this:

<div class="news" info="398825">
    <div class="normal" ...>
        <div class="supertitle">
            <a href="http://www.monitor.hr/marketing/sponzorirana.html" target="_blank">Sponzorirana vijest</a>
        </div>
        ...

I can differentiate "paid content" from rest of the content with this element:

<a href="http://www.monitor.hr/marketing/sponzorirana.html" target="_blank">Sponzorirana vijest</a>

I would like to remove every "paid content" ("Sponzorirana vijest") section from the linked website.


回答1:


That content appears to be static. So just leverage the page's jQuery like so:

// ==UserScript==
// @name     _Remove sponsored content
// @match    *://www.monitor.hr/*
// @grant    none
// ==/UserScript==

$(".supertitle > a[href*='marketing/sponzorirana']").closest (".news").remove ();


If more of those blocks are added dynamically, use waitForKeyElements() as shown in this answer. Something like this (untested in GM4):

// ==UserScript==
// @name     _Remove sponsored content
// @match    *://www.monitor.hr/*
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    none
// ==/UserScript==
waitForKeyElements (".supertitle > a[href*='marketing/sponzorirana']", removeNewsNode);

function removeNewsNode (jNode) {
    jNode.closest (".news").remove ();
}



Finally, per Greasemonkey's own developers, switch to Tampermonkey or Violentmonkey. Greasemonkey 4+ has serious deficiencies.




回答2:


And here is the script that works, just paste it into tampermonkey or greasemonkey.

// ==UserScript==
// @name         Disable for sponsored news
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://www.monitor.hr/
// @grant        none
// ==/UserScript==

(function() {
    $("a:contains('Sponzorirana vijest')").closest('.news').css('display', 'none');
})();

Script explained. @match shows on which website to apply it. Function parts is jquery that selects node of class news that is closest to the anchor tag that contains string Sponzorirana vijest, when selected display: none is applied.




回答3:


I've created this custom filter in uBlock and it works like a charm:

www.monitor.hr##.tag-sponzorirana-vijest.category-vijest


来源:https://stackoverflow.com/questions/48462714/how-to-use-greasemonkey-to-selectively-remove-content-from-a-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!