我正在尝试从 twitch 子页面“游戏列表”(twitch.tv/目录)中删除各种游戏,但我一无所获。
我已经使用警报、计时器和@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) 加载您要查找的链接。这意味着该链接会在您的用户脚本运行完成后很长时间内显示 - 即使您使用 @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
有关更多信息和更多链接,请参阅链接的答案。
关于javascript - Greasemonkey 无法找到/修改/删除内容? (在 Twitch.tv 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34355987/