Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can add event handlers to specific elements on a page. If the element has an <code>id</code> attribute, this is easy:</p> <pre><code>var element = document.getElementById( 'whatever' ); element.addEventListener( 'click', function () { alert( 'element clicked' ); }, true ); </code></pre> <p>This will add the event listener to the element with the ID "whatever".</p> <p>If the element doesn't have an ID attribute, you'll need to be a bit more creative. Here's one way to do it:</p> <pre><code>var links = document.getElementsByTagName( 'a' ); for ( var i = 0; i &lt; links.length; i++ ) { var link = links[i]; if ( /area=grassland/.test( link.href ) ) { link.addEventListener( 'click', function () { alert( 'grassland link clicked' ); }, true ); } } </code></pre> <p>The <code>/area=grassland/</code> is a <a href="http://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions" rel="nofollow">regexp</a> which matches strings that contain "area=grassland" as a substring. (You can <a href="http://www.javascriptkit.com/javatutors/re.shtml" rel="nofollow">do a lot more with regexps</a> if you want.) We test it against every link (<code>a</code> element) on the page, and add the click handler to those whose <code>href</code> attribute matches the regexp.</p> <p>By the want, you can get the text inside a link with <code>link.textContent</code>, if you'd prefer to match against that. It won't work so well with image links, though. (But you could always find, say, the first image tag inside the link with <code>link.getElementsByTagName( 'img' )[0]</code> and work from there.)</p> <hr> <p>As for your second question, regexps are your friend there too:</p> <pre><code>var regexp = /You find an? (Exalted )?(.*?) egg nearby/; var match = regexp.exec( document.body.textContent ); if ( match ) { if ( match[1] ) { alert( "Exalted egg found: " + match[2] ); } else { alert( "Normal egg found: " + match[2] ); } } </code></pre> <p>See the links I gave above for how that regexp works and how to modify it to better suit your needs.</p> <p>(Ps. Warning: I didn't actually test any of the code above, as I didn't feel like creating a trial account on the game site just for that. I hope it's correct, but there might be bugs or typos in it.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload