Can I locally remove table cells on a website using the Firebug console?

36 views Asked by At

Context: I am trying to choose a university course from a list provided via a table from a search engine. The search engine only recognises suffixes if there is a prefix, i.e. COSC3 or COSC350. 3 or 350 would not return any results.

What I would like to know is if it would be possible to use Firefox's Firebug to parse a console command that would remove all table rows that don't contain a 100-level paper.

Pseudocode:

string regex = [A-Z]{4};

for each(tr) {
    for each(td) {
        if(!td.contains(regex + "1") {
            tr.delete();
        }   
    }
}

My pseudocode is probably pretty ineffective but it was designed to give you a general idea as to what I would like to do.

2

There are 2 answers

0
light On

Yes, it's possible.

The general idea is outlined in your pseudo-code. The only tricky thing to note is that when operating on "live" HTMLCollection, you can't loop them like arrays.

// get all rows
var table = document.getElementById('my-table');
var trs = table.getElementsByTagName("tr");

// go through each row
var i = 0;
while (i < trs.length) {
  var tds = trs[i].getElementsByTagName("td");
  var deleted = false;

  // go through each cell of this row
  var j = 0;
  while (j < tds.length) {
    if (/[A-Z]{4}1/.test(tds[j].textContent)) {
      // delete this row
      trs[i].parentNode.removeChild(trs[i]);
      deleted = true;      
      break;
    } else {
      j++;
    }
  }
  
  if (!deleted) {
    i++;
  }
}
<p>The table below should not show any rows containing XXXX100 series courses.</p>
<table id="my-table" cellspacing="0">
  <tr>
    <td>COSC123</td>
    <td>COSC456</td>
    <td>COSC789</td>
  </tr>
  <tr>
    <td>ABCD123</td>
    <td>EFGH124</td>
    <td>IJKL125 <span>span</span></td>
  </tr>
  <tr>
    <td>MNOP233</td>
    <td>QRST294</td>
    <td>UVWX297</td>
  </tr>
  <tr>
    <td>COSC333</td>
    <td>COSC394</td>
    <td>COSC397</td>
  </tr>
  <tr>
    <td>ABCD3000</td>
    <td>ABC3456</td>
    <td>*COSC1997</td>
  </tr>
</table>

0
Sebastian Zartner On

To run JavaScript code on a website using Firebug, you can enter it into its Command Editor and then hit the Run button or pressing Ctrl/ + Enter.

Doing so, the code will be executed in the context of the page.