1

I'm trying to remove various games off the twitch sub-page "Game List" (twitch.tv/directory) but I'm getting nowhere.

I've debugged with alert, timers and @run-at document-end to no avail, the script reaches the page correctly but once I try to manipulate content nothing happens.

This is what I use to test it:

// ==UserScript==
// @name        TwitchDeleteTest
// @namespace   to.be.continued
// @include     http*://*twitch.tv/directory*
// @version     1
// @grant       none
// ==/UserScript==

var rmLoL = document.querySelector("a[title='League of Legends']");
var grandParent = rmLoL.parentNode.parentNode;
grandParent.parentNode.removeChild(grandParent);

Why isn't the script removing those nodes?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
SmitHan
  • 25
  • 5
  • Just to clarify, what question are you asking? – SteveLacy Dec 15 '15 at 19:25
  • ive edited the post, hopefully it's more clear now – SmitHan Dec 15 '15 at 19:32
  • 2
    Twitch probably adds the content dynamically: [Make Greasemonkey react to ajax change of an element](http://stackoverflow.com/a/16169221) – wOxxOm Dec 15 '15 at 19:35
  • I'm gonna check that next, I'm just wondering if the querySelector will run right like that, I mean will it treat the spaces right or will I have to use special formating for them ? (i believe its \20 or something) – SmitHan Dec 15 '15 at 19:46

1 Answers1

1

That site uses javascript (AJAX) to load the link you are looking for. This means that the link shows up long after your userscript finishes running -- even if you use @run-at document-end.

To get around this use AJAX-aware techniques such as with waitForKeyElements().

Here's a complete script showing how to do this the jQuery + waitForKeyElements way:

// ==UserScript==
// @name     Twitch Delete Test
// @match    *://*.twitch.tv/directory*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements (
    ".game.item a[title='League of Legends']", deleteContainingNode
);

function deleteContainingNode (jNode) {
    jNode.parent ().parent ().remove ();
}

Tested on this page: http://www.twitch.tv/directory

See the linked answer for more information and more links.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks a crapton, I just wished I could do this without getting jquery/ajax involved but I'll live- one thing though, the div that held the game is still there eventhough it's empty, is there a way for me to remove this to ? – SmitHan Dec 15 '15 at 20:04
  • 1
    Okay, try `jNode.parent ().parent ().remove ();`. Also, jQuery in a Greasemonkey script is a ***good thing*** when it's `@require`d. It will save you a ton of grief and effort. – Brock Adams Dec 15 '15 at 20:07
  • that did it! yea in general I just prefer building things ground-up and not having to rely on outside resources because that usually means I get to learn more and worry less about things that's outside of my control - but blablabla on me, this works so thanks ! – SmitHan Dec 15 '15 at 20:17
  • well, i'm still back to the drawing-board as this route doesn't work for the "streamer list" - the "items" are only removed once I hoover the mouse on them, any ideas what to do here ? (url: twitch.tv/directory/all ) – SmitHan Dec 15 '15 at 23:47
  • Open a new question for that and include a relevant snippet of the HTML, plus what you have tried. You probably just need to tune the selector, see http://stackoverflow.com/questions/15048223/choosing-and-activating-the-right-controls-on-an-ajax-driven-site – Brock Adams Dec 15 '15 at 23:49
  • Try changing `.game.item` to `.item`. – Brock Adams Dec 15 '15 at 23:52
  • I fixed it! - so basically Twitch renames these tags when you hover or click to inspect them - I managed to find the right name but now ahahaha x_x the scroller breaks because there's not enough entries to activate it. I.. need a break, but thanks for the help again! and sorry? for deleting these comments - edit vanishes after a time and i want to avoid doubleposting.. – SmitHan Dec 16 '15 at 01:13