<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-7117612757607239790</atom:id><lastBuildDate>Tue, 21 May 2013 15:26:50 +0000</lastBuildDate><category>Visual Studio</category><category>Microsoft Office</category><category>jQuery</category><category>Unity 3D</category><category>Javascript</category><category>Cracking</category><category>C</category><category>Gosu</category><category>CIL</category><category>YouTube</category><category>T4</category><category>Security</category><category>Java</category><category>Syntax Highlighting</category><category>Mercurial</category><category>XNA</category><category>Game Development</category><category>ASP.NET</category><category>Farseer Physics Engine</category><category>C#</category><category>Haskell</category><category>Windows Phone 7</category><category>Vim</category><category>NGUI</category><category>Electronics</category><category>Other</category><category>Mathematics</category><category>Arduino</category><category>Genetic Algorithms</category><category>Brainfuck</category><category>Ruby</category><category>Linux</category><category>Projects</category><category>Google Chrome Extensions</category><category>Ubuntu</category><category>Networks</category><category>Scheme</category><category>Kongregate</category><category>Facebook</category><category>LaTeX</category><category>Meta</category><title>Andreas Grech's Blog</title><description>Coffee, a pack of cigs and a keyboard</description><link>http://blog.dreasgrech.com/</link><managingEditor>noreply@blogger.com (Andreas Grech)</managingEditor><generator>Blogger</generator><openSearch:totalResults>116</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Knowledge-aholic" /><feedburner:info uri="knowledge-aholic" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>Knowledge-aholic</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-6333369263455524454</guid><pubDate>Tue, 07 May 2013 21:44:00 +0000</pubDate><atom:updated>2013-05-08T00:22:34.667+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unity 3D</category><category domain="http://www.blogger.com/atom/ns#">NGUI</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Coloring individual characters in an NGUI UILabel text</title><description>For the game project that I'm currently working on, I needed to find an easy way to color individual characters in the text of an NGUI &lt;a href="http://www.tasharen.com/?page_id=166" target="_blank"&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;UILabel&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Fortunately, the solution is pretty simple.  When the &lt;a href="http://www.tasharen.com/ngui/docs/class_u_i_label.html#a80bac259fe53a893a95f0e576cf69a00" target="_blank"&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;supportEncoding&lt;/span&gt;&lt;/a&gt; boolean field on a &lt;span style="font-family: Courier New, Courier, monospace;"&gt;UILabel&lt;/span&gt; is set to &lt;span style="font-family: Courier New, Courier, monospace;"&gt;true&lt;/span&gt;, the text supports color encoding in the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;[RRGGBB]&lt;/span&gt; format.&lt;br /&gt;
&lt;br /&gt;
This means that we can have write text such as the following (&lt;i&gt;the [-] is used to revert to a previous color&lt;/i&gt;):&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;pre&gt;[FF0000]Red[-] [00FF00]Green[-] [0000FF]Blue[-]
&lt;/pre&gt;&lt;/center&gt;&lt;br /&gt;
&lt;center&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-d9Tl2QzFhhg/UYlwSPCLo5I/AAAAAAAABTY/TB0JkJKX-3k/s1600/RGB.png" /&gt;&lt;/center&gt;&lt;br /&gt;
The reason I needed to do this was because we want the leading zeros in the Score &lt;span style="font-family: Courier New, Courier, monospace;"&gt;UILabel&lt;/span&gt; to have a different colour than the actual score.  And for that, I wrote this neat little method to format my current score to a string with a predefined number of leading zeros and then color those leading zeros independent from the main colour of the label text.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;private string FormatScore(int score, string leadingZerosRGBColor, int totalDigits)
{
    var scoreString = score.ToString(CultureInfo.InvariantCulture);
    var zeros = totalDigits - scoreString.Length;
    if (score == 0)
    {
        scoreString = String.Empty;
        zeros++;
    }

    return String.Format("[{0}]{1}[-]{2}", leadingZerosRGBColor, new string('0', zeros &amp;lt;= 0 ? 0 : zeros), scoreString);
}
&lt;/pre&gt;&lt;br /&gt;
And the end result pretty much looks like our designer intended:&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-cCbKVce-Kmw/UYlu9nlJIkI/AAAAAAAABTQ/EUxQNav0Ll0/s1600/T3D+Score.png" /&gt;&lt;/center&gt;&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Hawkwind",
  name : "Assault &amp; Battery Part 1",
  link : 'http://grooveshark.com/s/Assult+And+Battery+Part+I/3gqenJ?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/hMWhxyihoPk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/hMWhxyihoPk/coloring-individual-characters-in-ngui.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-d9Tl2QzFhhg/UYlwSPCLo5I/AAAAAAAABTY/TB0JkJKX-3k/s72-c/RGB.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/05/coloring-individual-characters-in-ngui.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-3679196015459387434</guid><pubDate>Fri, 26 Apr 2013 14:34:00 +0000</pubDate><atom:updated>2013-04-26T16:34:44.809+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><category domain="http://www.blogger.com/atom/ns#">Meta</category><title>Using the Blogger API to search within a post's HTML content</title><description>A couple of months ago I wanted to switch the syntax highlighter I used to have on this blog to &lt;a href="http://sunlightjs.com/" target="_blank"&gt;SunlightJS&lt;/a&gt;.  Thing is though, I didn't want to go through every individual post I had written before and check whether they are using the (old) syntax highlighter to update their HTML markup.&lt;br /&gt;
&lt;br /&gt;
Another problem was that the only way I would be able to truly know if a particular post is using the old syntax highlighter was by going through it's HTML content and determine whether the post currently has a tag like the following: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&amp;lt;pre class="&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The issue here is that the normal Google driven searchbox packaged with Blogger (the one you see on the right hand side) does not search through a posts' HTML content but rather through its textual content only which means it was useless for my task.&lt;br /&gt;
&lt;br /&gt;
Because of this, I decided to write a script to semi-automate this task for me.  The reason I say that the script semi-automates the job is because although I wanted it to show me all the posts in which I had used the old highlighter before, I could not risk making it to update the blog post automatically to use the new syntax highlighter because that would have involved HTML parsing and it was not worth the hassle.  Besides, I didn't want to risk ruining my old posts because of a script fuckup.&lt;br /&gt;
&lt;br /&gt;
So what the script does is it uses the Blogger API to batch-fetch all my previous blog posts, go through each posts' individual HTML content and notify me whether that particular post is using the old syntax highlighter.  Searching is done using the regular expression engine and for my case, the function was pretty trivial:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;getOccurences = function(haystack, needle) {
    var matches = haystack.match(new RegExp(needle, "g"));
    return matches ? matches.length: 0;
}
&lt;/pre&gt;&lt;br /&gt;
The Blogger API url I used to fetch my posts' details is as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;https://www.googleapis.com/blogger/v3/blogs/&amp;lt;blogid&amp;gt;/posts?key=&amp;lt;API key&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
For the script to work I needed to provide it with three things:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;My Blogger API key for this project&lt;/li&gt;
&lt;li&gt;My blog's ID (&lt;a href="http://support.google.com/blogger/bin/answer.py?hl=en&amp;amp;answer=42191" target="_blank"&gt;this is how you find yours&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;The search query I wanted to use&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
Here's a demo of the script in action: &lt;a href="http://jsfiddle.net/ydtuc/77/" target="_blank"&gt;http://jsfiddle.net/ydtuc/77/&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
And this is how it looks like:&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;a href="http://1.bp.blogspot.com/-BeFKModv_uI/UXqI0gCoR1I/AAAAAAAABS8/EihJxu4EIyY/s1600/BlogScriptsSearcher.png" imageanchor="1"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-BeFKModv_uI/UXqI0gCoR1I/AAAAAAAABS8/EihJxu4EIyY/s320/BlogScriptsSearcher.png" /&gt;&lt;/a&gt;&lt;/center&gt;&lt;br /&gt;
From the live demo (or the screenshot), you can see the output of the script in the bottom right pane.  The script nicely formats each of the returned matches in a way that if I click on the #number of the post, it would take me to the actual blog post and clicking on the name of the post takes me to the Blogger editor for that current post so that I can easily update it.  The number in parentheses next to the title of the post is the total number of occurrences the needle appears in the haystack.&lt;br /&gt;
&lt;br /&gt;
This is the full script:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;var key = 'AIzaSyCEsBaStg4bOHA2Yp75OqfbjBkk_kq0cMw',
    blogId = '7117612757607239790',
    baseMatcher = 'JavaScript', // the needle.
    allPosts = [],
    isProcessing,
    updateButton = $("#update"),
    update = (function (button) {
        var updateText = function (message, color) {
            button.attr("value", message);
            if (color) {
                button.css("color", color);
            }

        },
            revertButtonMessage = function () {
                changeButtonMessage("Update", "black");
            },
            changeButtonMessage = function (message, color) {
                var didIt;
                updateText(message, color);
                setTimeout(revertButtonMessage, 1000);
            },
            switchButtonMessage = function (message, color) {
                updateText(message, color);
            };

        return {
            changeButtonMessage: changeButtonMessage,
            switchButtonMessage: switchButtonMessage,
            revertButtonMessage: revertButtonMessage
        };
    }(updateButton)),
    formatLink = function (text, href) {
        return '&amp;lt;a href="' + href + '" target="_blank"&amp;gt;' + text + '&amp;lt;/a&amp;gt;';
    },
    getOccurences = function (haystack, needle) {
        var matches = haystack.match(new RegExp(needle, "g"));
        return matches ? matches.length : 0;
    },
    logger = (function () {
        var logs = $("#logs"),
            appendLine = function (line) {
                logs.append((line ? line : "") + "&amp;lt;br/&amp;gt;");
            };

        return {
            log: function (items) {
                appendLine('&amp;lt;span class="logHeader"&amp;gt;Logs #' + new Date().getTime() + '&amp;lt;/span&amp;gt;');
                appendLine();
                $.each(items, function (i, item) {
                    appendLine(formatLink('#' + i, item.url) + ': ' + formatLink(item.title, "http://www.blogger.com/blogger.g?blogID=" + blogId + "#editor/target=post;postID=" + item.id) + ' (' + getOccurences(item.content, baseMatcher) + ')');
                });
                appendLine();
            },
            clear: function () {
                logs.html('');
            }
        };
    }()),
    filter = function (items, predicateForPass) {
        var i = 0,
            j = items.length,
            item, passed = [],
            inc = 0;

        for (; i &amp;lt; j; ++i) {
            item = items[i];
            if (!predicateForPass(item)) {
                continue;
            }

            passed.push(item);
        }

        return passed;
    },
    getPosts = function (pageToken) {
        var url = 'https://www.googleapis.com/blogger/v3/blogs/' + blogId + '/posts?key=' + key,
            get = function (token) {
                var pagedUrl = url + (token ? ("&amp;amp;pageToken=" + token) : "");
                $.get(pagedUrl, function (response) {
                    var token = response.nextPageToken;
                    allPosts = allPosts.concat(response.items);

                    if (token) {
                        return get(token);
                    }

                    isProcessing = false;
                    update.changeButtonMessage("Finished", "green");
                    var filtered = filter(allPosts, function (item) {
                        var preTotal = getOccurences(item.content, baseMatcher),
                            preNewSyntaxHighlighter = getOccurences(item.content, baseMatcher + "sunlight");
                        return (preTotal - preNewSyntaxHighlighter) &amp;gt; 0;
                    });

                    console.log(filtered);
                    logger.clear();
                    logger.log(filtered);
                    allPosts.length = 0;
                });
            };

        update.switchButtonMessage("Getting posts...", "green");
        isProcessing = true;

        get(pageToken);
    };
updateButton.click(function () {
    if (isProcessing) {
        return update.changeButtonMessage("Busy; try again later", "red");
    }

    getPosts();
});

getPosts();
&lt;/pre&gt;&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Atomic Rooster",
  name : "Seven Lonely Streets",
  link : 'http://grooveshark.com/s/Seven+Lonely+Streets/345YtP?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/R_hch4eL6Qg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/R_hch4eL6Qg/using-blogger-api-to-search-within.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-BeFKModv_uI/UXqI0gCoR1I/AAAAAAAABS8/EihJxu4EIyY/s72-c/BlogScriptsSearcher.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/04/using-blogger-api-to-search-within.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-2607515549618732889</guid><pubDate>Fri, 19 Apr 2013 21:42:00 +0000</pubDate><atom:updated>2013-05-08T00:19:29.100+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unity 3D</category><category domain="http://www.blogger.com/atom/ns#">NGUI</category><title>Fading a UITexture's opacity within a panel in NGUI</title><description>Here is a common scenario I'm commonly finding myself into: changing a &lt;span style="font-family: Courier New, Courier, monospace;"&gt;UITexture&lt;/span&gt;'s opacity using its parent &lt;span style="font-family: Courier New, Courier, monospace;"&gt;UIPanel&lt;/span&gt;'s alpha field.&lt;br /&gt;
