1

I know similar questions have been asked countless times, I've been reading through them for a while now, but I can't seem to get it to work for my case, I've read through the following and tried to apply those solutions to my case, all from Stack Overflow, but I have not had success, so I'd appreciate the help.

Article 1 Article 2 Article 3 Article 4 Article 5

I would like to remove "This Week in Anime" articles from the sidebar on animenewsnetwork.com sites, an example of the code I would like to remove is:

<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>

I would like to remove this entirely, I tried, what I think was searching for the string "this-week-in-anime" and then removing or hiding it's parent , but I never got it to work.

I tried things like these:

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

and

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

and

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

Edit: Script's final form and advice received elsewhere.

 ==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'

Edwin
  • 21
  • 4

1 Answers1

0

Actually, that first article you linked has the technique you need. Since your target page adds the offensive content using javascript, you need to use something like waitForKeyElements().

Note that :contains searches for text, but the offending "this-week-in-anime" is in various node attributes. So, we look for links with it in their href.

Here's the configuration for 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 ();
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks, it didn't quite work for me as is, but it gave me a much better base that with help on another site lead me to it's final form, I'll update the question with the end result, as well as some tips that I was given about it. – Edwin Dec 22 '18 at 12:44