<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ITworks &#187; facebook</title>
	<atom:link href="http://itworks.hu/tag/facebook/feed/" rel="self" type="application/rss+xml" />
	<link>http://itworks.hu</link>
	<description>Random musings in IT</description>
	<lastBuildDate>Mon, 09 Jan 2012 08:01:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating for a bot for MafiaWars</title>
		<link>http://itworks.hu/2009/08/12/creating-for-a-bot-for-maffiawars/</link>
		<comments>http://itworks.hu/2009/08/12/creating-for-a-bot-for-maffiawars/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 21:54:07 +0000</pubDate>
		<dc:creator>csak</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[macro]]></category>

		<guid isPermaLink="false">http://itworks.hu/2009/08/12/creating-for-a-bot-for-maffiawars-2/</guid>
		<description><![CDATA[I started playing MafiaWars on my last job, where I had time to spare. As it has happened in the past with me playing on a mud, when the game got repetitive I start writing tools to speed things up.When &#8230; <a href="http://itworks.hu/2009/08/12/creating-for-a-bot-for-maffiawars/">Read more <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I started playing <a href="http://apps.facebook.com/inthemafia/" target="_blank">MafiaWars</a> on my last job, where I had time to spare. As it has happened in the past with me playing on a mud, when the game got repetitive I start writing tools to speed things up.When I was on the mud I used macro-enabled clients, like <a href="http://tintin.sourceforge.net/" target="_blank">tintin++</a> to perform simple tasks for me. Since   <a href="http://apps.facebook.com/inthemafia/" target="_blank">MafiaWars</a> is a Web 2.0 application, one must emulate a web client, so things are a bit trickier than what one can achieve using a simple wget or shell-script based bot.<br />
<span id="more-41"></span><span style="font-weight: bold;"> Preparations </span></p>
<p>Digging through the Internet I couldn&#8217;t find a decent bot-base that I could use for my experiment. The only traces of bots I found were simple brainless fight bots implemented as <a href="https://addons.mozilla.org/en-US/firefox/addon/3863" target="_blank">IMacros.</a></p>
<p><span style="font-weight: bold;">First steps</span></p>
<p>As it seemed like a good idea at the time, I started by installing <a href="https://addons.mozilla.org/en-US/firefox/addon/3863" target="_blank">IMacro plugin</a>, then saving a macro. My first goal was to allow for the bot to fight until it has stamina, then wait until some is regenerated.  I soon discovered that while there is a <code>LOOP</code> command, there is no conditional execution support in the <a href="https://addons.mozilla.org/en-US/firefox/addon/3863" target="_blank">IMacro</a> language. Recognizing that not even a simple AI can be exclusively written in <a href="https://addons.mozilla.org/en-US/firefox/addon/3863" target="_blank">IMacro</a>&#8216;s own language, I switched over to JavaScript based Macros.</p>
<p>The basic idea of the bot is to extract some data off the rendered screen, then perform some action, then start from the begining.</p>
<p><strong>Bot loop</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> terminated<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// Check for termination here</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>terminated<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// Do your logic here</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// I use throw(0) for forced termination when debugging, and I don't want alerts then</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Logging</strong></p>
<p>To make the bot&#8217;s work auditable, and to provide some help for debugging, I created a few simple logging functions.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> consoleWindow<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * Opens a console window
 *
 * @return the console window handle
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> openConsole<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> ww <span style="color: #339933;">=</span> Components.<span style="color: #660066;">classes</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;@mozilla.org/embedcomp/window-watcher;1&quot;</span><span style="color: #009900;">&#93;</span>
 .<span style="color: #660066;">getService</span><span style="color: #009900;">&#40;</span>Components.<span style="color: #660066;">interfaces</span>.<span style="color: #660066;">nsIWindowWatcher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow <span style="color: #339933;">=</span> ww.<span style="color: #660066;">openWindow</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;DescriptiveWindowName&quot;</span><span style="color: #339933;">,</span>
 <span style="color: #3366CC;">&quot;width=600,height=300&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'MaffiaWars Console'</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;html&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;head&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;title&amp;gt;MaffiaWars Console&amp;lt;/title&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;/head&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;body&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;div id=&quot;status&quot; style=&quot;font-size:10px;width:100%;background-color:red&quot;&amp;gt;Starting up&amp;lt;/div&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;div id=&quot;log&quot; style=&quot;font-size:10px&quot;&amp;gt;&amp;lt;/div&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&amp;lt;/body&amp;gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 setConsoleStatus<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> setConsoleStatus<span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>consoleWindow<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 consoleWindow <span style="color: #339933;">=</span> openConsole<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 statusE <span style="color: #339933;">=</span> consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'status'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 statusE.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">=</span>content<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * Writes to the console window, if it exists, opens it if it doesn't
 *
 * @param content
 * @return
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> writeConsole<span style="color: #009900;">&#40;</span>content<span style="color: #339933;">,</span>color<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>consoleWindow<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 consoleWindow <span style="color: #339933;">=</span> openConsole<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 logE <span style="color: #339933;">=</span> consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>color<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> styleA <span style="color: #339933;">=</span> document.<span style="color: #660066;">createAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'style'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 styleA.<span style="color: #660066;">nodeValue</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'background-color:'</span><span style="color: #339933;">+</span>color<span style="color: #339933;">;</span>
 p.<span style="color: #660066;">setAttributeNode</span><span style="color: #009900;">&#40;</span>styleA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 p.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>consoleWindow.<span style="color: #660066;">document</span>.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>d.<span style="color: #660066;">toDateString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> d.<span style="color: #660066;">toLocaleTimeString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: &quot;</span>
 <span style="color: #339933;">+</span> content<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 logE.<span style="color: #660066;">insertBefore</span><span style="color: #009900;">&#40;</span>p<span style="color: #339933;">,</span>logE.<span style="color: #660066;">firstChild</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> isConsoleOpen <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 <span style="color: #000066; font-weight: bold;">return</span> consoleWindow<span style="color: #339933;">!=</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span>consoleWindow.<span style="color: #660066;">closed</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">/**
 * Closes the console window
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> closeConsole<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isConsoleOpen<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 consoleWindow.<span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 consoleWindow<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This implements a simple log window, where you can add new entries with the chosen background color. Since the code is for my own fun, several documentation comments are missing. The window contains two areas, the status area is used to display longer units of work, while the log area displays the logs in reverse order (newest first).</p>
<p>Using the log is pretty straightforward. To set the status you use setConsoleStatus(message) and to add  a new log entry you use the function writeConsole(content,color).The log window is not reused, and the log is not written to file, because I don&#8217;t need it.</p>
<p><strong>Business logic</strong></p>
<p>The business logic of the bot is very simple. It is based on how I actually play the game. The features are implemented in separate functions.</p>
<p>There are some global variables in the script, to store data for workflow based operations. For example the fully mastered job tiers are stored in such a global array.</p>
<p><strong>Completed features</strong></p>
<ul>
<li>Search for and fight opponents with mafias of the same size or smaller, when stamina is available<br />
The function searches the fight screen for the mafia of the smallest size, that is smaller than ours. If found it&#8217;s attacked, if there is no target, the function changes the city and checks the other location for targets.</li>
<li>Going to hospital in NY (I have more money there) when the health is critical</li>
<li>Keeping track of fight results<br />
This feature is not useful for determining opponents yet, as there is no way of identifying the opponents on the Fight screen.</li>
<li>Selling goods in Cuba, then depositing to bank<br />
Selling goods was easier, when all items started with &#8220;Sell&#8230;.&#8221; now there are the corrupt politicians, that are either collected or bought right in the middle. I didn&#8217;t see it introduced, so I thought there was something wrong with the logic&#8230;</li>
<li>Depositing to bank in NY</li>
<li>Doing jobs</li>
<li>Tiers are scanned sequentially to see if there is a job that is available. If the tier is fully mastered, it is stored and is avoided in later searches. The jobs, that are not mastered are checked against the specified requirements (sufficient energy and consumables are available) If there are more jobs that satisfy the requirements the one with most required energy is executed.</li>
<li>Using up energy-packs wisely to get level boosts.<br />
The function considers the amount of energy available, the amount of experience required for the next level, the 25% extra energy from the pack, and a rule-of-thumb multiplyer, to calculate the amount of XP per energy point.</li>
</ul>
<p><span style="font-weight: bold;">Planned features</span></p>
<ul>
<li>Repair and protect buildings in NY, before going to bank</li>
<li>Playing free lotto tickets</li>
<li>Using profile points to boost attack/defense</li>
<li>Identifying job pre-requisites in NY and mark those jobs available despite them  mastered</li>
</ul>
<p><span style="font-weight: bold;">Lessons learned</span></p>
<p><em><span style="font-weight: bold;">JavaScript issues<br />
</span></em></p>
<p>JavaScript has never been the language of my choice. My code probably features really poor practices, there were several things that drove me nuts though.</p>
<p>One of my biggest disappointment in JavaScript, is that it doesn&#8217;t support suspending execution in any way. One can&#8217;t write a simple <code>while(true){}</code> loop, and use a <code>sleep()</code> or <code>yield()</code> function to allow his computer do other things then running the bot. To overcome this issue on Linux I used the <a href="http://cpulimit.sourceforge.net/" target="_blank">cpulimit</a> utility, and ran the browser in a different process than my normal process. In fact I used firefox 3.0, then later firefox 3.6 to run the bot, while using firefox 3.5 as my normal browser. If I was to start again I would be using <a href="http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/" target="_blank">setInterval()</a> and create some kind of heartbeat based finite state machine.</p>
<p>JavaScript doesn&#8217;t allow for code inclusion, which was a surprise for me. I thought I could organize the code properly, into logging, utility and business logic modules. This comes from the browser based nature of JavaScript. Unfortunately modifying the parent document to include the other files is impossible, once you run your script from a plug-in. Maybe it can be worked around, but there is really no need for that on such a pet project.</p>
<p>The other JavaScript issue I had, is that the authors of <a href="http://apps.facebook.com/inthemafia/" target="_blank">MafiaWars</a> used window level variables to store some data, and use that data to dynamically create URL&#8217;s. Thus in order to access the data, that is to be stored in the link I would have to be able to access this data. Unfortunately, I just can&#8217;t. I&#8217;ve tried everything, and even though the variables are visible using <a href="http://getfirebug.com/" target="_blank">FireBug</a> they are inaccessible through the <a href="https://addons.mozilla.org/en-US/firefox/addon/3863" target="_blank">IMacro plugin</a>.</p>
<p><em><strong>IMacro issues</strong></em></p>
<p><a href="https://addons.mozilla.org/en-US/firefox/addon/3863" target="_blank">IMacro</a> has issues with stopping running JavaScript macros. Once you close the iMacros tab it says it will terminate the running JavaScript macro, but in fact it&#8217;s only suspended. I had to implement in all the loops I used logic, to terminate the loop on certain conditions, like navigating the window away from FaceBook or closing the log window.</p>
<p>There is a strange feature of the <a href="https://addons.mozilla.org/en-US/firefox/addon/3863" target="_blank">IMacro</a>, it seems that stopping the script either normally or through an exception takes almost 2 minutes for the browser to recover. I couldn&#8217;t yet find a reason for this, but it certainly causes problems.</p>
<p>While <a href="https://addons.mozilla.org/en-US/firefox/addon/3863" target="_blank">IMacro plugin</a> is certainly great for simple tasks, invoking it frequently from JavaScript kills it&#8217;s performance. After a while it got so bad, that my stamina was recharging faster, then it was spent! To overcome this issue I reimplemented the simple data extraction functions in JavaScript.</p>
<p>These changes have sped the execution up, and reduced the number of times the browser had to be forcefully restarted.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Extracts the element matching the specified parameters
 *
 * @param tagName
 *            type of tag to look for
 * @param pos
 *            occurrence of the tag matching the search attributes
 * @param attributeTxt
 *            attributes to look for. The attributes are : separated key=value
 *            pairs, where the value is either a quoted string or a regular
 *            expression
 * @return the element matching the search attributes, or null if it's not found
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> extractElement<span style="color: #009900;">&#40;</span>tagName<span style="color: #339933;">,</span> pos<span style="color: #339933;">,</span> attributeTxt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> attributeA <span style="color: #339933;">=</span> attributeTxt.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">':'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> attributes <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>attributeA.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> attributeA<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">length</span><span style="color: #339933;">!=</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Error extracting attributes from string '</span> <span style="color: #339933;">+</span> attributeTxt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		attributes<span style="color: #009900;">&#91;</span>s<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>s<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	elements <span style="color: #339933;">=</span> content.<span style="color: #660066;">document</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span>tagName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> curPos <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	outer_loop<span style="color: #339933;">:</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> elementIx<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> elementIx<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>elements.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> elementIx<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> curE <span style="color: #339933;">=</span> elements<span style="color: #009900;">&#91;</span>elementIx<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> attrIx <span style="color: #000066; font-weight: bold;">in</span> attributes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'!curE.'</span><span style="color: #339933;">+</span>attrIx<span style="color: #339933;">+</span><span style="color: #3366CC;">' ||
				!(curE.'</span><span style="color: #339933;">+</span>attrIx<span style="color: #339933;">+</span><span style="color: #3366CC;">'.match('</span><span style="color: #339933;">+</span>attributes<span style="color: #009900;">&#91;</span>attrIx<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'))'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">continue</span> outer_loop<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		curPos<span style="color: #339933;">++;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>curPos<span style="color: #339933;">==</span>pos<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">return</span> curE<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * Extracts the text content from the specified element
 *
 * @param tagName
 * @param pos
 * @param attributeTxt
 * @return text content of the element
 * @see extractElement(tagName, pos, attributeTxt)
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> extractText<span style="color: #009900;">&#40;</span>tagName<span style="color: #339933;">,</span> pos<span style="color: #339933;">,</span> attributeTxt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> curE <span style="color: #339933;">=</span> extractElement<span style="color: #009900;">&#40;</span>tagName<span style="color: #339933;">,</span> pos<span style="color: #339933;">,</span> attributeTxt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>curE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Cannot find '</span><span style="color: #339933;">+</span>tagName<span style="color: #339933;">+</span> <span style="color: #3366CC;">' on position '</span> <span style="color: #339933;">+</span> pos
			<span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>attributeTxt<span style="color: #339933;">?</span><span style="color: #3366CC;">' matching '</span><span style="color: #339933;">+</span>attributeTxt<span style="color: #339933;">:</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> curE.<span style="color: #660066;">textContent</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * Extracts the html content from the specified element
 *
 * @param tagName
 * @param pos
 * @param attributeTxt
 * @return html content of the element
 * @see extractElement(tagName, pos, attributeTxt)
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> extractHTML<span style="color: #009900;">&#40;</span>tagName<span style="color: #339933;">,</span> pos<span style="color: #339933;">,</span> attributeTxt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> curE <span style="color: #339933;">=</span> extractElement<span style="color: #009900;">&#40;</span>tagName<span style="color: #339933;">,</span> pos<span style="color: #339933;">,</span> attributeTxt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>curE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Cannot find '</span><span style="color: #339933;">+</span>tagName<span style="color: #339933;">+</span> <span style="color: #3366CC;">' on position '</span> <span style="color: #339933;">+</span> pos
			<span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>attributeTxt<span style="color: #339933;">?</span><span style="color: #3366CC;">' matching '</span><span style="color: #339933;">+</span>attributeTxt<span style="color: #339933;">:</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> curE.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>The complete code</strong></p>
<p>Did I ever say I was to share it?</p>
]]></content:encoded>
			<wfw:commentRss>http://itworks.hu/2009/08/12/creating-for-a-bot-for-maffiawars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

