Greasemonkey无法找到/修改/删除内容? (在Twitch.tv上)

时间:2015-12-15 19:11:10

标签: javascript greasemonkey removechild tampermonkey twitch

我正试图从抽搐子页面“游戏列表”(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);

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

1 个答案:

答案 0 :(得分:1)

该网站使用javascript(AJAX)加载您要查找的链接。这意味着,即使您使用@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

请参阅链接的答案以获取更多信息和更多链接。