Hi this is the first question I have asked so sorry if its stupid, But I am new to JavaScript and 开发者_StackOverflow中文版Greasemonkey in general.
Question is how do I read specific areas of HTML and store them as variables?
Example Code To Read:
<td align="center">
<br>
<b>Player:</b>
58236
<br>
<br>
<b>Guild:</b>
<a href="guild.aspx?guild=23234">Example Code</a>
<br>
<div>
<br>
<b>Old Nickname: </b>
Nick
<br>
<small>(until 2011-07-31)</small>
<br>
<br>
<b>Level:</b>
21.04 (Rank 3,315)
<br>
<b>Economy:</b>
969
<br>
<br>
<b>Account:</b>
Upgraded
<br>
<b>Expire:</b>
2011-08-29
<br>
<br>
<b>Account Start:</b>
49 Days
<br>
<br>
<img title="Account Upgraded Badge" alt="Account Upgraded Badge" src="http://cdn.astroempires.com/images/badges/badge_upgraded.gif">
<br>
<br>
</td>
Now say I wish to extract the level of the player, As I understand it I would have to do somthing like.
Level = document.body.innerHTML.match(/<b>Level:<\/b>/);
And go somewhere from there, But I can't find much example code and explanation of how to read HTML on webpages using this sort of method.
If that's really the HTML, then it's a bit of an art.
If that's a "simplified" version of the HTML, link to the unedited page. How you can extract information depends on the precise details of the HTML.
Anyway, use jQuery. It's CSS-like selectors will really help you to home in on the desired bits.
Here is a Greasemonkey script that uses jQuery to extract the Level from a page like the one indicated. :
// ==UserScript==
// @name _Get info from poorly structured HTML
// @include http://YOUR_SERVER/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==
var LvlMarker = $("b:contains('Level:')");
var LvlText = LvlMarker[0].nextSibling.textContent;
alert (LvlText);
Beware that additional tricks are required on ajax-ified pages.
精彩评论