&lt;br /&gt;
The first time I tried this out, I couldn't get it to work.  I was changing the opacity of the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;UIPanel&lt;/span&gt; using the alpha field but the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;UITexture&lt;/span&gt; inside the panel wasn't fading.&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-JgxRRAU30Hw/UXG5ZK7zCnI/AAAAAAAABSk/M6NBXHqBZB8/s1600/NoTransparentUITexture.PNG" /&gt;&lt;/center&gt;&lt;br /&gt;
Long story short, you need to change the Shader of your &lt;span style="font-family: Courier New, Courier, monospace;"&gt;UITexture&lt;/span&gt; to something like &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Unlit/Transparent Colored&lt;/span&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;a href="http://2.bp.blogspot.com/-U2GXt2XjHUM/UXG6CMvhBUI/AAAAAAAABSs/rZtSfrATvCI/s1600/UnlitTransparentShader.png" imageanchor="1"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-U2GXt2XjHUM/UXG6CMvhBUI/AAAAAAAABSs/rZtSfrATvCI/s320/UnlitTransparentShader.png" /&gt;&lt;/a&gt;&lt;/center&gt;&lt;br /&gt;
And this will give you the result you desired:&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-JPcFahkaRaU/UXG5B8s7SPI/AAAAAAAABSc/ylS9-TJWeBk/s1600/TransparentUITexture.PNG" /&gt;&lt;/center&gt;&lt;br /&gt;
This is how it can be done programatically in C#:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;var uiTexture = powerupTexture.AddComponent&amp;lt;UITexture&amp;gt;();
uiTexture.shader = Shader.Find("Unlit/Transparent Colored");
&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/WZn6UrHAu2c" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/WZn6UrHAu2c/fading-uitextures-opacity-within-panel.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-JgxRRAU30Hw/UXG5ZK7zCnI/AAAAAAAABSk/M6NBXHqBZB8/s72-c/NoTransparentUITexture.PNG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/04/fading-uitextures-opacity-within-panel.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-3626673571657228475</guid><pubDate>Fri, 12 Apr 2013 15:24:00 +0000</pubDate><atom:updated>2013-04-12T17:24:25.371+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unity 3D</category><title>Clearing the browser cache of the Unity Web Player</title><description>For my current Unity web project, I am downloading scenes as AssetBundles hosted on my server, and now I have found myself constantly uploading the updated scenes whenever I make these (constant) changes.&lt;br /&gt;
&lt;br /&gt;
Problem is, when I try the game online to see how the downloaded scene looks, my main game file which is downloading these scenes keeps on using the cached scenes from before.&lt;br /&gt;
&lt;br /&gt;
That's because I am downloading the AssetBundles using &lt;a href="http://docs.unity3d.com/Documentation/ScriptReference/WWW.LoadFromCacheOrDownload.html" target="_blank"&gt;WWW.LoadFromCacheOrDownload&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
To clear the cache, just head on over to &lt;a href="http://unity3d.com/webplayer_setup/setup-3.x/" target="_blank"&gt;http://unity3d.com/webplayer_setup/setup-3.x/&lt;/a&gt; where you will see something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;a href="http://1.bp.blogspot.com/-XKOGw7Jqgnw/UWgl-iEek1I/AAAAAAAABSM/OABxf0O3Cy8/s1600/WebPlayerSettings.PNG" imageanchor="1" &gt;&lt;img border="0" src="http://1.bp.blogspot.com/-XKOGw7Jqgnw/UWgl-iEek1I/AAAAAAAABSM/OABxf0O3Cy8/s320/WebPlayerSettings.PNG" /&gt;&lt;/a&gt;&lt;/center&gt;&lt;br /&gt;
And from there, you can simply delete the cache, or even make sure that while you're still working on the game, don't keep a cache at all.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Gong",
  name : "The Isle Of Everywhere",
  link : 'http://grooveshark.com/s/The+Isle+Of+Everywhere/3gqxqq?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/16Jhwy0sDCo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/16Jhwy0sDCo/clearing-browser-cache-of-unity-web.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-XKOGw7Jqgnw/UWgl-iEek1I/AAAAAAAABSM/OABxf0O3Cy8/s72-c/WebPlayerSettings.PNG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/04/clearing-browser-cache-of-unity-web.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-6026207097783557381</guid><pubDate>Thu, 11 Apr 2013 15:26:00 +0000</pubDate><atom:updated>2013-04-11T21:20:41.402+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unity 3D</category><category domain="http://www.blogger.com/atom/ns#">C#</category><category domain="http://www.blogger.com/atom/ns#">Kongregate</category><title>Showing a Kongregate user's avatar image in a Unity 3D game</title><description>I decided that the game I'm currently working on is going to be tied to Kongregate, and since I'm doing that, might as well try to use as many features of the API as I can.&lt;br /&gt;
&lt;br /&gt;
One of the features I'm trying to integrate in my game is the ability to render a Kongregate user's avatar in the game itself.  For this tutorial, I will be using Kongregate's JSON REST API to fetch user data.&lt;br /&gt;
&lt;br /&gt;
After skimming through the Kongregate API documentation, I found the page about the &lt;a href="http://developers.kongregate.com/docs/rest/user-info" target="_blank"&gt;user-info&lt;/a&gt; REST call.&lt;br /&gt;
&lt;br /&gt;
The url for a user-info call is formatted as follows: http://www.kongregate.com/api/user_info.json?username=&lt;b&gt;&amp;lt;username&amp;gt;&lt;/b&gt; where username is obviously the username for the user account you're searching for.&lt;br /&gt;
&lt;br /&gt;
This provides you with a response such as the following:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;{
   "friends":[],
   "muted_users":[],
   "friend_ids":[],
   "muted_user_ids":[],
   "user_id":3590097,
   "username":"agnt666",
   "private":false,
   "page_num":1,
   "num_pages":1,
   "success":true,
   "user_vars":{
      "username":"agnt666",
      "level":3,
      "points":165,
      "avatar_url":"http://cdn4.kongcdn.com/assets/resize-image/50x50/assets/avatars/defaults/ant.png",
      "chat_avatar_url":"http://cdn4.kongcdn.com/assets/resize-image/16x16/assets/avatars/defaults/ant.png",
      "developer":false,
      "moderator":false,
      "admin":false,
      "gender":"Male",
      "age":25,
      "game_title":"Typocalypse 3D",
      "game_url":"http://www.kongregate.com/games/agnt666/typocalypse-3d"
   }
}
&lt;/pre&gt;&lt;br /&gt;
And the above response provides me with what I needed; the avatar url:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;"avatar_url" : "http://cdn4.kongcdn.com/assets/resize-image/50x50/assets/avatars/defaults/ant.png"
&lt;/pre&gt;&lt;br /&gt;
&lt;center&gt;&lt;img src="http://cdn4.kongcdn.com/assets/resize-image/50x50/assets/avatars/defaults/ant.png" /&gt;&lt;/center&gt;&lt;br /&gt;
&lt;h5&gt;Getting the avatar url in C#&lt;/h5&gt;Getting the username of the current Kongregate logged-in user is beyond the scope of this post (TODO: maybe in another one), so for this code I'm assuming you already have it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;public UITexture kongregateTexture;

IEnumerator FetchUserDetails(string username) {
    // Build the url for the REST call
    var detailsUrl = String.Format("/api/user_info.json?username={0}", username);

    // Do the call 
    var www = new WWW(detailsUrl);

    // ...and wait until we get back a response
    yield return www;

    // Get the JSON response text
    var responseText = www.text;

    // Deserialize it into a statically typed object to read to be able to read it's contents
    // For this demo, I'm using this JSON deserializer script: https://raw.github.com/gist/1411710/MiniJSON.cs
    var deserialized = MiniJSON.Json.Deserialize(responseText) as Dictionary&amp;lt;string,object&amp;gt;;

    //// TODO: a null check would be nice here

    // Get the user_vars object
    var userVars = deserialized["user_vars"] as Dictionary&amp;lt;string,object&amp;gt;;

    // Read the avatar url
    var avatar_url = user_vars["avatar_url"].ToString(); // http://cdn4.kongcdn.com/assets/resize-image/50x50/assets/avatars/defaults/ant.png

    //// Do the same routine to fetch the image
    var wwwAvatar = new WWW(avatar_url);
    yield return wwwAvatar;

    // And now we have the texture!
    Texture2D avatarTexture = wwwAvatar.texture;

    // Render the texture (I'm using an NGUI SimpleTexture widget)
    kongregateTexture.mainTexture = avatarTexture;
}
&lt;/pre&gt;&lt;br /&gt;
And this is the result &lt;i&gt;with a shitty border I made around it&lt;/i&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;a href="http://2.bp.blogspot.com/-uALmCRNpiJ0/UWaptuSSSjI/AAAAAAAABRY/1yL_xCQ7CWg/s1600/AntAvatar.PNG" imageanchor="1"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-uALmCRNpiJ0/UWaptuSSSjI/AAAAAAAABRY/1yL_xCQ7CWg/s1600/AntAvatar.PNG" /&gt;&lt;/a&gt;&lt;/center&gt;&lt;br /&gt;
&lt;h5&gt;Possible exception: &lt;span style="color: red;"&gt;Security Exception: No valid crossdomain policy available to allow access&lt;/span&gt;&lt;/h5&gt;Notice that in the above code, to build the url of the REST call, I used Kongregate's relative url: "/api/user-info.json...".&lt;br /&gt;
&lt;br /&gt;
If I had used this instead &lt;span style="font-family: Courier New, Courier, monospace;"&gt;String.Format("http://www.kongregate.com/api/user_info.json?username={0}", username);&lt;/span&gt;, I would have received this exception when running the game and looking at the debug log (console):&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-DG7TkS-4rQQ/UWad2i9l6EI/AAAAAAAABRI/6GfuVo143M4/s1600/SecurityException.PNG" /&gt;&lt;/center&gt;&lt;br /&gt;
This error is thrown when reading the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;text&lt;/span&gt; field of the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;WWW&lt;/span&gt; instance (&lt;span style="font-family: Courier New, Courier, monospace;"&gt;get_text&lt;/span&gt;).&lt;br /&gt;
&lt;br /&gt;
So be very careful about that when doing the REST calls; always use relative urls! &lt;i&gt;...and this of course works because the game is hosted on http://www.kongregate.com.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Gong",
  name : "Fohat Digs Holes in Space",
  link : 'http://grooveshark.com/s/Fohat+Digs+Holes+In+Space/3gqECc?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/7E6DLq_pi0s" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/7E6DLq_pi0s/showing-kongregate-users-avatar-image.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-uALmCRNpiJ0/UWaptuSSSjI/AAAAAAAABRY/1yL_xCQ7CWg/s72-c/AntAvatar.PNG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/04/showing-kongregate-users-avatar-image.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-4845959405375034691</guid><pubDate>Mon, 08 Apr 2013 15:25:00 +0000</pubDate><atom:updated>2013-04-08T17:30:12.602+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">T4</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Auto-Generating generic Tuple classes with T4 in C#</title><description>And yet again, here's another perfect example for using the T4 code generation toolkit in Visual Studio.&lt;br /&gt;
&lt;br /&gt;
For a recent project, I wanted to use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.tuple.aspx" target="_blank"&gt;Tuple&lt;/a&gt; class provided in the .NET Framework...problem was, I was stuck with .NET 3.5 and the Tuple class was introduced in version 4 of the framework.  So, instead of writing a single Tuple class by hand (which would have been very trivial to do), I decided to write a T4 template which would generate all the Tuple classes I needed automatically.&lt;br /&gt;
&lt;br /&gt;
The template itself is very trivial and the number of Tuple classes it generates is dependent on the value that I specify in the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;max&lt;/span&gt; variable at the top of the template.&lt;br /&gt;
&lt;br /&gt;
For each of the Tuple classes the template generates, it creates a contructor which accepts all of the properties that the tuple will contain and also the actual properties that will be accessed from the outside.&lt;br /&gt;
&lt;br /&gt;
Here's an example of what the template autogenerates for me when using &lt;span style="font-family: Courier New, Courier, monospace;"&gt;max = 3&lt;/span&gt; (granted, the first class it generates is a bit pointless but that can be easily omitted from within the template if it bothers you):&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;namespace FunWithTuples
{
    /// &amp;lt;summary&amp;gt;
    /// Represents a 1-tuple
    /// &amp;lt;/summary&amp;gt;
    public class Tuple&amp;lt;T1&amp;gt;
    {
        public T1 Item1 { get; set; }

        public Tuple(T1 item1) 
        {
            Item1 = item1;
        }
    }

    /// &amp;lt;summary&amp;gt;
    /// Represents a 2-tuple
    /// &amp;lt;/summary&amp;gt;
    public class Tuple&amp;lt;T1, T2&amp;gt;
    {
        public T1 Item1 { get; set; }
        public T2 Item2 { get; set; }

        public Tuple(T1 item1, T2 item2) 
        {
            Item1 = item1;
            Item2 = item2;
        }
    }

    /// &amp;lt;summary&amp;gt;
    /// Represents a 3-tuple
    /// &amp;lt;/summary&amp;gt;
    public class Tuple&amp;lt;T1, T2, T3&amp;gt;
    {
        public T1 Item1 { get; set; }
        public T2 Item2 { get; set; }
        public T3 Item3 { get; set; }

        public Tuple(T1 item1, T2 item2, T3 item3) 
        {
            Item1 = item1;
            Item2 = item2;
            Item3 = item3;
        }
    }
}
&lt;/pre&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
And this is the full T4 Template:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;&lt;#@ template debug="false" hostspecific="false" language="C#" #&gt;
&lt;#@ assembly name="System.Core" #&gt;
&lt;#@ import namespace="System.Linq" #&gt;
&lt;#@ import namespace="System.Text" #&gt;
&lt;#@ import namespace="System.Collections.Generic" #&gt;
&lt;#@ output extension=".cs" #&gt;

