1

I have an XPath /html/body/div[1]/div/div/center[1]/table and I would like to make it so it is no longer visible.

I saw I could use document.evaluate() but I am not sure how to hide it.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Bijan
  • 7,737
  • 18
  • 89
  • 149

1 Answers1

5

If you insist on using XPath, for a single node, use FIRST_ORDERED_NODE_TYPE like so:

var targEval = document.evaluate (
    "/html/body/div[1]/div/div/center[1]/table",
    document.documentElement,
    null,
    XPathResult.FIRST_ORDERED_NODE_TYPE,
    null
);
if (targEval  &&  targEval.singleNodeValue) {
    var targNode  = targEval.singleNodeValue;

    // Payload code starts here.
    targNode.style.display = "none";
}



But, XPath selectors are very brittle in userscripts. You're better off using a CSS selector in most cases.
Since the HTML wasn't provided, here is the CSS-selector method shown with a translation of that XPath into a CSS selector:

var targNode = document.querySelector ("body > div:nth-of-type(1) > div > div > center:nth-of-type(1) > table");
if (targNode) {
    targNode.style.display = "none";
}



Of course, it's even simpler with jQuery:

$("body > div:first > div > div > center:first > table").hide ();

or possibly:

$("body > div:first > div > div > center:first > table").first ().hide ();



If the node is added via AJAX, use a technique such as in this answer.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295