<?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>Patrick Tulskie &#187; plugin</title>
	<atom:link href="http://www.patricktulskie.com/tag/plugin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.patricktulskie.com</link>
	<description>Building a Better Internet</description>
	<lastBuildDate>Wed, 16 Jun 2010 19:12:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GreaseMonkey for Newbs</title>
		<link>http://www.patricktulskie.com/2008/06/greasemonkey-for-newbs/</link>
		<comments>http://www.patricktulskie.com/2008/06/greasemonkey-for-newbs/#comments</comments>
		<pubDate>Sun, 22 Jun 2008 05:22:52 +0000</pubDate>
		<dc:creator>Patrick Tulskie</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[greasemonkey]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.patricktulskie.com/?p=8</guid>
		<description><![CDATA[I won&#8217;t sit here and proclaim myself to be some god of Grease Monkey but I do know a thing or two about Java Script.  Basically, for those of you who are clueless as to what Grease Monkey is, it&#8217;s a plugin for Firefox that allows you to manipulate certain pages you visit using Java [...]]]></description>
			<content:encoded><![CDATA[<p>I won&#8217;t sit here and proclaim myself to be some god of Grease Monkey but I do know a thing or two about Java Script.  Basically, for those of you who are clueless as to what Grease Monkey is, it&#8217;s a plugin for Firefox that allows you to manipulate certain pages you visit using Java Script.  Now that we know what it is and have tons of ideas flowing through our heads, let&#8217;s just do something kinda cool.</p>
<p>Today we&#8217;re going to beat up on certain gallery pages that use images of the format &#8220;imageName.sized.jpg&#8221;.  What I mean by this is &#8211; we&#8217;re going to take the page (which is usually filled with junk) and replace everything with just the full size image and the caption.</p>
<p>Let&#8217;s get some assumptions out of the way:<br />
You know SOME Java Script, the site you&#8217;re visiting also has the non-sized images in the same directory as the sized images, and you don&#8217;t care what else is on the web page.  We&#8217;re also assuming there is only one caption with no &#8220;id&#8221; and we only have a style class to work with.</p>
<p>Ok?  Ok.  Let&#8217;s go.</p>
<p><span id="more-8"></span></p>
<p>Once you&#8217;ve installed GreaseMonkey, set it up, and got it going then you&#8217;ll want to make a new user script.  The namespace isn&#8217;t terribly important right now so just make it the URL for your website or something like that and continue on.  Once you&#8217;ve specified your edittor of choice, it will open up and you&#8217;ll basically have a blank canvas to work with.  The code you enter here will be run when the page is completed.  First thing we&#8217;re going to do is get the caption with this code:</p>
<blockquote><p>function getCaption()<br />
{<br />
var all = document.all ? document.all :<br />
document.getElementsByTagName(&#8217;p');<br />
var theCaption;<br />
for (var e = 0; e &lt; all.length; e++)<br />
{<br />
if (all[e].className == &#8220;pcaption&#8221;)<br />
{<br />
theCaption = all[e];<br />
}<br />
}<br />
return theCaption;<br />
}</p></blockquote>
<p>What we&#8217;re doing there is grabbing all of the elements of the document that are paragraphs (p tags) and then looping through until we find that &#8220;pcaption&#8221; class.  You might need to look at the gallery you&#8217;re visiting to get the exact names but that&#8217;s the basic gist of it.  Now let&#8217;s grab the image and replace the page with it and our caption:</p>
<blockquote><p>var theImage, unSizedImage, unSizedPath;<br />
theImage = document.getElementById(&#8217;galleryImage&#8217;);<br />
theCaption = getCaption();</p>
<p>if (theImage)<br />
{<br />
unSizedPath = theImage.src.replace(/.sized/,&#8221;");<br />
unSizedImage = document.createElement(&#8217;img&#8217;);<br />
unSizedImage.src = unSizedPath;<br />
document.body.innerHTML = &#8220;&lt;center&gt;&#8221; + theCaption.innerHTML + &#8220;&lt;br /&gt;&lt;img src=\&#8221;" + unSizedImage.src + &#8220;\&#8221; /&gt;&lt;/center&gt;&#8221;;<br />
}</p></blockquote>
<p>First we just create some variables and get our caption from before.  Once we have that all setup we say if you find an image with the id of &#8220;galleryImage&#8221; then we&#8217;re gonna do some work.   We grab the src for the image, strip out the .sized portion, and then assign that source to a new blank set of image tags.  Lastly we take the body&#8217;s innerHTML and assign it our caption&#8217;s innerHTML and our newly grabbed image.</p>
<p>In this little exercise we learned how to totally modify the contents of a page with a new image from &#8220;somewhere else.&#8221;  We also learned how to get a set of tags using the class for instances where we don&#8217;t have an id.  We also did some very basic string replacing and innerHTML utilization and manipulation.  Pretty cool huh?</p>
<p>Now you can toy with it anyway you want.  There&#8217;s a lot of fun stuff in there.  For more Grease Monkey tutorials and guides you can visit http://diveintogreasemonkey.org/  Have something you&#8217;re looking to accomplish?  Well, just comment to this post with a suggestion.</p>
<p>Note: Wordpress is really awesome at stripping out whitespace from stuff.  Sorry about the poor code formatting as a result.  I will be looking into a better way to display this stuff so it doesn&#8217;t happen in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.patricktulskie.com/2008/06/greasemonkey-for-newbs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