&lt;#
var max = 3; // The total number of classes to generate
#&gt;
namespace FunWithTuples
{
&lt;#
max += 1;
for (var i = 1; i &lt; max; ++i) {
#&gt;
    /// &lt;summary&gt;
    /// Represents a &lt;#=i#&gt;-tuple
    /// &lt;/summary&gt;
    public class Tuple&lt;#=GetGenericTypesSignature(i)#&gt;
    {
&lt;#
for (var j = 1; j &lt; i + 1; ++j) {
#&gt;
        public T&lt;#=j#&gt; Item&lt;#=j#&gt; { get; set; }
&lt;#
}
#&gt;

        public Tuple(&lt;#=GetConstructorArguments(i)#&gt;) 
        {
&lt;#
for (var j = 1; j &lt; i + 1; ++j) {
#&gt;
            Item&lt;#=j#&gt; = item&lt;#=j#&gt;;
&lt;#
}
#&gt;
        }
    }

&lt;#
}
#&gt;
}
&lt;#+
    public string GetGenericTypesSignature(int total) {
        return String.Format("&lt;{0}&gt;", String.Join(", ", Enumerable.Range(1, total).Select(n =&gt; "T" + n).ToArray()));
    }

    public string GetConstructorArguments(int total) {
        return String.Join(", ", Enumerable.Range(1, total).Select(n =&gt; String.Format("T{0} item{0}", n)).ToArray());
    }
#&gt;

&lt;/pre&gt;&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Falcon's Flying Circus",
  name : "On the side of the road",
  link : 'http://grooveshark.com/s/On+The+Side+Of+The+Road/2qW7G9?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/qJGN0DHK9o8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/qJGN0DHK9o8/auto-generating-generic-tuple-classes.html</link><author>noreply@blogger.com (Andreas Grech)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/04/auto-generating-generic-tuple-classes.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-3922856598899264686</guid><pubDate>Fri, 05 Apr 2013 10:17:00 +0000</pubDate><atom:updated>2013-04-05T12:17:33.701+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">LaTeX</category><title>Absolute value bars for fractions in LaTeX</title><description>The simplest way on how to include |absolute value| bars in LaTeX is to use the this notation: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;\left|&lt;/span&gt; and &lt;span style="font-family: Courier New, Courier, monospace;"&gt;\right|&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
Here's an example: &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;y = &lt;b&gt;\left|&lt;/b&gt;\frac{1}{x}&lt;b&gt;\right|&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
And that would be rendered as:&lt;br /&gt;
&lt;br /&gt;
&lt;img src="http://latex.codecogs.com/gif.latex?y=\left|\frac{1}{x}\right|" title="y=\left|\frac{1}{x}\right|" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Herbie Mann",
  name : "Bitch",
  link : 'http://grooveshark.com/s/Bitch/3AVJOw?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/s8S4UVmZjrQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/s8S4UVmZjrQ/absolute-value-bars-for-fractions-in.html</link><author>noreply@blogger.com (Andreas Grech)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/04/absolute-value-bars-for-fractions-in.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-4619887384037024724</guid><pubDate>Fri, 05 Apr 2013 10:01:00 +0000</pubDate><atom:updated>2013-04-05T12:02:08.403+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CIL</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Common CIL incantations</title><description>Since I recently decided to try my hand at CIL by writing assemblies by hand, I'm now writing this post where I will list down a couple of basic CIL routines.&lt;br /&gt;
&lt;br /&gt;
I am of course primarily writing this post for myself so that future me will have a reference if he (I?) decides to do something with it again.&lt;br /&gt;
&lt;br /&gt;
&lt;h5&gt;&lt;i&gt;Hello World&lt;/i&gt;&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;.assembly helloworld {}
.assembly extern mscorlib {}

.method public static void Main () {
    .entrypoint
    ldstr "Hello World!"
    call void [mscorlib]System.Console::WriteLine(string)
    ret
}&lt;/pre&gt;&lt;h5&gt;Including external assemblies&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;.assembly extern mscorlib {}
&lt;/pre&gt;&lt;h5&gt;Pushing a 32 bit integer constant to the stack&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;ldc.i4 42
&lt;/pre&gt;&lt;h5&gt;Pushing a 64 bit integer constant to the stack&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;ldc.i8 829190
&lt;/pre&gt;&lt;h5&gt;Pushing a string constant to the stack&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;ldstr "Andreas"
&lt;/pre&gt;&lt;h5&gt;Declaring and loading unnamed variables (popping elements off the stack)&lt;br /&gt;
&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;.locals init (int32, string) // init specifies that the variables must be initialized to the default types
ldc.i4 42
stloc.0 // storing the 32 bit integer at position 0 by popping it off the stack
ldstr "CIL"
stloc.1
&lt;/pre&gt;&lt;h5&gt;Defining a class&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;.class MathHelpers extends [mscorlib]System.Object {
    .method void .ctor() { // Constructor
        ldarg.0
        call instance void [mscorlib]System.Object::.ctor()
    }

    .method public instance int32 Inc(int32) { // Instance method; the instance keyword can be omitted
        ldarg.0
        ldc.i4.1
        add
        ret
    }
    
    .method public static int32 Add(int32 n, int32 m) {  // Static method
        ldarg n
        ldarg m
        add
        ret
    }
}&lt;/pre&gt;&lt;h5&gt;Invoking methods from external assemblies&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;.assembly extern mscorlib {}

// ...

ldstr "Writelining"
call void [mscorlib]System.Console::WriteLine(string)&lt;/pre&gt;&lt;h5&gt;Invoking an instance method&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;.locals init (class MathHelpers mh)

newobj instance void MathHelpers::.ctor()
stloc mh

call instance void MathHelpers::Show()
&lt;/pre&gt;&lt;h5&gt;Invoking a static method&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;call void MyNamespace.MyClass::StaticMethod()&lt;/pre&gt;&lt;h5&gt;Using an array&lt;/h5&gt;&lt;pre class="sunlight-highlight-msil" name="code"&gt;.locals init (string[] names);

.ldc.i4.4 // the int32 constant that's used for the size of the array: new string[4]
newarr string
stloc names

// Add element 0
ldloc names
ldc.i4.0 // array index
ldstr "Dreas"
stelem.ref

// Add element 1
ldloc names
ldc.i4.1 // array index
ldstr "John"
stelem.ref&lt;/pre&gt;&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Falcon's Flying Circus",
  name : "No Returns",
  link : 'http://grooveshark.com/s/No+Returns/2qW91C?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/z_02v5XNmZ4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/z_02v5XNmZ4/common-cil-incantations.html</link><author>noreply@blogger.com (Andreas Grech)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/04/common-cil-incantations.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-5752784885250076564</guid><pubDate>Fri, 05 Apr 2013 09:27:00 +0000</pubDate><atom:updated>2013-04-05T11:27:12.169+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C#</category><category domain="http://www.blogger.com/atom/ns#">Visual Studio</category><title>Adding a line break for IntelliSense in XML documentation comments</title><description>This is a quick post on how to insert a line break for XML documentation comments in C#.&lt;br /&gt;
&lt;br /&gt;
The easiest way I've found is to use a &lt;a href="http://www.theasciicode.com.ar/extended-ascii-code/non-breaking-space-no-break-space-ascii-code-255.html" target="_blank"&gt;non breaking space&lt;/a&gt; (ASCII code 255) in combination with the paragraph tags: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&amp;lt;para&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
To insert the non breaking space, press ALT + 255 (from the numeric keypad).&lt;br /&gt;
&lt;br /&gt;
Here's an example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;/// &amp;lt;summary&amp;gt;
/// &amp;lt;para&amp;gt;This method does everything.&amp;lt;/para&amp;gt;
/// &amp;lt;para&amp;gt;&amp;nbsp;&amp;lt;/para&amp;gt;
/// &amp;lt;para&amp;gt;...and I mean everything.&amp;lt;/para&amp;gt;
/// &amp;lt;/summary&amp;gt;
private void DoEverything() {
&lt;/pre&gt;&lt;br /&gt;
And this is how it looks in IntelliSense:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://1.bp.blogspot.com/-r1Gz_y-CmN0/UV6X5aPnJtI/AAAAAAAABQ4/2ZGGfPOcN7g/s1600/IntelliSenseBreakingLine.png" imageanchor="1"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-r1Gz_y-CmN0/UV6X5aPnJtI/AAAAAAAABQ4/2ZGGfPOcN7g/s320/IntelliSenseBreakingLine.png" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Falcon's Flying Circus",
  name : "Living Free",
  link : 'http://grooveshark.com/s/Living+Free/2qWaEI?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/Oh7VTmb_Hws" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/Oh7VTmb_Hws/adding-line-break-for-intellisense-in.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-r1Gz_y-CmN0/UV6X5aPnJtI/AAAAAAAABQ4/2ZGGfPOcN7g/s72-c/IntelliSenseBreakingLine.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/04/adding-line-break-for-intellisense-in.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-178369281218745809</guid><pubDate>Tue, 26 Feb 2013 15:08:00 +0000</pubDate><atom:updated>2013-02-26T16:08:04.539+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Vim</category><title>Preventing Vim from automatically inserting line breaks</title><description>I recently had a very frustrating issue with Vim where it was inserting a line break automatically for me whenever a line exceeded the 80 character mark.&lt;br /&gt;
&lt;br /&gt;
After some searching around, I realized that I had &lt;span style="font-family: Courier New, Courier, monospace;"&gt;textwidth&lt;/span&gt; (&lt;span style="font-family: Courier New, Courier, monospace;"&gt;tw&lt;/span&gt;) set to 79 in my &lt;span style="font-family: Courier New, Courier, monospace;"&gt;_vimrc&lt;/span&gt; file.  From the documentation:&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;blockquote&gt;textwidth.&lt;br /&gt;
Maximum width of text that is being inserted.  A longer line will be broken after white space to get this width.  A zero value disables this.&lt;/blockquote&gt;&lt;/center&gt;&lt;br /&gt;
So as you can see from the documentation, fixing it was very trivial: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;set tw = 0&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Jimi Hendrix",
  name : "Love Or Confusion",
  link : 'http://grooveshark.com/s/Love+Or+Confusion/38HQ54?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/mYuldeOHVwM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/mYuldeOHVwM/preventing-vim-from-automatically.html</link><author>noreply@blogger.com (Andreas Grech)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2013/02/preventing-vim-from-automatically.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-1896368516371084412</guid><pubDate>Wed, 19 Dec 2012 13:18:00 +0000</pubDate><atom:updated>2012-12-19T14:31:19.340+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">T4</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Auto-Generating EventArgs classes with T4</title><description>T4 is a code generator built into Visual Studio.  In this post, I will present a T4 template file which I've used to help auto-generate Event Arguments classes in C#.&lt;br /&gt;
&lt;br /&gt;
Normally, event arguments classes always follow the same structure.  You typically have a set of public properties in a class which extends &lt;a href="http://msdn.microsoft.com/en-us/library/system.eventargs.aspx" target="_blank"&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;EventArgs&lt;/span&gt;&lt;/a&gt; and a constructor which takes in the same number of formal arguments.&lt;br /&gt;
&lt;br /&gt;
Here's a typical event arguments class:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;public class OperationCompletedEventArgs : EventArgs
{
    public bool Result { get; private set; }

    public OperationCompletedEventArgs(bool result)
    {
        Result = result;
    }
}
&lt;/pre&gt;&lt;br /&gt;
Given the repetitive nature of how typical event arguments classes are structured, they make a perfect fit for an example of code generation.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
The intent of my template file for generating event arguments classes is to be able to define a declaration of what classes I want to be generated and then let the template generate those classes for me.&lt;br /&gt;
&lt;br /&gt;
The declaration part of the template looks something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;var collection = new List&amp;lt;EventArgumentsRepresentation&amp;gt;
                 {
                     new EventArgumentsRepresentation("OperationCompleted")
                     {
                         Members = new Dictionary&amp;lt;string, string&amp;gt; { 
                             {"Result", "bool"}
                         }
                     },
                     new EventArgumentsRepresentation("GenericAndPartial")
                         {
                             AccessModifier = "internal",
                             IsPartial = true,
                             GenericTypes = new List&amp;lt;string&amp;gt;() { "T", "TK"},
                             Members = new Dictionary&amp;lt;string, string&amp;gt; { 
                                 {"GenericMember1", "T"},
                                 {"GenericMember2", "TK"}
                             }
                         }
                 };

&lt;/pre&gt;&lt;br /&gt;
Given the above structure, the template would then generate the following two classes for me:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;public class OperationCompletedEventArgs : EventArgs
{
    public bool Result { get; private set; }

    public OperationCompletedEventArgs(bool result) 
    {
        Result = result;
    }
}

internal partial class GenericAndPartialEventArgs&amp;lt;T, TK&amp;gt; : EventArgs
{
    public T GenericMember1 { get; private set; }
    public TK GenericMember2 { get; private set; }

    public GenericAndPartialEventArgs(T genericMember1, TK genericMember2) 
    {
        GenericMember1 = genericMember1;
        GenericMember2 = genericMember2;
    }
}
&lt;/pre&gt;&lt;br /&gt;
The next I would want a new event arguments class, I would just add a new instance of &lt;span style="font-family: Courier New, Courier, monospace;"&gt;EventArgumentsRepresentation&lt;/span&gt; to the list and let the template do the work.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
And now, for the entire template file.  If you want to use it for your own projects, create a new "Text Template" (.tt file) in Visual Studio and copy the below template into it.  Once you then save the template file, the classes will be generated into a .cs file.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;&amp;lt;#@ template debug=&amp;quot;false&amp;quot; hostspecific=&amp;quot;false&amp;quot; language=&amp;quot;C#&amp;quot; #&amp;gt;
&amp;lt;#@ assembly name=&amp;quot;System.Core&amp;quot; #&amp;gt;
&amp;lt;#@ import namespace=&amp;quot;System.Linq&amp;quot; #&amp;gt;
&amp;lt;#@ import namespace=&amp;quot;System.Collections.Generic&amp;quot; #&amp;gt;
&amp;lt;#@ output extension=&amp;quot;.cs&amp;quot; #&amp;gt;

&amp;lt;#
var namespaceName = &amp;quot;EventArgumentsGenerationDemo&amp;quot;;
var usings = new List&amp;lt;string&amp;gt; {
    &amp;quot;System&amp;quot;,
};

var collection = new List&amp;lt;EventArgumentsRepresentation&amp;gt;
                 {
                     new EventArgumentsRepresentation(&amp;quot;OperationCompleted&amp;quot;)
                     {
                         Members = new Dictionary&amp;lt;string, string&amp;gt; { 
                             {&amp;quot;Result&amp;quot;, &amp;quot;bool&amp;quot;},
                         },
                     },
                     new EventArgumentsRepresentation(&amp;quot;GenericAndPartial&amp;quot;)
                         {
                             AccessModifier = &amp;quot;internal&amp;quot;,
                             IsPartial = true,
                             GenericTypes = new List&amp;lt;string&amp;gt;() { &amp;quot;T&amp;quot;, &amp;quot;TK&amp;quot;},
                             Members = new Dictionary&amp;lt;string, string&amp;gt; { 
                                 {&amp;quot;GenericMember1&amp;quot;, &amp;quot;T&amp;quot;},
                                 {&amp;quot;GenericMember2&amp;quot;, &amp;quot;TK&amp;quot;},
                             },
                         },
                     
                 };
#&amp;gt;
namespace &amp;lt;#=namespaceName#&amp;gt;
{
&amp;lt;#
foreach (var usingDirective in usings) {
#&amp;gt;
    using &amp;lt;#=usingDirective#&amp;gt;;
&amp;lt;#
}
#&amp;gt;

    /*
     * This file is autogenerated.  
     *
     * If you want to edit anything from here, use the EventArgumentsClasses.tt file.
     */

&amp;lt;#
foreach (var eventArgsRepresentation in collection) {
#&amp;gt;
    &amp;lt;#=eventArgsRepresentation.ClassSignature#&amp;gt;
    {
&amp;lt;#
foreach (var member in eventArgsRepresentation.Members) {
#&amp;gt;
        public &amp;lt;#=member.Value#&amp;gt; &amp;lt;#=EventArgumentsRepresentation.ToPascalCase(member.Key)#&amp;gt; { get; private set; }
&amp;lt;#
}
#&amp;gt;

        public &amp;lt;#=eventArgsRepresentation.ClassName#&amp;gt;(&amp;lt;#=eventArgsRepresentation.GetConstructorArguments()#&amp;gt;) 
        {
&amp;lt;#
foreach (var member in eventArgsRepresentation.Members) {
#&amp;gt;
            &amp;lt;#=EventArgumentsRepresentation.ToPascalCase(member.Key)#&amp;gt; = &amp;lt;#=EventArgumentsRepresentation.ToCamelCase(member.Key)#&amp;gt;;
&amp;lt;#
}
#&amp;gt;
        }
    }

&amp;lt;#
}
#&amp;gt;
}

&amp;lt;#+
internal class EventArgumentsRepresentation
{
    public string ClassName { get; private set; }

    public string AccessModifier { get; set; }
    public bool IsPartial { get; set; }
    public List&amp;lt;string&amp;gt; GenericTypes {get; set;}
    public Dictionary&amp;lt;string, string&amp;gt; Members { get; set; }
    public string ClassSignature {
        get {
            return String.Format(&amp;quot;{0} {1}class {2}{3} : EventArgs&amp;quot;, AccessModifier, IsPartial ? &amp;quot;partial &amp;quot; : &amp;quot;&amp;quot;, ClassName, GetGenericTypesSignature());
        }
    }

    public EventArgumentsRepresentation(string name)
    {
        ClassName = String.Format(&amp;quot;{0}EventArgs&amp;quot;, ToPascalCase(name));
        AccessModifier = &amp;quot;public&amp;quot;;
        GenericTypes = new List&amp;lt;string&amp;gt;();
        Members = new Dictionary&amp;lt;string, string&amp;gt;();
    }

    public string GetGenericTypesSignature() {
        if (GenericTypes.Count &amp;lt; 1) {
            return &amp;quot;&amp;quot;;
        }

        return String.Format(&amp;quot;&amp;lt;{0}&amp;gt;&amp;quot;, string.Join(&amp;quot;, &amp;quot;, GenericTypes.ToArray()));
    }

    public string GetConstructorArguments() {
        if (Members == null || Members.Count &amp;lt; 1) {
            return &amp;quot;&amp;quot;;
        }

        return String.Join(&amp;quot;, &amp;quot;, Members.Select(k =&amp;gt; String.Format(&amp;quot;{0} {1}&amp;quot;, k.Value, ToCamelCase(k.Key))).ToArray());
    }

    public static string ToPascalCase(string text) {
        return char.ToUpper(text[0]) + text.Substring(1);
    }

    public static string ToCamelCase(string text) {
        return char.ToLower(text[0]) + text.Substring(1);
    }
}
#&amp;gt;
&lt;/pre&gt;&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "The Jimi Hendrix Experience",
  name : "Wait Until Tomorrow",
  link : 'http://grooveshark.com/s/Wait+Until+Tomorrow/1MO3hd?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/0beR6mIB3JI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/0beR6mIB3JI/auto-generating-eventargs-classes-with.html</link><author>noreply@blogger.com (Andreas Grech)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/12/auto-generating-eventargs-classes-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-5461951607826933951</guid><pubDate>Thu, 01 Nov 2012 10:42:00 +0000</pubDate><atom:updated>2012-11-01T11:42:11.468+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Visual Studio</category><title>F5 not building before running in Visual Studio 2012</title><description>I recently started using Visual Studio 2012 but got quite annoyed after I realized that the F5 shortcut was not building the projects in my solution before actually running them.  Because of this, I was ending up with inconsistencies between the running application the source code.&lt;br /&gt;
&lt;br /&gt;
Long story short, here's how to fix this.&lt;br /&gt;
&lt;br /&gt;
Open the Options box from &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Debug &amp;gt; Options and Settings&lt;/span&gt;, then from &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Projects and Solutions &amp;gt; Build and Run&lt;/span&gt;, make sure that the dropdown for &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;b&gt;On Run, when projects are out of date:&lt;/b&gt;&lt;/span&gt;, the "&lt;span style="font-family: Courier New, Courier, monospace;"&gt;Always build&lt;/span&gt;" value is set:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-67TaAkZW280/UJJQd99gQ7I/AAAAAAAABPo/5hryWdw6EIw/s1600/VS_AlwaysBuild.png" /&gt;&lt;/div&gt;&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Black Widow",
  name : "Come to the Sabbat",
  link : 'http://grooveshark.com/s/Come+To+The+Sabbat/2BBH5P?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/HCgmy56W8kI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/HCgmy56W8kI/f5-not-building-before-running-in.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-67TaAkZW280/UJJQd99gQ7I/AAAAAAAABPo/5hryWdw6EIw/s72-c/VS_AlwaysBuild.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/11/f5-not-building-before-running-in.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-8740251084861842638</guid><pubDate>Thu, 25 Oct 2012 20:57:00 +0000</pubDate><atom:updated>2012-10-25T22:57:22.766+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CIL</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Fixing "Not a debug ModuleBuilder." thrown by SetLocalSymInfo</title><description>This was the first problem I had when I started playing with CIL and emitting CIL code with C#'s Emit; an &lt;span style="font-family: Courier New, Courier, monospace;"&gt;InvalidOperationException: Not a debug ModuleBuilder&lt;/span&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-taKCoZjGpt4/UImjyhPduNI/AAAAAAAABPQ/kIp1I7r_QSs/s1600/NotADebugModuleBuilder.png" /&gt;&lt;/div&gt;&lt;br /&gt;
The reason this was happening was because of this line:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;ModuleBuilder mb = ab.DefineDynamicModule(an.Name, "MainModule");
&lt;/pre&gt;&lt;br /&gt;
The &lt;span style="font-family: Courier New, Courier, monospace;"&gt;DefineDynamicModule&lt;/span&gt; has an overload which lets you specify whether symbol information should be emitted, and by default it's &lt;span style="font-family: Courier New, Courier, monospace;"&gt;false&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
By setting this &lt;span style="font-family: Courier New, Courier, monospace;"&gt;emitSymbolInfo&lt;/span&gt; argument to &lt;span style="font-family: Courier New, Courier, monospace;"&gt;true&lt;/span&gt;, it will also emit the &lt;a href="http://en.wikipedia.org/wiki/Program_database" target="_blank"&gt;pdb file&lt;/a&gt; for your assembly.&lt;br /&gt;
&lt;br /&gt;
So I passed in the argument and my problem was solved:&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;ModuleBuilder mb = ab.DefineDynamicModule(an.Name, "MainModule", true);
&lt;/pre&gt;&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Virus",
  name : "Revelation",
  link : 'http://grooveshark.com/s/Revelation/3UtK7E?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/ZTG382i-bFo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/ZTG382i-bFo/fixing-not-debug-modulebuilder-thrown.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-taKCoZjGpt4/UImjyhPduNI/AAAAAAAABPQ/kIp1I7r_QSs/s72-c/NotADebugModuleBuilder.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/10/fixing-not-debug-modulebuilder-thrown.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-6511482726647365133</guid><pubDate>Mon, 15 Oct 2012 18:00:00 +0000</pubDate><atom:updated>2012-10-15T20:00:37.889+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Projects</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Taking a screenshot of an application in C#</title><description>Recently I needed a way to take screenshots of applications programmatically in C# and I wrote this small application to accomplish that task for me.&lt;br /&gt;
&lt;br /&gt;
You can find the source here: &lt;a href="https://github.com/dreasgrech/Screenshotter" target="_blank"&gt;https://github.com/dreasgrech/Screenshotter&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
The only requirement I had was that I supply the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;pId&lt;/span&gt; (process ID) and the application takes a screenshot of that process for me...but the application can be very easily extended to accept a process name or anything that can identify a process.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-vLpVBPmpb2c/UHxOIqTMh5I/AAAAAAAABO4/xBCnL3jLJp0/s1600/screenshotter.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-vLpVBPmpb2c/UHxOIqTMh5I/AAAAAAAABO4/xBCnL3jLJp0/s1600/screenshotter.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Steel Mill",
  name : "Green Eyed God",
  link : 'http://grooveshark.com/s/Green+Eyed+God/3ZrSyg?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/xVcIKQ4pIgg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/xVcIKQ4pIgg/taking-screenshot-of-application-in-c.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-vLpVBPmpb2c/UHxOIqTMh5I/AAAAAAAABO4/xBCnL3jLJp0/s72-c/screenshotter.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/10/taking-screenshot-of-application-in-c.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-2626190590940805851</guid><pubDate>Sun, 14 Oct 2012 14:45:00 +0000</pubDate><atom:updated>2012-10-20T20:15:58.534+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CIL</category><category domain="http://www.blogger.com/atom/ns#">C#</category><category domain="http://www.blogger.com/atom/ns#">Cracking</category><title>Cracking .NET applications -  a simple tutorial</title><description>For this tutorial, I will be making use of &lt;a href="http://www.reflector.net/" target="_blank"&gt;Reflector&lt;/a&gt; and a plugin for Reflector called &lt;a href="http://reflexil.net/" target="_blank"&gt;Reflexil&lt;/a&gt;, and also &lt;a href="http://www.digitalbodyguard.com/graywolf.html" target="_blank"&gt;Gray.Wolf&lt;/a&gt; to partially view unobfuscated C# code. &lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Patching the CIL with Reflexil&lt;/h4&gt;Here is a very trivial example of how a simple application can validate a license key:&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;static void Main(string[] args)
{
    var invalidKey = "48011147";

    if (LicenseManager.Verify(invalidKey))
    {
        Console.WriteLine("Thank you for buying our product!");
    } 
    else
    {
        Console.WriteLine("Invalid license key; continue evaluation.");
    }
}
&lt;/pre&gt;&lt;pre class="sunlight-highlight-csharp" name="code"&gt;static class LicenseManager
{
    private static string validKey = "112358";

    public static bool Verify(string key)
    {
        return key == validKey;
    }
}
&lt;/pre&gt;From the code, it's fairly easy to see that to bypass the license key validation logic in our application, we simply need to modify the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;LicenseManager.Verify&lt;/span&gt; method to just return true rather than check for the valid license key.&lt;br /&gt;
&lt;br /&gt;
Running the unmodified application yields the following, since the equality check obviously fails:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-JgxBeK_pGbU/UHlnFAEBeWI/AAAAAAAABKI/PFrN1Sqe42Y/s1600/Simple_1.png" /&gt;&lt;/div&gt;&lt;br /&gt;
So now let's start patching the application by first doing some reconnaissance.  Fire up Reflector, add our compiled exe file and navigate to the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;LicenseManager.Verify&lt;/span&gt; method:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-FofnyVNnqA8/UHln_ErBA-I/AAAAAAAABKU/TihLkgWbeHM/s1600/Simple_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-FofnyVNnqA8/UHln_ErBA-I/AAAAAAAABKU/TihLkgWbeHM/s1600/Simple_2.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Now there are couple of ways we can modify the code with Reflexil...but let's start with the harder way, by modifying the &lt;a href="http://en.wikipedia.org/wiki/Common_Intermediate_Language" target="_blank"&gt;CIL&lt;/a&gt; code directly.&lt;br /&gt;
&lt;br /&gt;
If you look at the current CIL, we see that we have four operations: &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;a href="http://www.dotnetperls.com/ldarg" target="_blank"&gt;ldarg.0&lt;/a&gt;&lt;/span&gt;, &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;a href="http://www.dotnetperls.com/ldsfld" target="_blank"&gt;ldsfld&lt;/a&gt;&lt;/span&gt;, &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;a href="http://www.dotnetperls.com/call" target="_blank"&gt;call&lt;/a&gt;&lt;/span&gt; and &lt;span style="font-family: Courier New, Courier, monospace;"&gt;&lt;a href="http://www.dotnetperls.com/ret" target="_blank"&gt;ret&lt;/a&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-DPAdEEJYk9Q/UHltyv2ERTI/AAAAAAAABKo/VfesWe_NU4s/s1600/Simple_3.png" /&gt;&lt;/div&gt;&lt;br /&gt;
Here's what's going on:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;ldarg.0&lt;/span&gt; loads and pushes the actual argument at position 0 (&lt;span style="font-family: Courier New, Courier, monospace;"&gt;string key&lt;/span&gt;) to the stack.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;ldsfld&lt;/span&gt; loads and pushes the value of the static field &lt;span style="font-family: Courier New, Courier, monospace;"&gt;validKey&lt;/span&gt; ("112358") to the stack.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;call&lt;/span&gt; invokes the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;System.String::op_Equality(string, string)&lt;/span&gt; method by popping the last two values from the stack, comparing them and then popping the returned boolean value on the stack.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;ret&lt;/span&gt; pops and returns the value that the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;op_Equality&lt;/span&gt; last pushed on the stack.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
Now that we know what's going on, we need to change these instructions to simply &lt;span style="font-family: Courier New, Courier, monospace;"&gt;return true&lt;/span&gt; rather than comparing the strings.&lt;br /&gt;
&lt;br /&gt;
This means that our code should instead do:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.ldc_i4_1.aspx" target="_blank"&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;ldc.i4.1&lt;/span&gt;&lt;/a&gt; to push the constant value of 1 onto the stack.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;ret&lt;/span&gt; to pop and return that value.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
Note that if we didn't know how to actually write a method which just returns true in CIL, we could have simply wrote the method in C#, compiled it and then view it's corresponding CIL with Reflector:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-OpAtksow3XM/UHmhdOujYPI/AAAAAAAABK8/GNPDy60ObYY/s1600/Simple_4.png" /&gt;&lt;/div&gt;&lt;br /&gt;
&lt;i&gt;As for why you see a .maxstack in the above examples, you can find a pretty good explanation &lt;a href="http://stackoverflow.com/a/10612814/44084" target="_blank"&gt;here&lt;/a&gt;.  It seems that this is used by analysis tools.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Using Reflexil, we can now remove the existing operations and replace them with our desired code:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-YS60FEuQDFI/UHmj_n7BzvI/AAAAAAAABLQ/N7SKfOOrgOE/s1600/Simple_5.png" /&gt;&lt;/div&gt;&lt;br /&gt;
Once that's done, we should now save our modified executable:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-M1xQULiMs3U/UHmkybft9JI/AAAAAAAABLc/fqtwO9POYJQ/s1600/Simple_6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-M1xQULiMs3U/UHmkybft9JI/AAAAAAAABLc/fqtwO9POYJQ/s1600/Simple_6.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Running our newly patched compilation now shows:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-yqERFkrAqwg/UHrOcAB292I/AAAAAAAABOU/OqbocjP1KPI/s1600/Simple_7.png" /&gt;&lt;/div&gt;&lt;br /&gt;
Which is exactly what we strived for, since the validation logic has now been eliminated!&lt;br /&gt;
&lt;br /&gt;
Opening our patched application in Reflector, we can now see the new code:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-D-ICiSOW02I/UHml6zYG6GI/AAAAAAAABL0/ixWqIj6r4tE/s1600/Simple_8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-D-ICiSOW02I/UHml6zYG6GI/AAAAAAAABL0/ixWqIj6r4tE/s1600/Simple_8.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;h4&gt;Patching by modifying the C# code directly&lt;/h4&gt;Using Reflexil, there's an even easier way of patching our code...by actually writing C# code rather than CIL!&lt;br /&gt;
&lt;br /&gt;
Using a modified version of our first application from the previous example, we will now patch the code by writing C# code.&lt;br /&gt;
&lt;br /&gt;
This is our new, slightly more "complex" licensing code:&lt;br /&gt;
&lt;pre class="sunlight-highlight-csharp" name="code"&gt;static void Main(string[] args)
{
    var invalidKey = "48011147";

    if (LicenseManager.Verify(invalidKey) )
    {
        Console.WriteLine("Purchased license: {0}", LicenseManager.LicenseType);
    } 
    else
    {
        Console.WriteLine("Invalid license key; continue evaluation.");
    }
}
&lt;/pre&gt;&lt;pre class="sunlight-highlight-csharp" name="code"&gt;static class LicenseManager
{
    private static string validKey = "112358";
    public static string LicenseType { get; set; }

    public static bool Verify(string key)
    {
        if (key == validKey)
        {
            LicenseType = "Enterprise";
            return true;
        }

        return false;
    }
}
&lt;/pre&gt;&lt;br /&gt;
Notice how now, for the validation process to properly succeed, we need to set the LicenseKey property to "Enterprise" before returning true from the method.&lt;br /&gt;
&lt;br /&gt;
Now of course, we can do this in CIL again, but this time we'll use a much easier method.&lt;br /&gt;
&lt;br /&gt;
Open up the application with Reflector, but this time, instead of changing the CIL opcodes directly, right click the Reflexil window and choose "Replace all with code...":&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-iBeW1RFQ_sA/UHmp2DZIwbI/AAAAAAAABMM/KQy6j72Nqz8/s1600/Simple_9.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-iBeW1RFQ_sA/UHmp2DZIwbI/AAAAAAAABMM/KQy6j72Nqz8/s1600/Simple_9.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
This will bring up this window:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-fRKKYgWcMPU/UHmtBkWceRI/AAAAAAAABMg/fOh4QzWtFiI/s1600/Simple_10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-fRKKYgWcMPU/UHmtBkWceRI/AAAAAAAABMg/fOh4QzWtFiI/s1600/Simple_10.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
From here, we now need to type in the code that will set the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;LicenseType&lt;/span&gt; property to &lt;span style="font-family: Courier New, Courier, monospace;"&gt;"Enterprise"&lt;/span&gt; and then return true from the method.  After you input the code and press the Compile button, you should end up with the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-WyA8fit9fa0/UHmtCaHszJI/AAAAAAAABMs/oECWdxxzfy8/s1600/Simple_11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-WyA8fit9fa0/UHmtCaHszJI/AAAAAAAABMs/oECWdxxzfy8/s1600/Simple_11.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Looking at the generated CIL code, we see the opcodes from before:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;ldstr&lt;/span&gt; to load the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;"Enterprise"&lt;/span&gt; string onto the stack.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;call&lt;/span&gt; which invokes the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;set_LicenseType&lt;/span&gt; method (Properties in C# are just syntactic sugar for fields and their backing get/set methods) by popping the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;"Enterprise"&lt;/span&gt; string and using it as an actual argument.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;ldc.i4.1&lt;/span&gt; to push the integer 1 on the stack.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Courier New, Courier, monospace;"&gt;ret&lt;/span&gt; for popping and returning our boolean.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
Saving and running our patched application, we now get:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-wMfaTruxEg4/UHmvFZVSs0I/AAAAAAAABM8/i_kNbFWTfKk/s1600/Simple_12.png" /&gt;&lt;/div&gt;&lt;br /&gt;
&lt;h4&gt;What if the assembly is obfuscated!?&lt;/h4&gt;Sometimes, when you try to open a .NET assembly with Reflector, you will be slapped in the face with this message:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-jE6HyfCMELQ/UHm3AeXO6lI/AAAAAAAABNM/5aUDXKQAinI/s1600/Obfuscated_1.png" /&gt;&lt;/div&gt;&lt;br /&gt;
Or, even worse, when I obfuscated the last example program we wrote with a trial version of &lt;a href="http://www.babelfor.net/" target="_blank"&gt;babelfor.NET&lt;/a&gt;, Reflector crashed when I tried to open our &lt;span style="font-family: Courier New, Courier, monospace;"&gt;LicenseManager.Verify&lt;/span&gt; method:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-ULYeyQ4MMOk/UHm6Bc3SNKI/AAAAAAAABNk/stZNHzjWiyA/s1600/Obfuscated_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0"  src="http://3.bp.blogspot.com/-ULYeyQ4MMOk/UHm6Bc3SNKI/AAAAAAAABNk/stZNHzjWiyA/s1600/Obfuscated_2.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Note though that Reflexil still shows us the CIL code, so we don't have any problems when we need to work with CIL code directly...although it will be a bit harder to understand the code by looking at the CIL code only.&lt;br /&gt;
&lt;br /&gt;
And also, if you change the language in Reflector to IL, you will prevent it from crashing but of course you still won't be able to view the raw C# code:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-Cndo-5aKv14/UHm7Cv0xAHI/AAAAAAAABNw/jTYFFhW0aPg/s1600/Obfuscated_3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-Cndo-5aKv14/UHm7Cv0xAHI/AAAAAAAABNw/jTYFFhW0aPg/s1600/Obfuscated_3.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
But what we want to do for now is to find a way to view unobfuscated C# code, and  that's where an application such as &lt;a href="http://www.digitalbodyguard.com/graywolf.html" target="_blank"&gt;Gray.Wolf&lt;/a&gt; comes to the rescue!&lt;br /&gt;
&lt;br /&gt;
Opening our application in &lt;span style="font-family: inherit;"&gt;Gray.Wolf&lt;/span&gt; and navigating to the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;Verify&lt;/span&gt; method, we get:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-s1o6_Aq_-bc/UHm8F4kjAGI/AAAAAAAABN8/FSD1AxuHogE/s1600/Obfuscated_4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-s1o6_Aq_-bc/UHm8F4kjAGI/AAAAAAAABN8/FSD1AxuHogE/s1600/Obfuscated_4.png" width="800" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
As you can see from the right pane, it couldn't completely deobfuscate all of our code for this example but it's at least giving us a very good hint as to what's going on without studying the CIL code.&lt;br /&gt;
&lt;br /&gt;
From the C# code, we can see that there is an if-statement and although Gray.Wolf couldn't deobfuscate the two operands used in the statement, we can make out what they are from the CIL code because the two values that are pushed on the stack before the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;op_Equality&lt;/span&gt; is called are done using &lt;span style="font-family: Courier New, Courier, monospace;"&gt;ldarg.0&lt;/span&gt; which loads the first only actual argument in the method and &lt;span style="font-family: Courier New, Courier, monospace;"&gt;ldsfld&lt;/span&gt; which loads a value from a static field and since the only possible static that we have which makes sense to compare to is &lt;span style="font-family: Courier New, Courier, monospace;"&gt;validKey&lt;/span&gt;, we can be pretty certain that it's using that value.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Black Sabbath",
  name : "Symptom Of The Universe",
  link : 'http://grooveshark.com/s/Symptom+Of+The+Universe/1YAXMW?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/RA5yByx8Ak0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/RA5yByx8Ak0/cracking-net-applications-simple.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-JgxBeK_pGbU/UHlnFAEBeWI/AAAAAAAABKI/PFrN1Sqe42Y/s72-c/Simple_1.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/10/cracking-net-applications-simple.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-6780461389700127403</guid><pubDate>Sun, 23 Sep 2012 11:36:00 +0000</pubDate><atom:updated>2012-10-20T17:22:39.591+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><title>Immediately-invoking functions with an exclamation point in JavaScript</title><description>Recently I had to make use of &lt;a href="https://twitter.com/about/resources/buttons" target="_blank"&gt;Twitter buttons&lt;/a&gt; and I noticed something particularly interesting in their bootstrap code:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;!function(d,s,id){var ... }(document,"script","twitter-wjs");
&lt;/pre&gt;&lt;br /&gt;
What's interesting to note here is this incantation:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;!function () {

}();
&lt;/pre&gt;&lt;br /&gt;
Notice how this looks awfully familiar to how one would normally express an immediately-invoked function:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;(function () {

}());
&lt;/pre&gt;&lt;br /&gt;
In fact, they are fundamentally doing the same thing, yet &lt;b&gt;the former is a byte shorter&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;hr/&gt;Note that this technique can be accomplished with the other unary operators as well:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;-function(){}();
~function(){}();
+function(){}();
&lt;/pre&gt;&lt;br /&gt;
What's happening here is that the unary operators are turning a function declaration [&lt;span style="font-family: Courier New, Courier, monospace;"&gt;function () {}&lt;/span&gt;] to a function expression [&lt;span style="font-family: Courier New, Courier, monospace;"&gt;!function () {}&lt;/span&gt;], which can then be immediately-invoked.&lt;br /&gt;
&lt;br /&gt;
&lt;del&gt;One limitation to using this method is that you can't get back the output of the function, unless you are expecting a truthy/falsy value.&lt;br /&gt;
&lt;br /&gt;
This is because the return value is coerced to a boolean when using the exclamation point and negated (since the function invocation has higher precedence, which is why this technique works), and you can't undo that boolean coercion operation on complex types, strings and numbers.&lt;br /&gt;
&lt;br /&gt;
You can however undo the negation operation if you are expecting a truthy/falsy value by simply double negating the output: var truthy = !!function () { return 1; }();&lt;br /&gt;
&lt;/del&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Grateful Dead",
  name : "Althea",
  link : 'http://grooveshark.com/s/Althea/2vko5p?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/uPJfoNrdWPo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/uPJfoNrdWPo/immediately-invoking-functions-with.html</link><author>noreply@blogger.com (Andreas Grech)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/09/immediately-invoking-functions-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-2461245317839943844</guid><pubDate>Sun, 23 Sep 2012 08:37:00 +0000</pubDate><atom:updated>2012-09-23T10:55:50.928+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><category domain="http://www.blogger.com/atom/ns#">Projects</category><category domain="http://www.blogger.com/atom/ns#">jQuery</category><title>Webroulette: Take me somewhere random!</title><description>So once again, what follows is the result of a weekend-project-during-the-week kind of thing.&lt;br /&gt;
&lt;br /&gt;
Similar to chatroulette.com where you shuffle through random people, I wrote this so that you can shuffle through random websites instead:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin: 0 auto; width: 900px;"&gt;&lt;div class="separator" style="float: left; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-zghi0SNMlQM/UF3X9FDPLfI/AAAAAAAABI0/i_2u1cAXiQg/s1600/Webroulette_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="237" src="http://3.bp.blogspot.com/-zghi0SNMlQM/UF3X9FDPLfI/AAAAAAAABI0/i_2u1cAXiQg/s400/Webroulette_1.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="float: left; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-2GZj4QQncy8/UF3X9muLXUI/AAAAAAAABJA/VdYjp_OAURQ/s1600/Webroulette_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="242" src="http://1.bp.blogspot.com/-2GZj4QQncy8/UF3X9muLXUI/AAAAAAAABJA/VdYjp_OAURQ/s400/Webroulette_2.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
You can also specify a single word to be used as a form of bias:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin: 0 auto; width: 900px;"&gt;&lt;div class="separator" style="float: left; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-H1ltSVVmAuo/UF3bWuo3m8I/AAAAAAAABJU/17Oscu8v50o/s1600/Webroulette_3_bias.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="242" src="http://1.bp.blogspot.com/-H1ltSVVmAuo/UF3bWuo3m8I/AAAAAAAABJU/17Oscu8v50o/s400/Webroulette_3_bias.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="float: left; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-iGs0REPqH0M/UF3bXMD6LdI/AAAAAAAABJg/ZkN4VevGwYI/s1600/Webroulette_4_bias.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="242" src="http://2.bp.blogspot.com/-iGs0REPqH0M/UF3bXMD6LdI/AAAAAAAABJg/ZkN4VevGwYI/s400/Webroulette_4_bias.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style="font-size: large;"&gt;You can see it in action here: &lt;a href="http://dreasgrech.com/upload/webroulette/" target="_blank"&gt;http://dreasgrech.com/upload/webroulette/&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Source code&lt;/h4&gt;As usual, the source for this project resides on &lt;a href="https://github.com/dreasgrech/webroulette" target="_blank"&gt;github&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;How it's done&lt;/h4&gt;In a nutshell, I basically do a Google Custom Search API call with a randomly constructed query and then show the first result from the collection of hits.&lt;br /&gt;
&lt;br /&gt;
&lt;h5&gt;The server&lt;/h5&gt;On the server, I have a file of ~8.5k commonly used English words, and based on the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;length&lt;/span&gt; query string value, I select a number of these words in a random manner.&lt;br /&gt;
&lt;br /&gt;
Note that I enforce a hard limit on the range for the allowed values of length; currently, the range is [0, 10].  If no &lt;span style="font-family: Courier New, Courier, monospace;"&gt;length&lt;/span&gt; is specified, I default it to 3.&lt;br /&gt;
&lt;br /&gt;
This is the word-list that I'm currently using is at &lt;a href="http://dreasgrech.com/upload/webroulette/komuni.txt" target="_blank"&gt;http://dreasgrech.com/upload/webroulette/komuni.txt&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
If a bias is specified, I use it as the first word in the phrase and once this phrase is built, it's echoed so that it can be read by the client.&lt;br /&gt;
&lt;br /&gt;
Here are some examples of the script in action:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://dreasgrech.com/upload/webroulette/louie.php" target="_blank"&gt;http://dreasgrech.com/upload/webroulette/louie.php&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://dreasgrech.com/upload/webroulette/louie.php?bias=obfuscation" target="_blank"&gt;http://dreasgrech.com/upload/webroulette/louie.php?bias=obfuscation&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://dreasgrech.com/upload/webroulette/louie.php?bias=douglasadams&amp;amp;length=6" target="_blank"&gt;http://dreasgrech.com/upload/webroulette/louie.php?bias=douglasadams&amp;amp;length=6&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h5&gt;The client&lt;/h5&gt;So what the client does is an Ajax call to this PHP script to get the query and then do an Ajax call to the Google Custom Search API to get a list of hits.  Btw, I'm also using a query string which I termed &lt;span style="font-family: Courier New, Courier, monospace;"&gt;iesux&lt;/span&gt; which I use for a unix timestamp, and I had to do this because calls to the same url were being cached by Internet Explorer and so I needed this timestamp to introduce variety to the urls.&lt;br /&gt;
&lt;br /&gt;
What I then show on screen is the &lt;b&gt;first result&lt;/b&gt; from the list of google hits from that query.&lt;br /&gt;
&lt;br /&gt;
&lt;h5&gt;Why not embed an I'm Feeling Lucky link in the iframe?&lt;/h5&gt;Halfway during the project, I realized that this functionality is pretty similar to the I'm Feeling Lucky functionality that Google still offers (for some reason?).&lt;br /&gt;
&lt;br /&gt;
I figured that this way, I would eliminate the need to use the API (which, btw, has a 100 queries a day limit) but instead use an I'm Feeling Lucky url (like &lt;a href="http://www.google.com/search?&amp;amp;btnI=745&amp;amp;q=obfuscation" target="_blank"&gt;http://www.google.com/search?&amp;amp;btnI=745&amp;amp;q=obfuscation&lt;/a&gt;) for the source of the iframe.&lt;br /&gt;
&lt;br /&gt;
But after I tried a couple of queries like this, I realized that sometimes, an I'm Feeling Lucky url redirects to an actual Google Search page and since Google prevents their site to be rendered in an &amp;lt;iframe&amp;gt;, I was getting a lot of blank pages in the &amp;lt;iframe&amp;gt;...and that sucks.&lt;br /&gt;
&lt;br /&gt;
&lt;h5&gt;What about the 100 query limit?&lt;/h5&gt;For the free offering of the Google Custom Search API, Google enforces a 100 queries per day (per project) limit.&lt;br /&gt;
&lt;br /&gt;
To work around this, I created five projects (i.e. 5 API keys) and whenever someone loads the page, I serve one of the five keys at random.  If it turns out that that particular key is used up, the client requests another API key.&lt;br /&gt;
&lt;br /&gt;
You can also specify your own API key (make sure your that the the Custom Search API service is turned on) using the &lt;span style="font-family: Courier New, Courier, monospace;"&gt;key&lt;/span&gt; query string, as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;a target="_blank" href="http://dreasgrech.com/upload/webroulette/?key=AIzaSyCCv2V9wF1cO1uck19H5uuAodJ-Ml0rBgg"&gt;http://dreasgrech.com/upload/webroulette/?key=AIzaSyCCv2V9wF1cO1uck19H5uuAodJ-Ml0rBgg&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Brainticket",
  name : "Black Sand",
  link : 'http://grooveshark.com/s/Black+Sand/46hcOs?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/FcxwVQ0eFWE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/FcxwVQ0eFWE/webroulette-take-me-somewhere-random.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-zghi0SNMlQM/UF3X9FDPLfI/AAAAAAAABI0/i_2u1cAXiQg/s72-c/Webroulette_1.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/09/webroulette-take-me-somewhere-random.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-6474808629308621415</guid><pubDate>Mon, 05 Mar 2012 18:19:00 +0000</pubDate><atom:updated>2012-03-05T19:19:23.850+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><title>A run-length encoder in JavaScript</title><description>Run-length encoding is one of the most trivial forms of data compression methods.  It works by combining &lt;b&gt;runs of characters&lt;/b&gt; into a value containing the number of times that character is repeated in the run and the actual character itself.&lt;br /&gt;
&lt;br /&gt;
So something like:&lt;br /&gt;
&lt;pre&gt;UUUXXXXXXXXXXXXXXXXXXXXTTTTTKKKKK
&lt;/pre&gt;would be encoded to:&lt;br /&gt;
&lt;pre&gt;3U20X5T5K
&lt;/pre&gt;In this case, the encoded version of the text has been reduced by ~73%.  But note that there may be cases where the encoded text would end up larger than the actual (decoded) plain text; this happens when the text contains many isolated characters rather than runs of characters.  &lt;br /&gt;
&lt;br /&gt;
Take for example &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt; (26 characters); if you 'compress' that using a run-length algorithm, you'll end up with &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;1a1b1c1d1e1f1g1h1i1j1k1l1m1n1o1p1q1r1s1t1u1v1w1x1y1z&lt;/span&gt; (52 characters) which is a 100% increase from the decoded version.&lt;br /&gt;
&lt;h4&gt;Source and demo&lt;/h4&gt;The source can be found at &lt;a href="https://github.com/dreasgrech/runlength-js" target="_blank"&gt;https://github.com/dreasgrech/runlength-js&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
And the demo of the encoder is at &lt;a href="http://dreasgrech.com/upload/runlength/" target="_blank"&gt;http://dreasgrech.com/upload/runlength/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Note that as of current, the code does not handle instances where the decoded text contains digits.&lt;br /&gt;
&lt;/i&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-0RpA5uHC6Q4/T1PK__RJZwI/AAAAAAAABIA/iqNd1rJ5QJA/s1600/rle.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="400" src="http://2.bp.blogspot.com/-0RpA5uHC6Q4/T1PK__RJZwI/AAAAAAAABIA/iqNd1rJ5QJA/s400/rle.png" width="269" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Fuzzy Duck",
  name : "In Our Time",
  link : 'http://grooveshark.com/s/In+Our+Time/3EjQ7U?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/4lk-jxiKPmU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/4lk-jxiKPmU/run-length-encoder-in-javascript.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-0RpA5uHC6Q4/T1PK__RJZwI/AAAAAAAABIA/iqNd1rJ5QJA/s72-c/rle.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/03/run-length-encoder-in-javascript.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-8066805116088819491</guid><pubDate>Wed, 01 Feb 2012 09:47:00 +0000</pubDate><atom:updated>2012-10-20T17:18:31.332+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><title>Creating POJOs in JavaScript</title><description>&lt;a href="http://en.wikipedia.org/wiki/Plain_Old_Java_Object" target="_blank"&gt;POJO&lt;/a&gt; stands for Plain Old Java Object, but in our case, it will be JavaScript instead of Java...&lt;i&gt;Plain Old JavaScript Objects&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
Sometimes in your code, you would need to create objects that are just containers for members (in OOP terms, these objects are usually referred to as &lt;i&gt;classes&lt;/i&gt;).&lt;br /&gt;
&lt;br /&gt;
As an example:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;var dog = function (name, age) {
 return {
       name: name,
       age: age
 };
};
&lt;/pre&gt;Now you can obviously write all this by hand, but you don't have to because you can write a function that makes use of JavaScript's &lt;a href="http://en.wikipedia.org/wiki/Type_system#Dynamic_typing" target="_blank"&gt;dynamic&lt;/a&gt; nature to do it for you:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;var pojo = function () {
    var members = arguments;

    return function () {
        var obj = {}, i = 0, j = members.length;
        for (; i &amp;lt; j; ++i) {
            obj[members[i]] = arguments[i];
        }

        return obj;
    };
};
&lt;/pre&gt;And this is how it can be used:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;var dog = pojo('name', 'age'), // create the POJO
    fido = dog('Fido', 2); // create an 'instance' of the POJO

// fido.name -&amp;gt; 'Fido'
// fido.age  -&amp;gt; 2

&lt;/pre&gt;The &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;pojo&lt;/span&gt; function is pretty straightforward.  First we grab a reference to the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;arguments&lt;/span&gt; object (which contains the names of the members that we want to be in our object; &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;['name', 'age']&lt;/span&gt;) and then return a function which will serve as the initializer for our POJO object.&lt;br /&gt;
&lt;br /&gt;
Once the inner initializer function is invoked from the outside (&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;dog('Fido', 2)&lt;/span&gt;), we iterate through the member names we got from the initial &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;arguments&lt;/span&gt; object to create a new object (&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;obj&lt;/span&gt;) containing the member names and the values of the actual arguments that were passed to the inner function as their respective values.&lt;br /&gt;
&lt;br /&gt;
If you only want a single 'instance' of the POJO, you can get away with a one-liner:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;var fido = pojo('name', 'age')('Fido', 2);
&lt;/pre&gt;&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Please",
  name : "No More White Horses",
  link : 'http://grooveshark.com/s/No+More+White+Horses/3zyJBi?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/C6fqEzscvl8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/C6fqEzscvl8/creating-pojos-in-javascript.html</link><author>noreply@blogger.com (Andreas Grech)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/02/creating-pojos-in-javascript.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-7118643702561291305</guid><pubDate>Thu, 26 Jan 2012 21:16:00 +0000</pubDate><atom:updated>2012-01-26T22:16:47.404+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">YouTube</category><title>Bulk converting from YouTube-MP3.org</title><description>&lt;h4&gt;[Update]&lt;/h4&gt;Following an email conversation I had with Philip Matesanz, the owner of Youtube-MP3, I've decided to take down my github repository because of worries that my tool could be used to flood their server with requests.&lt;br /&gt;
&lt;br /&gt;
&lt;hr/&gt;&lt;a href="http://www.youtube-mp3.org/" target="_blank"&gt;YouTube-MP3.org&lt;/a&gt; is a free service which lets you convert YouTube videos to MP3s.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://www.youtube-mp3.org/" imageanchor="1" style="margin-left:1em; margin-right:1em" target="_blank"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-6I_UJlDyjYQ/Tm-0eikDCjI/AAAAAAAABDo/U515WnEb-kU/s1600/youtube-mp3.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
It's a great service, but you if you have many YouTube videos you want to convert, it can be a bit tedious to do them one by one while waiting for each one to complete and then download manually; and that's what I aimed to fix.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Bulk downloading&lt;/h4&gt;I wrote this application which takes a text file that is filled with YouTube links as input and which then starts relaying those links to the YouTube-MP3 service for conversion and downloading.&lt;br /&gt;
&lt;br /&gt;
The text file needs to be structured as follows i.e. a YouTube link on each line:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;http://www.youtube.com/watch?v=eslhuSEC_QU
http://www.youtube.com/watch?v=A7x_z6tG4yY
http://www.youtube.com/watch?v=MLxPknwiNoc
http://www.youtube.com/watch?v=WYV7qYyHKIs
http://www.youtube.com/watch?v=4f1K4HNnQWo
http://www.youtube.com/watch?v=SEsKwOrejec
http://www.youtube.com/watch?v=f4CwlUSSTzk
http://www.youtube.com/watch?v=G6ZsvYA-YTo
http://www.youtube.com/watch?v=tK7b3Y42w3k
http://www.youtube.com/watch?v=0ppTcHjvtsU
http://www.youtube.com/watch?v=8ZjADBfqOR4
http://www.youtube.com/watch?v=DW3ZpytL2Nk
http://www.youtube.com/watch?v=CsdzJTW7SUk
&lt;/pre&gt;&lt;br /&gt;
The process converting/downloading works in iterations.  Those videos which fail to convert/download during one iteration are moved to the next iteration so that the conversion/download can be tried again.  This is because YouTube-MP3 restricts its API usage to 10 conversion/downloads per hour.  Between each iteration, there is an interval during which the program is halted to wait until the iteration starts.&lt;br /&gt;
&lt;br /&gt;
The make the application more efficient, it converts/downloads videos simultaneously, rather than sequentially.  This means that the total running time of the program is equivalent to the longest-running job's time, not to the sum of all the job's times.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Usage&lt;/h4&gt;&lt;pre&gt;Usage: BulkYoutubeMP3Console.exe [options] &amp;lt;Youtube links file&amp;gt;

Options:
  -p, --path=VALUE           The download path
  -t, --time=VALUE           The waiting time in minutes between the
                               iterations.  Minimum: 10 minutes
  -x, --proxy=VALUE          Use if you want to use a proxy for downloading.
                               Format =&gt; &lt;host&gt;:&lt;port&gt; (default 80)


&lt;/pre&gt;&lt;br /&gt;
You can find the source of app from &lt;strike&gt;&lt;a target="_blank" href="https://github.com/dreasgrech/bulk-youtube-mp3"&gt;https://github.com/dreasgrech/bulk-youtube-mp3&lt;/a&gt;&lt;/strike&gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example run:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-XYIwL2o4HdQ/TncUW1gNzDI/AAAAAAAABDw/GfChssslxj8/s1600/BulkYoutubeMP3Console.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="238" width="400" src="http://2.bp.blogspot.com/-XYIwL2o4HdQ/TncUW1gNzDI/AAAAAAAABDw/GfChssslxj8/s400/BulkYoutubeMP3Console.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : "Mike D'Abo",
  name : "Handbags and Gladrags",
  link : 'http://grooveshark.com/s/Handbags+And+Gladrags/3JOq0X?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/oTNLtdUhu-g" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/oTNLtdUhu-g/bulk-converting-from-youtube-mp3org.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-6I_UJlDyjYQ/Tm-0eikDCjI/AAAAAAAABDo/U515WnEb-kU/s72-c/youtube-mp3.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/01/bulk-converting-from-youtube-mp3org.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-6619020320440847347</guid><pubDate>Thu, 26 Jan 2012 20:29:00 +0000</pubDate><atom:updated>2013-04-09T17:15:37.872+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><category domain="http://www.blogger.com/atom/ns#">Projects</category><category domain="http://www.blogger.com/atom/ns#">jQuery</category><title>Promoting your Chrome extension with a yellow infobar</title><description>This is a jQuery plugin I wrote which emulates Chrome's infobar for promoting a Google Chrome extension.&lt;br /&gt;
&lt;br /&gt;
The following are Chrome's infobar and the one I wrote for the plugin, respectively:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-ebZzuawFr8o/TxXHk0MfupI/AAAAAAAABGA/7hHJ-AOQQZk/s1600/chromebar.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-ebZzuawFr8o/TxXHk0MfupI/AAAAAAAABGA/7hHJ-AOQQZk/s1600/chromebar.png" width="750" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-sYWQSpyEes8/TxXHk4JpAqI/AAAAAAAABGI/pomc_m-pl2M/s1600/bar_default.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-sYWQSpyEes8/TxXHk4JpAqI/AAAAAAAABGI/pomc_m-pl2M/s1600/bar_default.png" width="750" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;h4&gt;Demo&lt;/h4&gt;I've set up a small demo of the plugin here: &lt;a href="http://www.dreasgrech.com/upload/extinfobar/" target="_blank"&gt;http://www.dreasgrech.com/upload/extinfobar/&lt;/a&gt;.&lt;br /&gt;
&lt;h4&gt;Download&lt;/h4&gt;&lt;strike&gt;&lt;i&gt;&lt;a href="http://blog.jquery.com/2011/12/08/what-is-happening-to-the-jquery-plugins-site/" target="_blank"&gt;Until the jQuery plugins page is restored&lt;/a&gt;, you're gonna have to download this plugin directly from github.&lt;/i&gt;&lt;/strike&gt;&lt;br /&gt;
&lt;br /&gt;
The plugin can be found at the jQuery plugins site: &lt;a target="_blank" href="http://plugins.jquery.com/extinfobar/"&gt;http://plugins.jquery.com/extinfobar/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
You can get the full source of the plugin from &lt;a href="https://github.com/dreasgrech/ChromeExtInfoBar" target="_blank"&gt;https://github.com/dreasgrech/ChromeExtInfoBar&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
These are all the images that you need:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-qPoY3rqYXM8/TyGqg82TpMI/AAAAAAAABGs/HO6q_I_aWmY/s1600/button_click.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="25" width="3" src="http://2.bp.blogspot.com/-qPoY3rqYXM8/TyGqg82TpMI/AAAAAAAABGs/HO6q_I_aWmY/s1600/button_click.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-WrYIeZyHw54/TyGqhEQZuWI/AAAAAAAABG0/GUv--z4rgbs/s1600/button_gradient.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="25" width="3" src="http://1.bp.blogspot.com/-WrYIeZyHw54/TyGqhEQZuWI/AAAAAAAABG0/GUv--z4rgbs/s1600/button_gradient.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-yvhCr8lguZw/TyGqhNjn70I/AAAAAAAABHE/jFmass5x-hQ/s1600/close.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="10" width="10" src="http://3.bp.blogspot.com/-yvhCr8lguZw/TyGqhNjn70I/AAAAAAAABHE/jFmass5x-hQ/s1600/close.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-b5bliVBhDIc/TyGqhfW5xAI/AAAAAAAABHQ/BNUGNTcrX3U/s1600/close_hover.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="10" width="10" src="http://2.bp.blogspot.com/-b5bliVBhDIc/TyGqhfW5xAI/AAAAAAAABHQ/BNUGNTcrX3U/s1600/close_hover.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-89W83ukn4qg/TyGqhuNE7cI/AAAAAAAABHc/cgN4qP9gKTM/s1600/defaulticon.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="20" width="20" src="http://2.bp.blogspot.com/-89W83ukn4qg/TyGqhuNE7cI/AAAAAAAABHc/cgN4qP9gKTM/s1600/defaulticon.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-eGrkIkN1mS4/TyGquUiJQfI/AAAAAAAABHo/Yc0wJcoukHE/s1600/gradient_bar.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="35" width="5" src="http://2.bp.blogspot.com/-eGrkIkN1mS4/TyGquUiJQfI/AAAAAAAABHo/Yc0wJcoukHE/s1600/gradient_bar.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;h4&gt;Usage&lt;/h4&gt;The simplest way to use the plugin is to simply invoke it using just your Chrome extension ID:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;$.fn.extInfobar({
    id: 'nbilgjjflfiiijecdjpnbganoiafneph'
});
&lt;/pre&gt;&lt;br /&gt;
You can also specify a custom message to be displayed on the infobar, as opposed to the default message:&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;$.fn.extInfobar({
    id: 'nbilgjjflfiiijecdjpnbganoiafneph',
    message: 'This is my custom message I want to show on the infobar'
});
&lt;/pre&gt;&lt;br /&gt;
There are three other optional parameters you can pass; &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;redirectIfInstallFails&lt;/span&gt;, &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;rememberClose&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;rememberRedirect&lt;/span&gt;.  All three default to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;true&lt;/span&gt; if unspecified.&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;$.fn.extInfobar({
    id: 'nbilgjjflfiiijecdjpnbganoiafneph',
    message: 'This is my custom message I want to show on the infobar',
    redirectIfInstallFails: false,
    rememberClose: false,
    rememberRedirect: false
});
&lt;/pre&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;redirectIfInstallFails&lt;/span&gt; specifies whether the plugin should redirect the user to the extension's Chrome Webstore page if the installation happens to fail or if the site you're using the plugin on is not verified (more info on that below).&lt;br /&gt;
&lt;br /&gt;
When the user clicks on the close button, the action is remembered via &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;localStorage&lt;/span&gt; so that the bar is not displayed again for him but the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;rememberClose&lt;/span&gt; parameter allows you to override this functionality; so if you set it to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;false&lt;/span&gt;, the bar will keep appearing on subsequent page visits even if the user closed it.&lt;br /&gt;
&lt;br /&gt;
The last parameter, &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;rememberRedirect&lt;/span&gt;, is used to save the action of the plugin redirecting the user to the extension's Chrome Webstore; by default, the action is saved which means that once a user is redirected to the Chrome Webstore page, the bar will not be shown to him on subsequent visits to your page, but you can of course override this functionality by setting &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;rememberRedirect&lt;/span&gt; to false.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;How it works&lt;/h4&gt;The plugin makes use of Chrome's &lt;a href="http://code.google.com/chrome/webstore/docs/inline_installation.html" target="_blank"&gt;inline installation&lt;/a&gt; functionality, but note that this only works if your extension's website is &lt;a href="http://support.google.com/webmasters/bin/answer.py?hl=en&amp;amp;answer=35179" target="_blank"&gt;verified&lt;/a&gt; and this plugin is invoked from the same domain as the verified site.&lt;br /&gt;
&lt;br /&gt;
When you click on the 'Install' button on the infobar, and the context meets the two aforementioned criteria, the following modal dialog will pop up:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-RQy2O5TLX-c/TxhsAra7kcI/AAAAAAAABGY/eylKuusDj50/s1600/inlineinstall.png" /&gt;&lt;/div&gt;&lt;br /&gt;
Otherwise, the user will be redirected directly to the extension's page on the &lt;a href="https://chrome.google.com/webstore/" target="_blank"&gt;Chrome Webstore&lt;/a&gt; (unless the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;redirectIfInstallFails&lt;/span&gt; option is explicitly set to false).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : 'Procol Harum',
  name : "Repent Walpurgis",
  link : 'http://grooveshark.com/s/Repent+Walpurgis/31S0Tn?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/xiuwo8oWaEE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/xiuwo8oWaEE/promoting-your-chrome-extension-with.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-ebZzuawFr8o/TxXHk0MfupI/AAAAAAAABGA/7hHJ-AOQQZk/s72-c/chromebar.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/01/promoting-your-chrome-extension-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-5025889989828882953</guid><pubDate>Thu, 26 Jan 2012 18:23:00 +0000</pubDate><atom:updated>2012-10-20T17:20:33.457+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><category domain="http://www.blogger.com/atom/ns#">Facebook</category><title>Making Facebook's Graph API work in Internet Explorer</title><description>I've encountered several issues while trying to get Facebook's Graph API work in IE.&lt;br /&gt;
