通过 Greasemonkey 删除特定网站内容

Specific Website content removal via Greasemonkey

我知道类似的问题已被问过无数次,我已经阅读了一段时间,但我似乎无法让它为我的案例工作,我已经阅读了以下内容和尝试将这些解决方案应用于我的案例,全部来自 Stack Overflow,但我没有成功,所以我很感激你的帮助。

Article 1 Article 3 Article 4 Article 5

我想从 animenewsnetwork.com 站点的边栏中删除 "This Week in Anime" 篇文章,我想删除的代码示例是:

<div class="herald box column" data-topics="article141022 column anime">
  <div class="category-line column"></div>
  <div class="thumbnail" data-src="/thumbnails/cover400x200/cms/this-week-in-anime/141022/g24.png.jpg">
    <div class="overlay">
      <div class="category column">column</div>
      <div class="comments"> <a href="/cms/discuss/141022">51 comments</a> </div>
    </div>
    <a href="/this-week-in-anime/2018-12-20/.141022" data-track="id=66149&amp;from=HP.MF"></a>
  </div>
  <div class="wrap">
    <div>
      <h3>
        <a href="/this-week-in-anime/2018-12-20/.141022" data-track="id=66149&amp;from=HP.MF">This Week in Anime - Is Back Street Girls: Gokudols Worth Watching?</a>
      </h3>
      <div class="byline">
        <time datetime="2018-12-20T19:31:04Z" title="2018-Dec-20 11:31:04 -0800 (1.4 day ago)">Dec 20, 14:31</time>
        <div class="comments"> <a href="/cms/discuss/141022">51 comments</a> </div>
        <span class="topics"> <span class="anime">anime</span> </span>
      </div>
      <div class="preview">
        <span class="intro">Netflix's newest anime acquisition is controversial to say the least. Andy and Steve find out (the hard way) if there's any decent comedy hiding under this show's initial shock factor.
        </span>
        <span class="full">Netflix's newest acquisition, Back Street Girls: Gokudols, is controversial to say the least. This week, Andy and Steve find out if there's any decent comedy hiding under this show's initial shock factor. Disclaimer...
        </span>
      </div>
    </div>
  </div>
</div>

我想完全删除它,我试过了,我认为是在搜索字符串 "this-week-in-anime" 然后删除或隐藏它的 parent ,但我从来没有让它工作。

我试过这些:

var badDivs = $("div div:contains('this-week-in-anime')");
badDivs.hide ();

var badDivs = $("div div:contains(this-week-in-anime)");
badDivs.parent ().hide ();

$('li:contains("this-week-in-anime")').parent().hide();

编辑:脚本的最终形式和其他地方收到的建议。

 ==UserScript==
// @name     _Anime News Network, remove "This Week in Anime" articles
// @match    *://www.animenewsnetwork.com/*
// @grant    none
// @run-at   document-idle
// ==/UserScript==

(function () {
    'use strict';
    if (!Element.prototype.matches) {
        Element.prototype.matches =
            Element.prototype.matchesSelector ||
            Element.prototype.mozMatchesSelector ||
            Element.prototype.msMatchesSelector ||
            Element.prototype.oMatchesSelector ||
            Element.prototype.webkitMatchesSelector;
    }

    var getParent = function (node, selector) {
        while (node && node.parentNode) {
            node = node.parentNode;
            if (node.matches(selector)) {
                return node;
            }
        }
    };

    document.querySelectorAll(".thumbnail > a[href*='this-week-in-anime']").forEach(function (v) {
        var node = getParent(v, '.herald.box');
        if (node) {
            node.remove();
        }
    });
})();

Some things to consider:

Encapsulate your stuff in a function. This prevents things you declare to be accessible from the page itself.

"use strict"; as the first line in a function fixes some JS annoyance you might not catch immediately. For example a typo in a variable name.

Try to avoid using @require. This can block page loads for a long time if that resource has trouble being delivered

If you want to remove elements, @run-at document-idle is a good start because it makes sure the "load" event has fired

If the element you want to remove is dynamically loaded at a later time, use a setInterval or better MutationObserver

Try to avoid jQuery. Most of it's features are available in regular JS by now.

In my example I completely remove the node. If you don't want that, you can use node.style.display='none'

实际上,that first article you linked 拥有您需要的技术。由于您的目标页面使用 javascript 添加了令人反感的内容,因此您需要使用类似 waitForKeyElements().

的内容

请注意 :contains 搜索文本,但有问题的 "this-week-in-anime" 在各种节点属性中。因此,我们在他们的 href.

中寻找与它的链接

这是 your target website 的配置:

// ==UserScript==
// @name     _Anime News Network, remove "This Week in Anime" articles
// @match    *://www.animenewsnetwork.com/*
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    none
// ==/UserScript==
//-- WARNING: Using the page's copy of jQuery.  This may blow up at some point.
/* global $, waitForKeyElements */

waitForKeyElements (".thumbnail > a[href*='this-week-in-anime']", hideContainerNode);

function hideContainerNode (jNode) {
    //-- jQuery closest() finds an ancestor of current node.
    var containerNode = jNode.closest (".herald.box");
    containerNode.hide ();
}