views:

32

answers:

1

I'm writing a Greasemonkey script, and I wanted to change the text of a forum post on a phpBB2 forum by using XPath to select the body of the post that occurs after a certain username, but the whole thing is a giant mess of tables.

<tr>
  <td>
    <span class="name">
      <a>
      <b>username</b>
    </span>
    <span></span>
  </td>
  <td>
    <table>
      <tbody>
        <tr></tr>
        <tr></tr>
        <tr>
          <td>
            <span class="postbody">text of post</span>
            <span></span>
          </td>
        </tr>
      </tbody>
    </table>
  </td>
</tr>

I need to get the postbody span that happens after the username in the b tag equals a certain name, and then mess with the text. Here is how I am trying to do it:

var postguy = document.evaluate('fffffff', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < postguy.snapshotLength; i++) {
    thisLink = postguy.snapshotItem(i);
    //then do something with the post
}
+3  A: 

I'd do it like this:

//tr[.//b/text()='username']//span[@class='postbody']

To align with the comments, you could narrow things down a bit by restricting where the b tag is:

//tr[.//span[@class='name']//b/text()='username']//span[@class='postbody']

Damn it! I was in too much of a hurry for those @s. Gotta slow down and smell the @s sometimes.

Welbog
Will give a false positive if someone puts the magic username bolded in their post text, I would imagine - I think we need to restrict the search for the username to the `name` class span.
AakashM
@AakashM: A fair suggestion. I will add the restriction.
Welbog
You need to put a @ in front of the classes.
Biff