&lt;br /&gt;
As for my general setup, I was using jQuery's &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;getJSON&lt;/span&gt; and a typical call looked something like the following:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;$.getJSON('https://graph.facebook.com/13601226661?access_token=' + accessToken, function (response) {
    callback(response);
});
&lt;/pre&gt;&lt;br /&gt;
The first problem was that I was getting a &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;No Transport&lt;/span&gt; when making a call to https://graph.facebook.com. The issue here was due to the fact that IE uses &lt;a href="http://msdn.microsoft.com/en-us/library/ie/cc288060(v=vs.85).aspx" target="_blank"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;XDomainRequest&lt;/span&gt;&lt;/a&gt; and I ultimately resolved it by using the following workaround: &lt;a href="https://github.com/jaubourg/ajaxHooks/blob/master/src/ajax/xdr.js" target="_blank"&gt;https://github.com/jaubourg/ajaxHooks/blob/master/src/ajax/xdr.js&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
This is because &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;XDomainRequest&lt;/span&gt; is currently &lt;a href="http://bugs.jquery.com/ticket/8283" target="_blank"&gt;not supported&lt;/a&gt; in jQuery.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
After including the xdr.js file, I started getting a different problem.  Internet Explorer now started saying "&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;Access is denied&lt;/span&gt;" whenever I make the AJAX call.&lt;br /&gt;
&lt;br /&gt;
After some reading,  I found out from &lt;a href="http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx" target="_blank&amp;quot;"&gt;here&lt;/a&gt; that:&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;blockquote style="width: 600px"&gt;7) Requests must be targeted to the same scheme as the hosting page.&lt;/blockquote&gt;&lt;/center&gt;&lt;br /&gt;
Essentially this means that "[...]&amp;nbsp;&lt;i&gt;if your AJAX page is at http://example.com, then your target URL must also begin with HTTP. Similarly, if your AJAX page is at https://example.com, then your target URL must also begin with HTTPS&lt;/i&gt;".&lt;br /&gt;
&lt;br /&gt;
And of course, I was making calls from &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;http&lt;/span&gt; (my domain) to &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;https&lt;/span&gt; (graphs.facebook), and that goes against the aforementioned point #7.&lt;br /&gt;
&lt;br /&gt;
So what I first tried was to make a call to http://graphs.facebook.com instead (i.e. using &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;http&lt;/span&gt; instead of &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;https&lt;/span&gt;), and that works...but only when not using an &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;access_token&lt;/span&gt; in your request; and I needed to include my token in all the requests.&lt;br /&gt;
&lt;br /&gt;
And the solution to this problem was &lt;a href="http://en.wikipedia.org/wiki/JSONP" target="_blank&amp;quot;"&gt;jsonp&lt;/a&gt;.  With jsonp, IE allows the cross-domain requests.  I'm not going to go into the details of jsonp in this post, and luckily, jQuery supports it natively since all you need to do is include &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;callback=?&lt;/span&gt; as part of your request.  This means that my calls now changed as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;$.getJSON('https://graph.facebook.com/13601226661?access_token=' + accessToken + '&amp;amp;callback=?', function (response) {
    callback(response);
});
&lt;/pre&gt;&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : 'Ramases',
  name : "Life Child",
  link : 'http://grooveshark.com/s/Life+Child/2g4xA0?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/ZNhIrmOP3S4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/ZNhIrmOP3S4/making-facebooks-graph-api-work-in.html</link><author>noreply@blogger.com (Andreas Grech)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/01/making-facebooks-graph-api-work-in.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-3277463600844572918</guid><pubDate>Mon, 09 Jan 2012 19:31:00 +0000</pubDate><atom:updated>2012-10-20T17:17:24.337+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><category domain="http://www.blogger.com/atom/ns#">Projects</category><category domain="http://www.blogger.com/atom/ns#">Google Chrome Extensions</category><title>Paste and Go - A Google Chrome extension</title><description>A functionality in browsers, specifically Google Chrome and Mozilla Firefox, which I find extremely comfortable is "Paste and Go".&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-l-iv23wH3UQ/Tws4ZwneAVI/AAAAAAAABFo/RZRmJaLooMk/s1600/PasteGoChrome.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="152" src="http://3.bp.blogspot.com/-l-iv23wH3UQ/Tws4ZwneAVI/AAAAAAAABFo/RZRmJaLooMk/s320/PasteGoChrome.png" width="187" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
