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

<channel>
	<title>platform agnostic &#187; javascript</title>
	<atom:link href="http://www.deepakg.com/prog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deepakg.com/prog</link>
	<description></description>
	<lastBuildDate>Tue, 29 Jun 2010 19:08:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Hacking Chrome Developer Tools Protocol for fun and profit</title>
		<link>http://www.deepakg.com/prog/2010/06/hacking-chrome-developer-tools-protocol-for-fun-and-profit/</link>
		<comments>http://www.deepakg.com/prog/2010/06/hacking-chrome-developer-tools-protocol-for-fun-and-profit/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 17:49:40 +0000</pubDate>
		<dc:creator>deepakg</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.deepakg.com/prog/?p=116</guid>
		<description><![CDATA[I recently came across a tip on Hacker News which along with Firefox, MozRepl plugin and this snippet: autocmd BufWriteCmd *.html,*.css,*.gtpl :call Refresh_firefox() function! Refresh_firefox() if &#38;modified write silent !echo 'vimYo = content.window.pageYOffset; \ vimXo = content.window.pageXOffset; \ BrowserReload(); \ content.window.scrollTo(vimXo,vimYo); \ repl.quit();' &#124; \ nc localhost 4242 2&#62;&#38;1 &#62; /dev/null endif endfunction allows you [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across a tip on <a href="http://news.ycombinator.com/">Hacker News</a> which along with Firefox, <a href="http://wiki.github.com/bard/mozrepl/">MozRepl plugin</a> and <a href="http://vim.wikia.com/wiki/Refresh_Firefox_%28preserving_scroll%29_on_Vim_save,_using_MozRepl">this snippet</a>:</p>
<pre>autocmd BufWriteCmd *.html,*.css,*.gtpl :call Refresh_firefox()
function! Refresh_firefox()
  if &amp;modified
    write
    silent !echo  'vimYo = content.window.pageYOffset;
                 \ vimXo = content.window.pageXOffset;
                 \ BrowserReload();
                 \ content.window.scrollTo(vimXo,vimYo);
                 \ repl.quit();'  |
                 \ nc localhost 4242 2&gt;&amp;1 &gt; /dev/null
  endif
endfunction
</pre>
<p>allows you to refresh a tab in Firefox the moment you save your edits in Vim. No longer do you need to switch to Firefox, hit refresh to see the edits and then come back to your work. This is especially useful when you are on a multi-monitor setup.</p>
<p>My primary browser these days is Google Chrome. I was wondering if such a thing would be possible with Chrome too. It so happens it is. If you start Chrome with &#8211;remote-shell-port=9222 switch (on my Mac I do it like this: ~/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome &#8211;remote-shell-port=9222), you can connect to it using a TCP socket over port 9222 and then issue it commands using <a href="http://code.google.com/p/chromedevtools/wiki/ChromeDevToolsProtocol">Chrome Dev Tools Protocol</a>.</p>
<p>I wrote a small <a href="/prog/code/ChromeTool.pm">Perl wrapper</a> around the protocol and then wrote another simple script that simply refreshes the last open tab in your browser:</p>
<pre>
#refresh.pl

use strict;
use ChromeTool;

my $chrome = ChromeTool->new;
if($chrome) {
    my $tabs = $chrome->tabs;
    if(scalar(@$tabs) > 0) {
        $chrome->eval($tabs->[-1]->[0],
                                 "javascript:window.location.reload()");
    }
}
</pre>
<p>I modified the original Vim snippet mentioned above so that each time I save my code, both the browsers get auto-refreshed:</p>
<pre>
autocmd BufWriteCmd *.html,*.htm,*.css,*.gtpl,*.tt2 :call Refresh_firefox()
function! Refresh_firefox()
  if &#038;modified
    write
    silent !echo  'vimYo = content.window.pageYOffset;
                 \ vimXo = content.window.pageXOffset;
                 \ BrowserReload();
                 \ content.window.scrollTo(vimXo,vimYo);
                 \ repl.quit();'  |
                 \ nc localhost 4242 2>&#038;1 > /dev/null

    <b>silent !perl -I/Users/deepakg/Projects /Users/deepakg/Projects/refresh.pl</b>
  endif
endfunction
</pre>
<p>Of course, you&#8217;ll need to change /Users/deepakg/Projects to where you save the ChromeTool.pm file and refresh.pl. Here is a quick screencast showing this in action:</p>
<p><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/AizqXBAiQy4&#038;hl=en_US&#038;fs=1&#038;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/AizqXBAiQy4&#038;hl=en_US&#038;fs=1&#038;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></p>
<p><b>Credits:</b> I learned about existence of Chrome Developer Tools Protocol from <a href="http://blog.8-p.info/2009/11/chrome-repl">this post</a>. You can also find link to a Ruby REPL client for talking to Chrome on the same page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deepakg.com/prog/2010/06/hacking-chrome-developer-tools-protocol-for-fun-and-profit/feed/</wfw:commentRss>
		<slash:comments>-1</slash:comments>
		</item>
		<item>
		<title>Ruby-like difference between two arrays in JavaScript</title>
		<link>http://www.deepakg.com/prog/2009/01/ruby-like-difference-between-two-arrays-in-javascript/</link>
		<comments>http://www.deepakg.com/prog/2009/01/ruby-like-difference-between-two-arrays-in-javascript/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 18:15:03 +0000</pubDate>
		<dc:creator>deepakg</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[difference]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.deepakg.com/prog/?p=50</guid>
		<description><![CDATA[Ruby has a nifty feature that allows you to &#8220;subtract&#8221; two arrays. e.g. Fruit = ["Apple", "Kinnow", "Mango", "Orange"] Citrus = ["Lemon", "Kinnow", "Orange", "Tangerine"] Then Fruit &#8211; Citrus gives: ["Apple", "Mango"] Notice that elements in Citrus not in Fruit (Lemon, Tangerine) are not in the difference. Now I needed something similar in Javascript. So [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby has a nifty feature that allows you to &#8220;subtract&#8221; two arrays. e.g.<br />
<code><br />
Fruit = ["Apple", "Kinnow", "Mango", "Orange"]<br />
Citrus = ["Lemon", "Kinnow", "Orange", "Tangerine"]<br />
</code></p>
<p>Then Fruit &#8211; Citrus gives:<code><br />
["Apple", "Mango"]<br />
</code></p>
<p>Notice that elements in Citrus not in Fruit (Lemon, Tangerine) are not in the difference.</p>
<p>Now I needed something similar in Javascript. So I started by pushing my luck:<br />
<code><br />
var Fruit = ["Apple", "Kinnow", "Mango", "Orange"];<br />
var Citrus = ["Lemon", "Kinnow", "Orange", "Tangerine"];<br />
var Diff = Fruit - Citrus;<br />
</code><br />
Depending on where you are running this code, Diff will be 0 or NaN. This meant that I would have to come up with something of my own. I figured I&#8217;ll put javascript&#8217;s regular expressions to some use and came up with this:</p>
<pre>
function diffArrays (A, B) {

  var strA = ":" + A.join("::") + ":";
  var strB = ":" +  B.join(":|:") + ":";

  var reg = new RegExp("(" + strB + ")","gi");

  var strDiff = strA.replace(reg,"").replace(/^:/,"").replace(/:$/,"");

  var arrDiff = strDiff.split("::");

  return arrDiff;
}
</pre>
<p><code>diffArrays(Fruit, Citrus) </code>gives:<code><br />
["Apple", "Mango"]<br />
</code></p>
<p>The thing with dynamically typed languages is that you can take even numeric arrays through this regular expression rigmarole and get a correct result back without anyone complaining:<br />
<code><br />
var Natural = [1,2,3,4,5,6];<br />
var Prime = [1,2,3,5,7];<br />
</code></p>
<p><code>diffArrays(Natural, Prime)</code> gives:<code><br />
[4, 6]</code></p>
<p>A brief explanation of how this works:</p>
<p>I start by converting the first array to a string delimited with two colons, :: &#8211; the choice was arbitrary. The fact that regular expressions don&#8217;t treat a colon any specially helps. I then prefix and suffix the resulting string with another colon. The idea here is to have each element &#8220;surrounded&#8221; by its own pair of colons. For instance, [1,2,3,4,5,6] becomes :1::2::3::4::5::6:</p>
<p>The array to be subtracted from it &#8211; say [1,2,3,5] is converted to the form &#8211; :1:|:2:|:3:|:5: which I then use to create a regular expression. The pipe | has a special meaning in the regular expression world and will cause :1: or :2: or :3: or :5: to be matched.</p>
<p>Calling strA.replace with our just-crafted regular expression replaces :1:, :2:, :3: and :5: with &#8220;&#8221; giving us :4::6:, which I strip clean of the leading and trailing colons through another couple of replace calls. Finally, spitting the string on :: gives the delta array!</p>
<p>Now this implementation is certainly not going to win me any prizes in speed/elegance pageants, but I think this somewhat awkward application of regular expression was something worth sharing!</p>
<p>On a somewhat related note, a special thanks goes out to <a href="http://steve-yegge.blogspot.com/">Steve Yegge</a> for his wonderful <a href="http://code.google.com/p/js2-mode/">js2-mode</a> and <a href="http://code.google.com/p/ejacs/">ejacs</a>. The latter especially comes in handy when you are just &#8220;doodling&#8221; around on JavaScript problems like these.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deepakg.com/prog/2009/01/ruby-like-difference-between-two-arrays-in-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.386 seconds -->
