<?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>Squash &#187; javascript</title>
	<atom:link href="http://balpreetpankaj.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://balpreetpankaj.com/blog</link>
	<description>Just another weblog</description>
	<lastBuildDate>Sun, 16 Oct 2011 23:32:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Greasemonkey and avoiding the sandbox</title>
		<link>http://balpreetpankaj.com/blog/2011/08/07/greasemonkey-and-avoiding-the-sandbox/</link>
		<comments>http://balpreetpankaj.com/blog/2011/08/07/greasemonkey-and-avoiding-the-sandbox/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 22:03:00 +0000</pubDate>
		<dc:creator>balpreetspankaj</dc:creator>
				<category><![CDATA[Greasemonkey]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://balpreetpankaj.com/blog/?p=82</guid>
		<description><![CDATA[Recently, I was trying to write a greasemonkey script to override the HTMLHeadElement.appendChild function in the browser DOM. Well, the reason I was trying to do that was to redirect a json-p call to my servers instead of the once mentioned in the webpage. Json-p works by dynamically adding a script tag to get around [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I was trying to write a greasemonkey script to override the HTMLHeadElement.appendChild function in the browser DOM. Well, the reason I was trying to do that was to redirect a json-p call to my servers instead of the once mentioned in the webpage. Json-p works by dynamically adding a script tag to get around the cross-domain restriction policy of browsers. So, in most cases the script tag is added to the "<head>" element. What I was trying to do was :</p>
<p><code><br />
HTMLHeadElement.prototype._appendChild = HTMLHeadElement.prototype.appendChild;</p>
<p>HTMLHeadElement.prototype.appendChild = function(node) {<br />
  if(node.nodeName == "SCRIPT") {<br />
    var new_src = "http://foo.com/?...";<br />
    node.setAttribute("src", new_src);<br />
  }<br />
  HTMLHeadElement.prototype._appendChild.apply(this,arguments);<br />
}<br />
</code></p>
<p>Well, this script worked on the Firefox console. But, it turns out it does not work as a Greasemonkey script. The reason being the greasemonkey scripts are run as part of a sandbox, and so do not have access to all the window elements. ( I wonder why ? ) . I even tried using "unsafewindow" , but even that does not work. You get access to HTMLHeadElement, but HTMLHeadElement.prototype is undefined. So, what now ?</p>
<p>Well, there is a simple workaround. Just modify your greasemonkey script to run the above code as part of the script in page. Basically, create a script tag, and add the abpve script as part of the script. Now since, this code will run in the context of the page and not in the context of the greasemonkey script, you will not have to worry about any restrictions imposed by the greaemonkey script. I basically put all my code in a separate js file and then loaded that into the script tag using greasemonkey:<br />
<code></p>
<p>var scriptElement = document.createElement("script");</p>
<p>scriptElement.setAttribute("src", "new_js.js");<br />
scriptElement.setAttribute("type", "text/javascript");</p>
<p>document.body.appendChild(scriptElement);<br />
</code></p>
<p>And, there is another advantage to writing greasemonkey scripts this way, you separate out the main javascript from the greasemonkey script. And now you can keep updating your javascript and the users can keep getting the updated functionality without them re-installing the greasemonkey script again. Sweet!</p>
]]></content:encoded>
			<wfw:commentRss>http://balpreetpankaj.com/blog/2011/08/07/greasemonkey-and-avoiding-the-sandbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a headline rotator</title>
		<link>http://balpreetpankaj.com/blog/2008/11/02/creating-a-headline-rotator/</link>
		<comments>http://balpreetpankaj.com/blog/2008/11/02/creating-a-headline-rotator/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 04:46:17 +0000</pubDate>
		<dc:creator>balpreetspankaj</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[AdevelopedWorld.com]]></category>
		<category><![CDATA[headline rotator]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Learning JQuery]]></category>

		<guid isPermaLink="false">http://balpreetpankaj.com/blog/?p=16</guid>
		<description><![CDATA[I was recently reading the book Learning Jquery by Jonathan Chaffer and Karl Swedberg . After reading the chapter on Rotators and Shufflers, which shows how you can create a headline rotator, I decided to implement one for my own website, adevelopedworld.com. adevelopedworld.com is a website which features social entrepreneurs from all around the world. [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently reading the book <strong>Learning Jquery</strong> by <em>Jonathan Chaffer and Karl Swedberg</em> . After reading the chapter on <em>Rotators and Shufflers</em>, which shows how you can create a headline rotator, I decided to implement one for my own website, <a href="http://adevelopedworld.com">adevelopedworld.com</a>. <a href="http://adevelopedworld.com">adevelopedworld.com</a> is a website which features social entrepreneurs from all around the world. We constantly get updates from the social entrepreneurs that we have featured, and we had to manually update the homepage to show these updates. After implementing the headline rotator, we just keep adding the updates to a text file, and the homepage gets automatically updated. And to top it, the homepage looks all the more dynamic.</p>
<p>The tutorial on how to create the rotator in the book is very complete. I just had to make one change. The tutorial is based on an old version of jQuery which supported XPath expressions. XPath expressions are no longer a part of JQuery. But there are two solutions :<br />
a). Either add the XPath plugin for jQuery, OR<br />
b). Change the XPath expression to a normal jQuery selector expression.</p>
<p>I used the latter.</p>
<p>A live demo of the headline rotator can be found at <a href="http://adevelopedworld.com">adevelopedworld.com.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://balpreetpankaj.com/blog/2008/11/02/creating-a-headline-rotator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

