在AJAX驱动的页面上,基于特定href删除节点时出现问题



这应该很简单,但我所尝试的一切似乎都不起作用。也许我太累了,所以我想换一双新眼睛。请帮忙
在Greasemonkey中:查看页面中的<a href="#?#/3/">链接。如果它在那里,请将其父对象从视图中移除。

我尝试过的东西(几乎都可以在另一个堆栈页面上找到)

1

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
$("a[href='#?#/3']").parent().parent().parent().parent().parent().remove();

2

$("a").each(function() {
    if (this.href.indexOf('#?#/3/') != -1) {
        this.parent().parent().parent().parent().parent().parent().remove();
    }
/});

3

$('a').each(function () {
  if ($(this) == 'url("#?#/3/")' {
    $(this).parent().parent().parent().parent().parent().remove();
  }
});

4

var targNode = document.querySelector ("BODY>DIV:nth-of-type(2)>DIV>DIV:nth-of-type(3)>DIV:nth-of-type(2)>DIV:nth-of-type(3)");
var targNodeCheck = targNode.contains("#?#/3");
var targNodeFull = targNode.parent().parent().parent().parent().parent(); 
if (targNodeCheck === true){
  targNodefull.style.display = "none";
}

编辑

我以前没有想过,但确实需要等待页面加载。(大约3秒,有一个jQuery加载轮)我不相信Greasemonkey扩展有问题?

这就是网站的基本结构。有200多个初始div类需要解析不同的URL。

<BODY>
    <DIV CLASS="one">
        <DIV CLASS="HOLDER">
            <DIV CLASS="A one">
                <DIV CLASS="IMAGES">
                    <DIV CLASS="LINKHOLDER">
                        <A HREF="#?#/13121/">Link</a>
                        <A HREF="#?#/21231/">Link</a>
                        <A HREF="#?#/3/">Link</a>
                        <A HREF="#?#/41551/">Link</a>
                        <A HREF="#?#/54600/">Link</a>
                        <A HREF="#?#/61650/">Link</a>
                        <A HREF="#?#/72613/">Link</a>
                        <A HREF="#?#/83454/">Link</a>
                        </DIV>

如果您看到jQuery loading wheel,那么页面肯定是由脚本生成的,这意味着当用户脚本默认在DOMContentLoaded/load事件中执行时(大约与$(document).ready()$(function() { ... })同时)

正确的解决方案是定期检查(setTimeout/setInterval)元素是否出现,或者使用MutationObserver。

使用setMutationHandler包装器的示例:

// @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
setMutationHandler(document, "a[href='#?#/3']", function(nodes) {
    nodes.forEach(function(n) {
        $(n).parents().get(4).remove();
    });
    return true; // continue enumeration of current Mutations batch
});

我认为您只需要使用*

$("a[href*='#?#/3']").remove();

我真的不知道parent(.parent()是什么意思……但如果你需要用class 1删除div,你可以使用

$("a[href*='#?#/3']").closest('.one').remove(); // but this code will remove all the div with links inside

你可以在中使用你的代码

$(document).ready(function(){
    // your code here
});

你可能需要阅读Selectors

这只是一个例子

这是为AJAX驱动的页面编写脚本时的一个典型问题。像waitForKeyElements()这样的实用程序是为这类问题显式生成的。

以下是一个完整的脚本,应该适用于问题中给出的代码:

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @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 ("a[href='#?#/3/']", removeLinksParents);           
function clickNode (removeLinksParents) {
    jNode.parent ().parent ().parent ().parent ().parent ().remove ();
}

最新更新