-1

It's should be simple, but everything I'm trying doesn't seem to work. Maybe I'm overly tired, so I'm asking for fresh eyes. Please help.
In Greasemonkey: Check page for this exact link <a href="#?#/3/">. If it's there, remove its parents from view.

Things I've tried (pretty much all can be found on another stack page)

1 from this

// @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";
}

EDIT

I didn't think about that before, but it's true you do need to wait for the page to load. (about 3 seconds, there's a jQuery loading wheel) I didn't believe that was an issue with the Greasemonkey extension?

This is essentially what the structure of the website is. And there are 200+ initial div classes with different URLs to parse.

<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>
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
J. Smith
  • 13
  • 4

3 Answers3

0

I think you just need to use *

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

and I really don't know what do you mean with parent().parent()........ but if you need to remove div with class one you can use

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

and you can use your code inside

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

may be you need to read about Selectors

and this is Just Example

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • There could be a div class=198 that I would need to remove. Whatever div has the special href, I need to remove, and the only thing that's different from div to div is the presence of the special href. The .parent() essentially goes up one level. – J. Smith Nov 08 '15 at 16:29
0

If you see a jQuery loading wheel then the page is definitely generated by a script which means that the element you need doesn't exist when the userscript is executed at DOMContentLoaded / load event by default (about the same time as $(document).ready() or $(function() { ... }))

The correct solution is to either check periodically (setTimeout / setInterval) whether the element appeared or use a MutationObserver.

An example using setMutationHandler wrapper:

// @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
});
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Something like this? setTimeout(function() { postinsql(topicId); }, 4000) – J. Smith Nov 08 '15 at 16:36
  • Yeah, something like this, why not? Although personally I prefer MutationObserver. – wOxxOm Nov 08 '15 at 16:36
  • I'm looking for a quick solution if it's dirty it doesn't matter. Is there's a fast guide to MutationObserver? I should of checked the link. Thanks i'll read on. – J. Smith Nov 08 '15 at 16:44
0

This is a typical issue when scripting for AJAX-driven pages. Utilities like waitForKeyElements() are made explicitly for this kind of problem.

Here is a complete script that should work for the code given in the question:

// ==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 ();
}
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • I tried this code because it looks very promising. It took a few seconds longer to load the images so I was hopeful but unfortunately the bad divs are still present. For the time being I'll have to sleep and read up on mutation observation later. But this type of answer would have been exactly what I was looking for. – J. Smith Nov 08 '15 at 17:08
  • This code wouldn't affect the image loading time at all. Something that is not in the question is the matter. **[Check the *Browser Console*](http://stackoverflow.com/a/31086018/331508)** and also provide a link to the target page. – Brock Adams Nov 08 '15 at 17:28