Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab HTML table content through a userscript?

I'm creating a Greasemonkey script which reads and stores information from a text-based game into a database to use in the future.

What I want is to be able to read the user's stats and turn those stats into variables so that I can proceed with making the information useful.

Here is the source code of the table which I want to grab the stats info from:

<table width="100%">
  <tr>
    <td width="50%" valign="top" style="padding-right: 25px;">
      <table class="table_lines" width="100%" cellspacing="0" cellpadding="6" border="0">
        <tr>
          <th colspan="3">Military Effectiveness</th>
        </tr>
        <tr>
          <td><b>Strike Action</b></td>
          <td align="right">16,376,469,657</td>
          <td align="right">Ranked #443</td>
        </tr>
        <tr>
          <td><b>Defensive Action</b></td>
          <td align="right">4,016,716,436</td>
          <td align="right">Ranked #569</td>
        </tr>
        <tr>
          <td><b>Spy Rating</b></td>
          <td align="right">12,245,896</td>
          <td align="right">Ranked #1,204</td>
        </tr>
        <tr>
          <td><b>Sentry Rating</b></td>
          <td align="right">5,291,630,090</td>
          <td align="right">Ranked #831</td>
        </tr>
      </table>

Now as you can see the stats don't have identifying class IDs or anything, so I'm not sure how to do this. I only really use PHP, so JavaScript is very new to me but it seems similar to PHP.

Maybe something that says "After <td><b>Strike Action</b></td>, grab the first td value" and then put it as a variable?

NOTE: Strike Action, Defensive Action, Spy Rating, and Sentry Rating are the variables I need.

like image 414
Billy Rammal Avatar asked Oct 28 '25 06:10

Billy Rammal


1 Answers

  1. Use jQuery to make parsing the table easier.
  2. Since you want the rating, don't forget to parse the numbers into javascript integers.
  3. If the page is AJAX-driven, use AJAX-aware techniques.

Here is a complete Greasemonkey/Tampermonkey script showing how to do all of that:

// ==UserScript==
// @name     _Parse table information that has low information scent.
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @include  http://bilalrammal.ca/clicker/tester.html
// @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 (".table_lines", parseMilitaryEffectivenessTable);

function parseMilitaryEffectivenessTable (jNode) {
    //--- Note that :contains() is case-sensitive.
    var strikeAction    = jNode.find ("tr:contains('Strike Action') td:eq(1)").text ();
    var defensiveAction = jNode.find ("tr:contains('Defensive Action') td:eq(1)").text ();
    var spyRating       = jNode.find ("tr:contains('Spy Rating') td:eq(1)").text ();
    var sentryRating    = jNode.find ("tr:contains('Sentry Rating') td:eq(1)").text ();

    //--- Convert strings to integers...
    strikeAction        = parseInt (strikeAction   .replace (/\D/g, ""), 10);
    defensiveAction     = parseInt (defensiveAction.replace (/\D/g, ""), 10);
    spyRating           = parseInt (spyRating      .replace (/\D/g, ""), 10);
    sentryRating        = parseInt (sentryRating   .replace (/\D/g, ""), 10);

    //--- Show on console:
    console.log ("strikeAction: ",       strikeAction);
    console.log ("defensiveAction: ",    defensiveAction);
    console.log ("spyRating: ",          spyRating);
    console.log ("sentryRating: ",       sentryRating);
}
like image 133
Brock Adams Avatar answered Oct 29 '25 21:10

Brock Adams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!