This pastes in your clipboard contents in the address bar and submits it automatically at one go, which is a step less than pressing Paste and then submitting manually.&lt;br /&gt;
&lt;br /&gt;
Because I like this functionality a lot, I decided to try and emulate it for input textboxes on websites with a Google Chrome extension.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-fCtX-Iniv7A/Tws8YdmC73I/AAAAAAAABF0/09U1CqEg8uQ/s1600/PasteAndGoScreen1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://1.bp.blogspot.com/-fCtX-Iniv7A/Tws8YdmC73I/AAAAAAAABF0/09U1CqEg8uQ/s320/PasteAndGoScreen1.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;h4&gt;Download&lt;/h4&gt;The extension is published at &lt;a href="https://chrome.google.com/webstore/detail/nbilgjjflfiiijecdjpnbganoiafneph" target="_blank"&gt;https://chrome.google.com/webstore/detail/nbilgjjflfiiijecdjpnbganoiafneph&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
And the source: &lt;a href="https://github.com/dreasgrech/PasteAndGo" target="_blank"&gt;https://github.com/dreasgrech/PasteAndGo&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;How it works&lt;/h4&gt;When invoking it (Right Click -&amp;gt; Paste and Go), the extension grabs reference to the input text box where you right clicked, clears its value and pastes in the contents of your clipboard.&lt;br /&gt;
&lt;br /&gt;
It then searches for the closest &amp;lt;form&amp;gt; to your text box with jQuery's &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;a href="http://api.jquery.com/closest/" target="_blank"&gt;closest&lt;/a&gt;&lt;/span&gt; function and if it finds a form, it submits it.&lt;br /&gt;
&lt;br /&gt;
Note that &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;closest&lt;/span&gt; only traverses &lt;b&gt;up&lt;/b&gt; the DOM tree, so it will not find &amp;lt;form&amp;gt;s which are either below (in the tree) or adjacent (siblings) to your input text box.&lt;br /&gt;
&lt;br /&gt;
If no &amp;lt;form&amp;gt; is found, another approach is taken.  This time, the extension searches for the closest button to your input text box and if it finds one, it clicks it.  This time though, I couldn't use jQuery's &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;closest&lt;/span&gt; since, most of the time, submit buttons in &amp;lt;form&amp;gt;s are placed after the input text box.  Therefore, I wrote this function to find the 'closest' button:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sunlight-highlight-javascript" name="code"&gt;var getClosestButton = function (el) {
        var buttons;
        while ((el = el.parent()).length) {
            buttons = el.find(":button, input[type=submit]"); // :button -&gt; "input[type=button], button"

            if (buttons.length) {
                return buttons.eq(buttons.length - 1);
            }
        }
};
&lt;/pre&gt;&lt;br /&gt;
Once a button is found, a click event is simulated on it.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;A word of caution&lt;/h4&gt;This extension may not always work as expected.  This is because it assumes that the first &amp;lt;form&amp;gt; it finds closest (upwards only) to your input box is the correct form which needs to be submitted, or that the 'closest' button that's found near your input box is the button that needs to be clicked...and the extension may not always find the &lt;i&gt;correct&lt;/i&gt; button or submit the correct form; although the later is very rarely an issue.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Therefore, use it at your own risk.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Here are some of the places where it worked successfully:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Facebook&lt;/li&gt;
&lt;li&gt;docs.jquery.com&lt;/li&gt;
&lt;li&gt;Stackoverflow&lt;/li&gt;
&lt;li&gt;Twitter&lt;/li&gt;
&lt;li&gt;Google&lt;/li&gt;
&lt;li&gt;Yahoo!&lt;/li&gt;
&lt;li&gt;Bing&lt;/li&gt;
&lt;li&gt;YouTube&lt;/li&gt;
&lt;li&gt;The Pirate Bay&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
And here are some of the sites in which it didn't work as expected:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Grooveshark&lt;/li&gt;
&lt;li&gt;Gmail&lt;/li&gt;
&lt;li&gt;CodePlex&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;center&gt;&lt;i&gt;Where did you try it, and did it work as expected?&lt;/i&gt;&lt;/center&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : 'Steamhammer',
  name : "I Wouldn't Have Thought",
  link : 'http://grooveshark.com/s/I+Wouldn+t+Have+Thought/3yI0Ve?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/mBiFzeDgqts" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/mBiFzeDgqts/paste-and-go-google-chrome-extension.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-l-iv23wH3UQ/Tws4ZwneAVI/AAAAAAAABFo/RZRmJaLooMk/s72-c/PasteGoChrome.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2012/01/paste-and-go-google-chrome-extension.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-896054437697825189</guid><pubDate>Wed, 30 Nov 2011 03:20:00 +0000</pubDate><atom:updated>2011-12-06T04:37:26.585+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">XNA</category><category domain="http://www.blogger.com/atom/ns#">Projects</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Conway's Game of Life in XNA</title><description>The following video demonstrates an implementation of &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life"&gt;Conway's Game of Life&lt;/a&gt; I once wrote in in XNA.&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;iframe width="640" height="360" src="http://www.youtube.com/embed/0TxuIamXrnk?hd=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;&lt;/center&gt;&lt;br /&gt;
