<?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>Einar Egilsson &#187; Javascript</title>
	<atom:link href="http://einaregilsson.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://einaregilsson.com</link>
	<description>A site for my programming pet projects</description>
	<lastBuildDate>Fri, 10 Feb 2012 09:54:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Spades</title>
		<link>http://einaregilsson.com/spades-card-game/</link>
		<comments>http://einaregilsson.com/spades-card-game/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 08:37:25 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Card Games]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://einaregilsson.com/?p=445</guid>
		<description><![CDATA[Yet another card game finished. The last card game I created was Hearts, which is a trick taking game, but you play on your own. This time I wanted to create a game where you play in teams, so a human player and a computer player work together against two other computer players. So I [...]]]></description>
			<content:encoded><![CDATA[<p>Yet another card game finished. The last card game I created was <a href="http://www.hearts-cardgame.com">Hearts</a>, which is a trick taking game, but you play on your own. This time I wanted to create a game where you play in teams, so a human player and a computer player work together against two other computer players. So I created <a href="http://www.spades-cardgame.com">Spades</a>, a nice trick taking game where you play in teams and must bid before each hand how many tricks you think you&#8217;ll take.<br />
<span id="more-445"></span><br />
It was an interesting game to implement, especially making the computer players work sensibly with their partners. Usually you want to get as many tricks as possible, but you can also bid 0, and then you can&#8217;t take any trick, and your partner has to try to help you out if he sees that you are about to take the trick. You also have to be careful to not get too many tricks above your bid, since if you get 10 tricks more (over the course of a few hands) you&#8217;ll get a big penalty.</p>
<p>I can track the win percentage of the human team and the computer team, so far the computer team only wins about 35% of its games, but it&#8217;s a nice challenge to try to make it win more. And now that the game is live, and has lots of plays every day it&#8217;s a lot of fun to tweak the computer players, publish the new version and then see the next day if the win percentage goes up or down.</p>
<p>For my next project I think I&#8217;ll do something different. I&#8217;ve purchased the domain <a href="http://solitaire-cardgame.com">http://solitaire-cardgame.com</a> and am going to try to create Klondike (classic solitaire) first, if that&#8217;s fun then I&#8217;ll probably add a few others later on.</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/spades-card-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Online Crazy Eights Card Game</title>
		<link>http://einaregilsson.com/online-crazy-eights-card-game/</link>
		<comments>http://einaregilsson.com/online-crazy-eights-card-game/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 21:20:18 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Card Games]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=441</guid>
		<description><![CDATA[My pet projects over the last few months have been mostly Visual Studio Extensions and online card games. And now I&#8217;ve finished a new card game. This time I made Crazy Eights, a popular card game in Iceland under the name Olsen Olsen. There are dozens of variations of it possible but I just made [...]]]></description>
			<content:encoded><![CDATA[<p>My pet projects over the last few months have been mostly Visual Studio Extensions and online card games. And now I&#8217;ve finished a new card game. This time I made Crazy Eights, a popular card game in Iceland under the name Olsen Olsen. There are dozens of variations of it possible but I just made a simple one where eights change suits and you can play multiple cards together if they have the same rank. Not much more to say about it, now go and <a href="http://www.crazyeights-cardgame.com">play Crazy Eights online!</a> </p>
<p>UPDATE 28.06.2011: And now I have <a href="http://www.hearts-cardgame.com/">a new card game, Hearts</a>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/online-crazy-eights-card-game/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Idiot cardgame for Facebook</title>
		<link>http://einaregilsson.com/idiot-cardgame-for-facebook/</link>
		<comments>http://einaregilsson.com/idiot-cardgame-for-facebook/#comments</comments>
		<pubDate>Tue, 07 Dec 2010 22:28:19 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Card Games]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=419</guid>
		<description><![CDATA[A couple of years ago I created a simple javascript version of the cardgame Idiot. It was very simple, no animation, tracking of scores or anything like that. Still, it got a few users, and there was some activity on it every day. A few months ago I wanted to learn some new technologies. I [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago I created a <a href="http://einaregilsson.com/2008/09/13/idiot-cardgame/">simple javascript version of the cardgame Idiot</a>. It was very simple, no animation, tracking of scores or anything like that. Still, it got a few users, and there was some activity on it every day. A few months ago I wanted to learn some new technologies. I wanted to try Google App Engine, learn more about jQuery and try creating a Facebook app. So, I decided to use Idiot as a test project and create a better version of the game. <span id="more-419"></span></p>
<p>I worked on it on and off for months. No pressure to finish since it was just a hobby project. And finally, on November 18th I launched the new version at <a href="http://www.idiot-cardgame.com">http://www.idiot-cardgame.com</a> and at <a href="http://apps.facebook.com/idiot-cardgame/">http://apps.facebook.com/idiot-cardgame/</a> to play it within Facebook itself.</p>
<div style="border:solid 1px black; margin-bottom:10px; padding:5px; border-radius:5px; -moz-border-radius:5px; -webkit-border-radius:5px; background-color:#ddd;"><strong>UPDATE 24.05.2011:</strong> Because the game is better known under the name Shithead I bought a new domain for that, shithead-cardgame.com. Unfortunately both Facebook and Google App Engine were unhappy with a domain with profanity in the title so I forked the code and made a simple anonymous version that&#8217;s not a Facebook app. Go there to <a href="http://www.shithead-cardgame.com/play-shithead-online.html">play shithead card game online</a>.
</div>
<p>The new game has animated playing of cards, shows your avatars and tracks statistics. You can also choose cardbacks and cardfronts, animation speed and some other options, and there is a <a href="http://www.idiot-cardgame.com/highscores">High scores</a> page which shows the 10 best players.</p>
<p>Now, 6 weeks after launch it has about 250 registered Facebook users plus a lot of anonymous users. I wanted to allow anonymous play as well instead of forcing everyone to give their Facebook credentials. However, tracking of statistics and saving preferences requires Facebook registration. There are around 6-700 games played every day and the computer player wins around 45-50% of them.</p>
<p>All in all I&#8217;m happy with the game. I&#8217;ve been adding a few small features and fixing bugs since launch. The next major feature I&#8217;m planning is the ability to play against your friends. The newest App Engine SDK includes support for Comet style connections which would make this a lot easier than it was a few months ago.</p>
<p>Finally, some lessons learned:</p>
<ul>
<li>Create your basic game engine in vanilla javascript and use events (or callbacks) for rendering the actions. I did this and can run games for testing in a simple command line script, which is really nice while you&#8217;re developing the basic game.</li>
<li>Make the human player and the computer player implement the same interface, and let the Game object be ignorant of which player is which. This gives you the ability to play two computer players against each other during testing, which helps a lot if you don&#8217;t want to actually play the game thousands of times during development. I even have hidden options to do this in production so I can just watch an animated game play out.</li>
<li>Facebook SDK documentation is complete crap. Best resource to test things is <a href="http://fbrell.com">http://fbrell.com</a>.</li>
<li>App Engine regularly throws errors when connecting to the Datastore. Not all the time, but it is something you have to plan for.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/idiot-cardgame-for-facebook/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>URL-2-Titlebar Firefox Add-on</title>
		<link>http://einaregilsson.com/url-2-titlebar-firefox-add-on/</link>
		<comments>http://einaregilsson.com/url-2-titlebar-firefox-add-on/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 11:48:04 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Mozilla]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=275</guid>
		<description><![CDATA[Recently I got a request in the mail from a user of one of my Firefox add-ons. He asked me if I could make an add-on that displayed the url of the current tab in the titlebar instead of the actual title of the page being shown. I&#8217;m not interested in creating more add-ons for [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I got a request in the mail from a user of one of my Firefox add-ons. He asked me if I could make an add-on that displayed the url of the current tab in the titlebar instead of the actual title of the page being shown. I&#8217;m not interested in creating more add-ons for Firefox or Thunderbird, they take up quite a lot of time with user requests, getting approved by <a href="http://addons.mozilla.org">the Mozilla addons site</a>, etc. That is why I&#8217;ve <a href="/projects/abandoned/">discontinued</a> 6 out of the 9 add-ons I&#8217;ve created. But anyway, I knew that it would be trivial to make this add-on so I decided to help this person out.<span id="more-275"></span></p>
<p>You can <a href="/download/url2titlebar.xpi">download the add-on</a> here. This add-on will never be updated or published on AMO, it is offered strictly as-is. The one thing I found interesting about this was figuring out how to do the absolute minimum to get this add-on working. There is no localization, no folder structure, just three files, install.rdf, chrome.manifest and url2titlebar.xul. Put those into a zip file, rename it to .xpi and you have your extension. These of course are not best practices in extension development, just an exercise in making the simplest extension possible. Below is the full content of the three files:
</p>
<h5>url2titlebar.xul</h5>
<pre class="brush: xml; title: ; notranslate">&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot;?&amp;amp;gt;
&amp;amp;lt;overlay xmlns=&amp;amp;quot;http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul&amp;amp;quot;&amp;amp;gt;
  &amp;amp;lt;script&amp;amp;gt;
    window.addEventListener('load', function() {
      function setTitle() {
        document.title = gBrowser.selectedBrowser.contentDocument.location;
      }
      gBrowser.tabContainer.addEventListener('TabSelect', setTitle, false);
      document.addEventListener('pageshow', setTitle, false);
      setTitle();
    },false);
  &amp;amp;lt;/script&amp;amp;gt;
&amp;amp;lt;/overlay&amp;amp;gt;
</pre>
<h5>chrome.manifest</h5>
<pre class="brush: plain; title: ; notranslate">
content url2titlebar file:./
overlay chrome://browser/content/browser.xul chrome://url2titlebar/content/url2titlebar.xul
</pre>
<p><strong>install.rdf</strong></p>
<pre class="brush: xml; title: ; notranslate">&amp;amp;lt;?xml version=&amp;amp;quot;1.0&amp;amp;quot; encoding=&amp;amp;quot;UTF-8&amp;amp;quot;?&amp;amp;gt;
&amp;amp;lt;RDF xmlns=&amp;amp;quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&amp;amp;quot;
 xmlns:em=&amp;amp;quot;http://www.mozilla.org/2004/em-rdf#&amp;amp;quot;&amp;amp;gt;
  &amp;amp;lt;Description about=&amp;amp;quot;urn:mozilla:install-manifest&amp;amp;quot;&amp;amp;gt;
    &amp;amp;lt;em:id&amp;amp;gt;url2titlebar@einaregilsson.com&amp;amp;lt;/em:id&amp;amp;gt;
    &amp;amp;lt;em:name&amp;amp;gt;URL-2-Titlebar&amp;amp;lt;/em:name&amp;amp;gt;
    &amp;amp;lt;em:version&amp;amp;gt;0.0.1&amp;amp;lt;/em:version&amp;amp;gt;
    &amp;amp;lt;em:creator&amp;amp;gt;Einar Egilsson&amp;amp;lt;/em:creator&amp;amp;gt;
    &amp;amp;lt;em:description&amp;amp;gt;Set the title of the window to the current url&amp;amp;lt;/em:description&amp;amp;gt;
    &amp;amp;lt;em:targetApplication&amp;amp;gt;
      &amp;amp;lt;Description&amp;amp;gt;
        &amp;amp;lt;em:id&amp;amp;gt;{ec8030f7-c20a-464f-9b0e-13a3a9e97384}&amp;amp;lt;/em:id&amp;amp;gt; &amp;amp;lt;!-- firefox --&amp;amp;gt;
        &amp;amp;lt;em:minVersion&amp;amp;gt;3.0&amp;amp;lt;/em:minVersion&amp;amp;gt;
        &amp;amp;lt;em:maxVersion&amp;amp;gt;3.5.*&amp;amp;lt;/em:maxVersion&amp;amp;gt;
      &amp;amp;lt;/Description&amp;amp;gt;
    &amp;amp;lt;/em:targetApplication&amp;amp;gt;
  &amp;amp;lt;/Description&amp;amp;gt;
&amp;amp;lt;/RDF&amp;amp;gt;
</pre>
<p>So, there you have it, the simplest possible Firefox add-on. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/url-2-titlebar-firefox-add-on/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JScript REPL</title>
		<link>http://einaregilsson.com/jscript-repl/</link>
		<comments>http://einaregilsson.com/jscript-repl/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 20:09:02 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=197</guid>
		<description><![CDATA[Lately I have been doing some COM automation stuff on Windows. I&#8217;ve been using JScript (Microsoft&#8217;s JavaScript implementation) since that&#8217;s available on all Windows machines, and the other option, VBScript, is horrible. Normally I would use Python and the win32com package, but I needed to make some scripts that could work on any box without [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been doing some COM automation stuff on Windows. I&#8217;ve been using JScript (Microsoft&#8217;s JavaScript implementation) since that&#8217;s available on all Windows machines, and the other option, VBScript, is horrible. Normally I would use Python and the win32com package, but I needed to make some scripts that could work on any box without installing Python first. JScript is a pretty nice language, but it doesn&#8217;t come with a <a href="http://en.wikipedia.org/wiki/Read-eval-print_loop">REPL</a> built in, which is very handy when you&#8217;re doing experimental stuff (REPL = Read-Execute-Print-Loop). Now, writing your own REPL in a dynamic language with an <strong>eval</strong> statement is pretty easy, so I did just that. It took about 30 lines, of which about 10 are just about printing evaluated expressions nicely. <span id="more-197"></span></p>
<p>Here&#8217;s how it works. If you enter a single line that ends with ; it is evaluated immediately. If it doesn&#8217;t start with if,while,var,try,do,with,function,switch,for or print it is assumed to be an expression and its value is printed to the screen. If a new line doesn&#8217;t end with ; is is assumed to be the beginning of a larger code block. In that case the script will keep reading in code without evaluating until either you end a line with ;; or you enter a line which consists of only one ;. The script also includes a convenience function called print() to print expressions. The script just uses a single variable, _$, so it won&#8217;t conflict with variables you define when using it. Writing a single line with just the word &#8216;exit&#8217; will stop the script. Below you can see an example of the usage of the REPL. This sample is actually a online version that I did for fun, you can write in any command in it and try it out, it works exactly the same as the real script, although the online version took quite a bit more code. It even has command history, activated with the up and down arrow keys. (There are some issues in Internet Explorer, tab key doesn&#8217;t work and strings with html entities my display incorrectly, because of <a href="http://stud3.tuwien.ac.at/~e0226430/innerHtmlQuirk.html">weird behaviour with &lt;pre&gt; tags and the .innerHTML property).</a></p>
<p><a href="http://einaregilsson.com/download/jsrepl.js">Download script</a> | <a href="http://einaregilsson.com/download/jsrepl.js.html">View script syntax highlighted</a></p>
<p><iframe src="http://einaregilsson.com/repl.html" style="width:620px; height:400px;overflow-x:hidden; border-style:none; border:solid 0px black;" frameborder="0"></iframe></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/jscript-repl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Idiot cardgame (a.k.a. Shithead)</title>
		<link>http://einaregilsson.com/idiot-cardgame/</link>
		<comments>http://einaregilsson.com/idiot-cardgame/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 18:47:33 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Card Games]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=107</guid>
		<description><![CDATA[UPDATE 20.10.2010: A few days ago I launched a completely revamped version of this game, with animation, tracking of results, connection to Facebook and a lot more. That means that the blog post is completely out of date! Play the new version at http://www.idiot-cardgame.com or at http://www.shithead-cardgame.com. Or, you can play the original version, the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE 20.10.2010:</strong> A few days ago I launched a completely revamped version of this game, with animation, tracking of results, connection to Facebook and a lot more. That means that the blog post is completely out of date! Play the new version at <a href="http://www.idiot-cardgame.com">http://www.idiot-cardgame.com</a> or at <a href="http://www.shithead-cardgame.com/play-shithead-online.html">http://www.shithead-cardgame.com</a>. Or, you can play the original version, the one that this blog post is talking about at <a href="http://einaregilsson.com/idiot/">http://einaregilsson.com/idiot/</a>. </p>
<p>Original blog post follows:</p>
<p>The last few weeks my hobby project has been creating a cardgame in Javascript. I&#8217;ve always wanted to create a cardgame and I decided to use simple Javascript so I could make it available online since no-one wants to bother to download small games. I figured there were thousands of blackjacks and solitaires out there so I decided to create my favorite cardgame, Idiot. <span id="more-107"></span>After googling around I found that there was a <a href="http://en.wikipedia.org/wiki/Shithead_(card_game)">Wikipedia page</a> about it under the name Shithead which listed all sorts of different variations. My variation uses the rules that I&#8217;ve usually used in real life:</p>
<ul>
<li>The point is to lose all your cards by putting them on the pile, a card that is put on the pile must be equal or higher than the top card of the pile. If you have no card to put on the pile you must take the entire pile.</li>
<li>There are always three cards in your hand, if you have less than three you must draw enough cards from the deck to get three again. After the deck is finished no more cards are drawn.</li>
<li>There are three downwards facing cards and three upwards facing cards on the table. You cannot play them until your hand is finished. You cannot look at a downward facing card before playing it.</li>
<li>Before you start you can exchange cards from your hand with the upwards facing cards on the table.</li>
<li>You can put a two on top of everything, it is the restart card.</li>
<li>You can put a five on top of everything, it is the reverse card, meaning that in the next turn the opponent must put a card equal or lower than five.</li>
<li>You can put a ten on top of everything and it burns the pile, and you get to put out the first card in the new pile.</li>
<li>You can put out more than one card at a time as long as they have the same rank.</li>
<li>If there are four cards with the same rank at the top of the pile then the pile is removed.</li>
<li>If you have no card that you can put on top of the pile then you may draw the top card of the deck and put on the pile, if it is a legal move then you do not have to take the pile.</li>
</ul>
<p>The game can be played at the url <a href="http://einaregilsson.com/idiot/">http://einaregilsson.com/idiot/</a>. As for the implementation, don&#8217;t expect any animation or fancy stuff. You simply choose the cards you want to put out and press the <em>Play selected cards</em> button. When the computer is playing the cards simply disappear from its hand and show up on the pile. However there is a small delay between each move by the computer so you can see when it puts out two or more cards and when it&#8217;s drawing cards. As for the rule about drawing a card off the top of the deck when you have to take the pile, that is done automatically. You simply press the <em>Take the pile</em> button and you will see a new card appear at the top of the pile (given that the deck is not empty). If the move is legal then the pile will stay, if it is not legal you will get the whole pile in your hand.</p>
<p>The game is implemented in two files, <a href="/download/cards.js">cards.js</a> and <a href="/download/idiot.js">idiot.js</a>. Cards.js is a generic library that has a base class for a card container and concrete classes for a hand, deck and card, it could easily be re-used for any card game. Idiot.js is the game itself, it works allright, although there are a lot of edge cases that could be added to the computer player to make it smarter. The code is pretty heavily commented so it should be easy to follow should anyone want to extend the game or use the library for something. The game files are licensed under a <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution license</a> and so can be used for pretty much anything, they have been tested on the latest versions of all the major browsers. Please leave any error reports or suggestions as comments on this page.</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/idiot-cardgame/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Mozilla Extension Generator</title>
		<link>http://einaregilsson.com/mozilla-extension-generator/</link>
		<comments>http://einaregilsson.com/mozilla-extension-generator/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 01:00:17 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/01/mozilla-extension-generator/</guid>
		<description><![CDATA[Since I started creating Mozilla extensions I spent a lot of time writing boilerplate code, and copying from one extension from the next. Then I found <a href="http://ted.mielczarek.org/code/mozilla/extensionwiz/">Ted Mielczarek's Extension Generator</a> which is a great page that takes some parameters and creates an extension ready for you to use. I used that for some time but in the end I still kept modifying each extension to fit my own style and include my library functions. So I decided to make my own Mozilla Extension Generator in Python]]></description>
			<content:encoded><![CDATA[<p>Since I started creating Mozilla extensions I spent a lot of time writing boilerplate code, and copying from one extension from the next. Then I found <a href="http://ted.mielczarek.org/code/mozilla/extensionwiz/">Ted Mielczarek&#8217;s Extension Generator</a> which is a great page that takes some parameters and creates an extension ready for you to use. I used that for some time but in the end I still kept modifying each extension to fit my own style and include my library functions. So I decided to make my own Mozilla Extension Generator in Python<span id="more-44"></span>, that would spit out extensions exactly the way I want them. I mostly liked the output of Ted&#8217;s generator so I took an extension from it, modified to fit my style and then created the generator to create it. So if you see similarities between extension my by generator and Ted&#8217;s, that&#8217;s the reason, mine is partially based on his output. I&#8217;ve decided to put the generator on my page for others to download, and they can then tweak it to fit their ideas of what should be in all extensions. </p>
<p>When the script is run the output will look something like this:</p>
<pre style="background-color:black; color:white; font-family:monospace; font-size:11pt;">
Mozilla Extension Generator
Copyright (c) 2007 Einar Egilsson (http://einaregilsson.com)

Extension name: My New Extension
Extension code name: MyNewExtension
Extension description: Does this and that
Firefox or Thunderbird (f/t) : f
Include Menu item ? (y/n): y
Include Context menu ? (y/n): y
Author name: Einar Egilsson
Guid: mynewextension@einaregilsson.com
Menu item label: My New MenuItem
Menu item access key: m
Contextmenu item label: My New ContextItem
Contextmenu item access key: c

Creating files...

mynewextension/install.rdf
mynewextension/chrome.manifest
mynewextension/defaults/preferences/mynewextension.js
mynewextension/chrome/content/mynewextension.js
mynewextension/chrome/content/overlay.xul
mynewextension/chrome/skin/overlay.css
mynewextension/chrome/content/mynewextension.png
mynewextension/chrome/content/mynewextensionlib.js
mynewextension/chrome/locale/en-US/mynewextension.dtd
mynewextension/chrome/locale/en-US/mynewextension.properties

Extension complete
</pre>
<p>This will generate all the necessary files for the extension, you can simply go into the folder created, zip it up, name as .xpi and install the extension. The questions are pretty straight forward, just about the only thing you need to know is that Extension code name must be a single word, because it will become a javascript object that all your other code will be wrapped in, to avoid polluting the global namespace. </p>
<p>Now, some features of the generated extension:</p>
<ul>
<li>Localization ready, strings are in .dtd and .properties files</li>
<li>Includes a library file with common function like accessing your prefs, writing to a file and more.</li>
<li>Includes debug functions that output to the javascript console and can be turned off in about:config</li>
<li>Can include menu item and context menu commands</li>
<li>&#8230;and more, just look at the generated files</li>
</ul>
<p>The generator is a single python file that includes all the other files. It should be easy to modify for anyone with a working knowledge of python. There are a few constants at the top that you can tweak, they are:</p>
<pre class="brush: python; title: ; notranslate">
#Constants
#Fill these out so you won't get prompted for them everytime:
YOUR_NAME       = ''
YOUR_DOMAIN     = '' # if filled out the extension guid will be of
                     #the form extensionname@yourdomain.com
FIREFOX_MIN     = '2.0'
FIREFOX_MAX     = '2.0.0.*'
THUNDERBIRD_MIN = '2.0'
THUNDERBIRD_MAX = '2.0.0.*'
INITIAL_VERSION = '0.9'
</pre>
<p>The icon file is also embedded in the file as a base64 encoded string. To replace it with your own file, create a .png image file (34&#215;34 pixels) and run the script below (<a href="/download/base64.py">download</a>) to create the base64 string, then copy it over the existing base64 string in the extension.py file.</p>
<pre class="brush: python; title: ; notranslate">
import sys, base64

if len(sys.argv) == 1:
    print 'Usage: base64 &amp;lt;filename&amp;gt;'
    sys.exit(0)

b64 = base64.b64encode(open(sys.argv[1], 'rb').read())
i = 0

while i &lt; len(b64):
    print b64[i:i+80]
    i += 80
</pre>
<p>And finally, you can <a href="/download/extension.py">download the python script here</a>. You can also <a href="/download/extension.py.html">view it syntax highlighted here</a> but don&#8217;t copy it from there as the syntax highlighting might screw something up. Enjoy <img src='http://einaregilsson.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/mozilla-extension-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript format strings</title>
		<link>http://einaregilsson.com/javascript-format-strings/</link>
		<comments>http://einaregilsson.com/javascript-format-strings/#comments</comments>
		<pubDate>Tue, 31 Jul 2007 16:02:13 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/07/31/javascript-format-strings/</guid>
		<description><![CDATA[Python has been my favorite language for a while but since I started making Mozilla Extensions I&#8217;ve started to like Javascript a lot too. One thing I miss from Python though are the format strings. In Python you can always create a string like this: Since Javascript is a very flexible language and allows you [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript" src="/download/javascript-format-strings.js"></script><br />
Python has been my favorite language for a while but since I started making Mozilla Extensions I&#8217;ve started to like Javascript a lot too. One thing I miss from Python though are the format strings. In Python you can always create a string like this:</p>
<pre class="brush: python; title: ; notranslate">
s = '%s is a %s' % ('John', 'Moron')
</pre>
<p>Since Javascript is a very flexible language and allows you to alter its built in types I decided to try to create something similar for it. Here&#8217;s what I came up with:<br />
<span id="more-43"></span></p>
<pre class="brush: jscript; title: ; notranslate">
String.prototype.$ = function() {

  s = this;
  if (arguments.length == 1 &amp;amp;amp;&amp;amp;amp; arguments[0].constructor == Object) {
    for (var key in arguments[0]) {
      s = s.replace(new RegExp(&amp;amp;quot;\$&amp;amp;quot; + key, &amp;amp;quot;g&amp;amp;quot;), arguments[0][key]);
    }
  } else {
    for (var i = 0; i &amp;amp;lt; arguments.length; i++) {
      s = s.replace(new RegExp(&amp;amp;quot;\$&amp;amp;quot; + (i+1), &amp;amp;quot;g&amp;amp;quot;), arguments[i]);
    }
  }
  return s;
};
</pre>
<p>The function works in two ways.</p>
<ol>
<li>You can send in 1-n parameters and then $1 &#8211; $n in the string will be replaced by the parameters values. Example
<pre class="brush: jscript; title: ; notranslate">
var undef; //Undefined value
var s = 'My name is $1 and I am $2 years old. This is a null variable: $3, this one is undefined: $4' .$ ('Einar', 27, null, undef);
alert(s);
</pre>
<p><button style="width:40px; margin-right:30px;" onclick="test_numbers();">  Test  </button></p>
</li>
<li>You can send in 1 parameter that&#8217;s a javascript object and then $keyname in the string will be replaced by the value for &#8216;keyname&#8217; in the javascript object.  Example
<pre class="brush: jscript; title: ; notranslate">
var d; //Undefined value
var dict = { name : 'Einar', age : 27, nullvar : null, undef : d };
var s = 'My name is $name and I am $age years old. This is a null variable: $nullvar, this one is undefined: $undef' .$ (dict);
alert(s);
</pre>
<p><button style="width:40px; margin-right:30px;" onclick="test_dict();">  Test  </button></p>
</li>
</ol>
<p>This works fine in Mozilla Firefox and Internet Explorer 7, I haven&#8217;t tested it in other browsers. I know the $ is used for document.getElementById in prototype and maybe other js libraries but I thought it was the best choice here too, I don&#8217;t think it&#8217;s confusing to use it for these two things. In case the syntax highlighting stuff screwed up the code on the page you can also <a href="/download/javascript-format-strings.js">download it here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/javascript-format-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

