我试图将各种游戏从抽搐子页“游戏列表”(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);为什么脚本不删除这些节点?
发布于 2015-12-15 19:48:05
该站点使用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
有关更多信息和更多链接,请参见链接答案。
https://stackoverflow.com/questions/34297465
复制相似问题