&lt;br /&gt;
Unfortunately, I currently don't have the source code for this online but I'll post it once I stop procrastinating.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : 'Episode Six',
  name : "Mr. Universe",
  link : 'http://grooveshark.com/s/Episode+Six+Mr+Universe/3Q9bzc?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/-uN-Z7AnlHY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/-uN-Z7AnlHY/conways-game-of-life-in-xna.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://img.youtube.com/vi/0TxuIamXrnk/default.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2011/11/conways-game-of-life-in-xna.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-7117612757607239790.post-5925033192957725533</guid><pubDate>Mon, 07 Nov 2011 20:39:00 +0000</pubDate><atom:updated>2012-10-20T17:19:18.913+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Javascript</category><title>Cheating at a typing test with JavaScript</title><description>So I just found this website called &lt;a href="http://speedtest.10-fast-fingers.com/" target="_blank"&gt;10fastfingers&lt;/a&gt; and it has a typing game that measures your speed in WPM.  I played with it for a while to see how fast I could really type but after I got bored keying in random words in a text box, I decided to spice things up a bit...by cheating; because hey, why not?&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;iframe width="640" height="480" src="http://www.youtube.com/embed/kC4n_lqFB64?hd=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;&lt;/center&gt;&lt;br /&gt;
&lt;br /&gt;
And so, I wrote two scripts to do the typing for me; sort of like secretaries.  The first one just completes the test instantly, which guarantees that all the words available in the test are "typed in".  The second one takes its time with a delay between each word, but it's more fun to look at because you actually get to see the words which are being completed.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;The instant one&lt;/h4&gt;&lt;pre class="sunlight-highlight-javascript" name="code"&gt;var words = document.getElementById('wordlist').innerHTML.split(/\|/),
    input = document.getElementById('inputfield'),
    pressEvent = document.createEvent("KeyboardEvent"),
    pressSpace = function () {
        pressEvent.initKeyEvent("keyup", true, true, window, false, false, false, false, 32, false);
        input.dispatchEvent(pressEvent);
    },
    go = function () {
        var i = 0,  j = words.length;
        for (; i &amp;lt; j; ++i) {
            input.value += words[i];
            pressSpace();
        }
    };

