Greasemonkey is unable to find/modify/delete content? (on Twitch.tv)

≡放荡痞女 提交于 2019-12-20 05:31:44

问题


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?


回答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.



来源:https://stackoverflow.com/questions/34297465/greasemonkey-is-unable-to-find-modify-delete-content-on-twitch-tv

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