-1

The webpages of a certain website are all constructed out in sets of tables. Basically, each set of tables is constructed from scripts.

There are about 80 table tags in the DOM of any webpage I've checked in that site (I counted 77 tables in one page in the DOM). A very old site.

Some element children (td) include some tables inside themselves.

I Came to the conclusion that webpages in that site are constructed from scripts because of two reasons:

1) Either of these won't work on basically any table element in a webpage:

document.querySelector("#mySelector").style.display = "none";
document.querySelector(".mySelector").style.display = "none";
document.querySelector(" /* LONG-CSS-PATH-TAKEN-FROM-DEVTOOL (via "Copy CSS path") */ ").style.display = "none";

Such action will usually output:

TypeError: document.querySelector(...) is null

Ofcourse, all 3 examples work fine on other websites.

2) There are script segments before different parts of the DOM.


My question:

My question is comprised of the following two questions:

1) How could I prevent loading of a particular script that creates a certain set of tables? (a certain part of the whole webpage)?

  • I ask this because it seems to be the only way to hide some elements in this odd layout, given that targeting them traditionally (ID, Class, CSS-PATH) gives the above error.

2) May there be another reason than "loading HTML from scripts" that causes the elements not to be targeted in the traditional way?

Notes:

  1. I run this script from Console.

  2. I also tried to run it with Greasemonkey inside a window.onload event handler.

sangoko
  • 331
  • 1
  • 13
  • 1
    If an element is not in the DOM, a DOM method like `querySelector` will not be able to find it and will return `null`. That’s all there is to it. – Sebastian Simon Sep 01 '17 at 16:12
  • Thank you, so how could I effect this code and manipulate it? – sangoko Sep 01 '17 at 16:21
  • When do your `document.querySelector(...)` code gets executed? Do you type it directly in the console? Do you send a request from a distant/local server ( web scraping ) ? – Bird Sep 01 '17 at 16:43
  • I run it from either Greasemonkey or console --- It get's executed but I get the above error. Regarding web scraping - No I didn't do that. – sangoko Sep 01 '17 at 16:50

1 Answers1

1

If you can see the element in the DOM, you can query it.

One possible explanation for the behavior you're seeing is that the elements are being loaded into the DOM after your query. You haven't provided enough clues to offer any practical advice other than to figure out what scripts are being loaded and take it from there.

Another possible explanation is that the tables are in iframes. Querying against the DOM of the top frame won't return elements in the DOMs of other frames. You might see them in the DevTools Elements pane because it shows all the DOMs, but you have to get a reference to the correct frame's document before your can query it.

Last but definitely not least is the very simple question... WAT? What's going on there? Hundreds of tables? It makes no sense, even by the standards of the late 90s when using tables for layout was considered a good idea.

Your strategy shouldn't be to hide them but rather to completely get rid of them. Consider running away.

shovavnik
  • 2,878
  • 3
  • 24
  • 21
  • Yes, 77 tables is wired (I edited the question after DOM count to ensure precision) but to the body of the issue: I did see in `frameset`, and I think also `iframe` tags in the huge DOM of the webpage. But even if it is framesets that has many tables, is there any comfortable way to target the table inside it? – sangoko Sep 01 '17 at 20:54
  • Not really. If the outer and inner frames share the same origin, you can use this technique: https://stackoverflow.com/a/13875886/213343. Otherwise, you can either add support for CORS on the server or use the `postMessage` method on the client, but it's definitely more involved. Security has been hardened everywhere since that site was built. – shovavnik Sep 03 '17 at 13:18
  • Seriously, though, all those tables are bad programming, framesets are deprecated, and hiding those tables instead of just getting rid of them probably won't work the way you expect it to. Expect, for example, for the tables to appear briefly before they disappear even if you do manage to hide them. You should really consider a different approach. – shovavnik Sep 03 '17 at 13:22