在AJAX驱动的页面上基于特定href删除节点的麻烦?

时间:2015-11-08 15:46:11

标签: javascript jquery xpath greasemonkey

它应该很简单,但我正在尝试的一切似乎都不起作用。也许我太累了,所以我要求新鲜的眼睛。请帮忙。
在Greasemonkey中:检查页面以获取此确切链接<a href="#?#/3/">。如果它在那里,从视图中删除其父项。

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

来自this

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>

3 个答案:

答案 0 :(得分:0)

我认为你只需要使用*

$("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

这是Just Example

答案 1 :(得分:0)

如果您看到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
});

答案 2 :(得分:0)

这是针对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 ();
}