Greasemonkey 无法find/modify/delete内容? (在 Twitch.tv 上)

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

我正在尝试从 twitch 子页面 "Game List"(twitch。tv/directory)中删除各种游戏,但我一无所获。

我已经使用警报、计时器和 @run-at document-end 进行了调试,但都无济于事,脚本可以正确到达页面,但是一旦我尝试操作内容,就什么也没有发生。

这是我用来测试的:

// ==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);

为什么脚本不删除这些节点?

该站点使用 javascript (AJAX) 来加载您要查找的 link。这意味着 link 在您的用户脚本完成 运行 之后很久就会显示 - 即使您使用 @run-at document-end.

要解决此问题,请使用 AJAX-aware techniques,例如 waitForKeyElements()

这里是一个完整的脚本,展示了如何使用 jQuery + waitForKeyElements 方式:

// ==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 ();
}

在此页面上测试:http://www.twitch.tv/directory

查看 linked 答案了解更多信息和更多 links。