<?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>Earnest Apathy &#187; userscripts</title>
	<atom:link href="http://blog.pyrolupus.com/category/javascript/userscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pyrolupus.com</link>
	<description>The meaning of one life</description>
	<lastBuildDate>Mon, 20 Apr 2009 18:27:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery Detector Overkill</title>
		<link>http://blog.pyrolupus.com/2008/07/jquery-detector-overkill/</link>
		<comments>http://blog.pyrolupus.com/2008/07/jquery-detector-overkill/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 02:28:10 +0000</pubDate>
		<dc:creator>pyrolupus</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[userscripts]]></category>

		<guid isPermaLink="false">http://blog.pyrolupus.com/?p=10</guid>
		<description><![CDATA[With the ideas, help, and suggestions of some very smart folks, I&#8217;ve been able to put together a userscript that displays whether jQuery is used on the current page&#8212;along with version&#8212;and can also load jQuery into the current page by clicking the indicator if jQuery is not already present.

In early 2007, Paul Bakaus wrote a [...]]]></description>
			<content:encoded><![CDATA[<p>With the ideas, help, and suggestions of some very smart folks, I&#8217;ve been able to put together a userscript that displays whether jQuery is used on the current page&#8212;along with version&#8212;and can also load jQuery into the current page by clicking the indicator if jQuery is not already present.<br />
<span id="more-12"></span><br />
In early 2007, Paul Bakaus wrote a <a href="http://paul.jquery.com/jquerydetector.user.js">nifty userscript</a> that displays the jQuery icon (<img src='http://jquery.com/images/favicon.png' width='14' height='14' alt='jQuery Logo' class='alignnone' style='border:0;margin:0;padding:0 2px;vertical-align: middle;' />) at the bottom right hand side of the browser window when the page contains <a href="http://jquery.com">jQuery</a>.  (You need either <a href="http://opera.com/">Opera</a> or the <a href="http://www.greasespot.net/">Greasemonkey</a> plugin for <a href="http://getfirefox.com/">Firefox</a> to run userscripts.)</p>
<p>In a thread on the <a href="http://groups.google.com/group/jquery-en">jQuery discussion group</a> that (re)announced Paul&#8217;s userscript, <a href="http://groups.google.com/group/jquery-en/browse_thread/thread/2439fc066ea47d9a/5c4b95261f8206bd#dcbc48cfe398cf9f">Karl Swedberg suggested</a> putting the version of jQuery in the image&#8217;s title attribute so it would show as a tooltip when you hover over it. As I am curious about when each site started using jQuery and what capabilities they have available on their site, I thought that was a fantastic idea. So, I copied <a href="http://paul.jquery.com/jquerydetector.js">Paul&#8217;s detector script</a> to my own server, and added in Karl&#8217;s suggestion (modifying my userscript to point to my modded detector script).</p>
<p>I had been (and am still) having some problems with the version tooltip showing on my home browser&#8211;some plug-in or configuration issue, no doubt&#8211;so I cobbled together something that would display the <a href="http://pyrolupus.com/js/jquerydetector.version.js">page&#8217;s jQuery version</a> in a div when I clicked on the icon.</p>
<p>Now, on a separate track, Karl had talked about how to create a <a href="http://www.learningjquery.com/2006/12/jquerify-bookmarklet">jQuery bookmarklet</a> that would insert jQuery into pages that do not have it, in order to have jQuery&#8217;s power on the console in <a href="http://getfirebug.com/">Firebug</a> or <a href="http://opera.com/products/dragonfly/">Dragonfly</a>&#8211;or slightly more painstakingly with &#8220;javascript:&#8221; statements in the address bar for <a href="http://www.microsoft.com/windows/products/winfamily/ie/default.mspx">Internet Exploder</a> and <a href="http://www.apple.com/safari/">Safari</a> (does Safari have its own console?).  I&#8217;ve been using and loving it ever since I found it.</p>
<p>Karl recently wrote about an <a href="http://www.learningjquery.com/2008/06/updated-jquery-bookmarklet">updated jQuery bookmarklet</a> that has things like a spiffy div that pops up and tells you what it&#8217;s doing.  When I saw that and thought about the display-on-click that I&#8217;d added to my jQuery Detector, I just knew that these were two great tastes that had to, er, play golf together.</p>
<p>So, here is the &#8220;debug&#8221; version of the script, <i>i.e.</i>, with line breaks and long(er) variables names:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> version<span style="color: #339933;">=</span><span style="color: #CC0000;">0.3</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">//cheaply find version when compressed</span>
    b<span style="color: #339933;">=</span>document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    el<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    img <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    els <span style="color: #339933;">=</span> el.<span style="color: #660066;">style</span><span style="color: #339933;">,</span> ims <span style="color: #339933;">=</span> img.<span style="color: #660066;">style</span><span style="color: #339933;">,</span>
    <span style="color: #006600; font-style: italic;">//&quot;jquery is present&quot; image</span>
    jqimg<span style="color: #339933;">=</span><span style="color: #3366CC;">'http://jquery.com/images/favicon.png'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// style info div</span>
  els.<span style="color: #660066;">position</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'fixed'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">bottom</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'2px'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">right</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'20px'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">width</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'200px'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">height</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'24px'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">margin</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'0 auto 0 auto'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">padding</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'5px 10px 5px 10px'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">backgroundColor</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'#c33'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">color</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'#fff'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">fontSize</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'8pt'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">textAlign</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'center'</span><span style="color: #339933;">;</span>
  els.<span style="color: #660066;">display</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// style jquery detector icon</span>
  ims.<span style="color: #660066;">position</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'fixed'</span><span style="color: #339933;">;</span>
  ims.<span style="color: #660066;">bottom</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'2px'</span><span style="color: #339933;">;</span>
  ims.<span style="color: #660066;">right</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'2px'</span><span style="color: #339933;">;</span>
  ims.<span style="color: #660066;">zIndex</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'5'</span><span style="color: #339933;">;</span>
  ims.<span style="color: #660066;">cursor</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'pointer'</span><span style="color: #339933;">;</span>
  ims.<span style="color: #660066;">border</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'0'</span><span style="color: #339933;">;</span>
&nbsp;
  b.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  b.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// function overhead in order to rebind click event after loading jQuery</span>
  <span style="color: #006600; font-style: italic;">// showJq(): img.click = display version of jQuery currently loaded</span>
  <span style="color: #003366; font-weight: bold;">function</span> showJq<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> jq<span style="color: #339933;">=</span>jQuery<span style="color: #339933;">,</span> v <span style="color: #339933;">=</span> jq.<span style="color: #660066;">fn</span>.<span style="color: #660066;">jquery</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//&quot;if&quot; protects jquery.com from needless rechecking</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>img.<span style="color: #660066;">src</span><span style="color: #339933;">!=</span>jqimg<span style="color: #009900;">&#41;</span> img.<span style="color: #660066;">src</span><span style="color: #339933;">=</span>jqimg<span style="color: #339933;">;</span>
    img.<span style="color: #660066;">title</span><span style="color: #339933;">=</span>v<span style="color: #339933;">;</span>
    el.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'Using jQuery v'</span><span style="color: #339933;">+</span>v<span style="color: #339933;">;</span>
    jq<span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      els.<span style="color: #660066;">display</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
      window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        jq<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #006600; font-style: italic;">//jquery exists; display version on icon click</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> jQuery <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    showJq<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">//no jquery; show bland icon and load jquery on click</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    img.<span style="color: #660066;">src</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'http://jquery.com/images/ics.png'</span><span style="color: #339933;">;</span>
    img.<span style="color: #660066;">title</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'Load jQuery'</span><span style="color: #339933;">;</span>
    img.<span style="color: #660066;">onclick</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//todo: rebind click event to above</span>
      <span style="color: #003366; font-weight: bold;">var</span> s<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      s.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'head'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #006600; font-style: italic;">// loadJQuery(): recursive calls until jQuery finishes loading</span>
      <span style="color: #003366; font-weight: bold;">function</span> loadJQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        el.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'jQuery Loading...'</span><span style="color: #339933;">;</span>els.<span style="color: #660066;">display</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">&amp;</span>#038<span style="color: #339933;">;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> jQuery <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span>loadJQuery<span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//recheck every x milliseconds</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>    <span style="color: #006600; font-style: italic;">//don't try rechecking more than y times</span>
          el.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'Load jQuery failed&lt;br/&gt;(try again?)'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>    <span style="color: #006600; font-style: italic;">//finished loading; show version and fade out</span>
          img.<span style="color: #660066;">onclick</span><span style="color: #339933;">=</span>showJq<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//rebind to just show version</span>
          el.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'jQuery loaded, v'</span><span style="color: #339933;">+</span>jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">jquery</span><span style="color: #339933;">;</span>
          img.<span style="color: #660066;">src</span><span style="color: #339933;">=</span>jqimg<span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">//reset img url to jquery icon</span>
          window.<span style="color: #660066;">setTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            jQuery<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      loadJQuery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">//start loading jquery</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once that&#8217;s done, all that&#8217;s left is to churn this (much heavier than Paul&#8217;s original) script through the <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a> to cut its size down a bit.  I also turned on compression for my scripts directory to get even more mileage out of it.</p>
<p>I have not yet gotten this working in Opera 9.5x, though Paul&#8217;s original script worked fine in 9.2x.</p>
<p>Each of the resulting scripts are available below:</p>
<ul>
<li><a href="http://pyrolupus.com/js/jquerydetector.user.js">jQuery Detector userscript</a> (for Greasemonkey/user scripts)</li>
<li><a href="http://pyrolupus.com/js/jqd.js">jQuery Detector Script</a> (referenced by the userscript)</li>
<li><a href="http://pyrolupus.com/js/jqd.debug.js">jQuery Detector Script &#8211; debug</a> (verbose)</li>
</ul>
<p>Can you find problems or think of improvements&#8212;or give me tips on why I&#8217;ve broken it in Opera?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pyrolupus.com/2008/07/jquery-detector-overkill/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