go();
&lt;/pre&gt;&lt;br /&gt;
This script is not very entertaining since it finishes the test almost instantly because it's using a for loop to go through the words.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Putting a delay&lt;/h4&gt;&lt;pre class="sunlight-highlight-javascript" name="code"&gt;var words = document.getElementById('wordlist').innerHTML.split(/\|/),
    input = document.getElementById('inputfield'),
    timer = document.getElementById('timer'),
    pressEvent = document.createEvent("KeyboardEvent"),
    pressSpace = function () {
        pressEvent.initKeyEvent("keyup", true, true, window, false, false, false, false, 32, false);
        input.dispatchEvent(pressEvent);
    },
    go = function (delay) {
        var current = 0,
            worker = setInterval(function () {
                if (current == words.length || timer.className) {
                    clearInterval(worker);
                    return;
                }
                input.value += words[current++];
                pressSpace();
            }, delay);
    };

go(60000 / words.length);
&lt;/pre&gt;&lt;br /&gt;
This is a very slightly modified version of the first script which inserts a delay between each word that's "typed in".  The delay, in milliseconds, is specified as an argument to the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;go&lt;/span&gt; function.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Running the scripts&lt;/h4&gt;To run the scripts, open Firefox, head over to to http://speedtest.10-fast-fingers.com/, launch &lt;a target="_blank" href="http://getfirebug.com/"&gt;Firebug&lt;/a&gt; and paste the script in the console.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-S5G5vo0_Fzc/Trg_JMQHEPI/AAAAAAAABFU/AroRH5t5xW4/s1600/firebugscript.png" /&gt;&lt;/div&gt;&lt;br /&gt;
Now press CTRL+ENTER and watch the magic happen.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogmusic"&gt;&lt;script&gt;
kaholic.blogmusic([
{ artist : 'Luv Machine',
  name : "Witches Wand",
  link : 'http://grooveshark.com/s/Witches+Wand/39uajD?src=5'
}
]);
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/Knowledge-aholic/~4/fb8-jMK31tU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/Knowledge-aholic/~3/fb8-jMK31tU/cheating-at-typing-test-with-javascript.html</link><author>noreply@blogger.com (Andreas Grech)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://img.youtube.com/vi/kC4n_lqFB64/default.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.dreasgrech.com/2011/11/cheating-at-typing-test-with-javascript.html</feedburner:origLink></item></channel></rss>
