<?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>tomputer &#187; wtframework</title>
	<atom:link href="http://droza.net/blog/tag/wtframework/feed/" rel="self" type="application/rss+xml" />
	<link>http://droza.net/blog</link>
	<description>Software. Gadgets. Music. Rants. By Tom D'Roza</description>
	<lastBuildDate>Tue, 09 Mar 2010 19:10:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>TinyURL Bookmarklet</title>
		<link>http://droza.net/blog/2008/12/03/tinyurl-bookmarklet/</link>
		<comments>http://droza.net/blog/2008/12/03/tinyurl-bookmarklet/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 22:16:16 +0000</pubDate>
		<dc:creator>tdroza</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[bookmarklet]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>
		<category><![CDATA[tinyurl]]></category>
		<category><![CDATA[wtframework]]></category>

		<guid isPermaLink="false">http://droza.net/blog/?p=97</guid>
		<description><![CDATA[If you just want to try this out, drag the link below to your bookmarks toolbar, then click the new bookmark. I&#8217;ve only tested in Firefox 3.04 so far so your mileage may vary if you use another browser. Let me know in the comments if you find this useful or have any problems.
Tiny
(v1.1 updated [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_119" class="wp-caption aligncenter" style="width: 442px"><a href="http://droza.net/blog/wp-content/uploads/2008/12/tiny_bookmarklet.gif"><img class="size-full wp-image-119" title="TinyURL bookmarklet" src="http://droza.net/blog/wp-content/uploads/2008/12/tiny_bookmarklet.gif" alt="TinyURL bookmarklet in action" width="432" height="101" /></a><p class="wp-caption-text">TinyURL bookmarklet in action</p></div>
<p>If you just want to try this out, drag the link below to your bookmarks toolbar, then click the new bookmark. I&#8217;ve only tested in Firefox 3.04 so far so your mileage may vary if you use another browser. Let me know in the comments if you find this useful or have any problems.</p>
<blockquote><p><a class="aligncenter" title="TinyURL Bookmarklet" href="javascript:var%20el=document.getElementById('__tiny');if(el){document.body.removeChild(el)}else{var%20scriptTag=document.createElement('script');scriptTag.type='text/javascript';scriptTag.src='http://json-tinyurl.appspot.com/?callback=window.tinyCallback&amp;url='+document.location;scriptTag.id='__tinyURL_script';document.body.appendChild(scriptTag)}if(!window['tinyCallback']){window.tinyCallback=function(data){var%20c=document.createElement('span');c.id='__tiny';c.style.opacity='0.7';c.style.filter='alpha(opacity=70)';c.style.border='solid%202px%20%23fff';c.style.textDecoration='none';c.style.textAlign='left';c.style.position='fixed';c.style.zIndex='9000';c.style.top='15px';c.style.right='20px';c.style.background='#000';c.style.styleFloat='right';c.style.padding='7px%2010px';c.style.color='%23fff';c.style.font='12px%20Lucida%20Grande,Helvetica,Tahoma';c.style.MozBorderRadius='5px';c.style.WebkitBorderRadius='5px';c.style.WebkitBoxShadow='0px%200px%2020px%20%23000';c.style.MozBoxShadow='0px%200px%2020px%20%23000';document.body.appendChild(c);c.innerHTML=&quot;&lt;input%20id='__tiny_text'%20type='text'%20value='&quot;+data.tinyurl+&quot;'%20style='border:0px;background:transparent;color:#fff;'/&gt;&quot;;document.getElementById('__tiny_text').focus();document.getElementById('__tiny_text').select();var%20x=document.getElementById('__tinyURL_script');if(x){document.body.removeChild(x)}}}void(0);"><span style="font-size: x-large; font-weight: bold;">Tiny</span></a></p>
<p>(v1.1 updated Dec 6th: The generated tiny url is now automatically selected ready to be copied)</p></blockquote>
<p>Read on for a description of how it works&#8230;</p>
<p><span id="more-97"></span></p>
<p><span style="text-decoration: underline;"><strong>What it does</strong></span><br />
Inspired by the <a title="WTFramework" href="http://blog.olicio.us/2008/11/08/wtframework-bookmarklet/" target="_blank">WTFramework</a> bookmarklet I decided to create a TinyURL bookmarklet that would generate a shortened url without opening a new browser window and without navigating the user away from the current page. It took me a little while to figure out how to define a callback function within a bookmarklet (thanks to <a title="Dion's blog" href="http://almaer.com/blog/">Dion Almaer</a> for a useful <a title="Dion's blog post" href="http://almaer.com/blog/translate-select-any-text-in-the-browser-and-have-it-convert-to-english-or-your-language">tutorial</a>) but I got it working in a couple of hours and I quite like it. Thanks also to <a title="About Oskar" href="http://blog.olicio.us/the-dude" target="_blank">Oskar Krawczyk</a> for the CSS which I &#8220;borrowed&#8221; from the aforementioned WTFramework.</p>
<p><span style="text-decoration: underline;"><strong>How it works<br />
</strong></span>When you click the bookmarklet, I first check whether the UI element is visible and if it is, remove it:</p>
<pre class="brush: jscript;">
var el=document.getElementById('__tiny');
if (el){
document.body.removeChild(el);
}
</pre>
<p>This makes the bookmarklet act as a toggle: Click once to show the UI, click again to remove it. The WTFramework bookmarklet also removes the UI when the user clicks on the UI element itself  but I removed that functionality from this bookmarklet as the user is likely to want to click and select the tinyurl to copy it which would be difficut if the UI disappears on mouse click.</p>
<p>If the UI is not present, I generate a URL which is called via script injection to circumvent the browser&#8217;s same origin policy (i.e. I generate as <code>script</code> tag, set its <code>src</code> attribute and add the tag to the DOM):</p>
<pre class="brush: jscript;">
var scriptTag = document.createElement('script');
scriptTag.type='text/javascript';
scriptTag.src='http://json-tinyurl.appspot.com/?callback=window.tinyCallback&amp;amp;amp;url=' + document.location;
scriptTag.id='__tinyURL_script';
document.body.appendChild(scriptTag);
</pre>
<p>The response is a JSONP formatted string. TinyURL provides a very simple API (e.g. <a title="TinyURL API example" href="http://tinyurl.com/api-create.php?url=http://scripting.com/" target="_blank">http://tinyurl.com/api-create.php?url=http://www.droza.net/blog</a>) but doesn&#8217;t offer a JSONP interface &#8211; I was going to create one with Yahoo Pipes, but found that a suitable JSONP wrapper for TinyURL already exists, hosted in Google&#8217;s AppEngine (e.g. http://json-tinyurl.appspot.com/?callback=window.tinyCallback&amp;url=http://www.droza.net/blog) so I use that instead (thanks <a title="Simon Willison" href="http://simonwillison.net/2008/Aug/27/jsontinyurl/" target="_blank">Simon</a>!).</p>
<p>After the script tag is added to the page, the browser will call the API and the response will invoke the tinyCallback function (I check to see whether the function already exists before declaring it to prevent declaring it twice if the bookmarklet is used multiple times on the same page). The callback constructs a semi-transparent UI element positioned in the top-right of the browser page and sets its content to be the generated tiny url. The bulk of the code is CSS:</p>
<pre class="brush: jscript;">
window.tinyCallback = function(data) {
var c=document.createElement('span');
c.id='__tiny';
c.style.opacity='0.7';
c.style.filter='alpha(opacity=70)';
c.style.border='solid 2px %23fff';
c.style.textDecoration='none';
c.style.textAlign='left';
c.style.position='fixed';
c.style.zIndex='9000';
c.style.top='15px';
c.style.right='20px';
c.style.background='#000';
c.style.styleFloat='right';
c.style.padding='7px 10px';
c.style.color='%23fff';
c.style.font='12px Lucida Grande,Helvetica,Tahoma';
c.style.MozBorderRadius='5px';
c.style.WebkitBorderRadius='5px';
c.style.WebkitBoxShadow='0px 0px 20px %23000';
c.style.MozBoxShadow='0px 0px 20px %23000';
document.body.appendChild(c);
c.innerHTML=data.tinyurl;
</pre>
<p>It then removes the script tag that it added to the DOM, just to keep things tidy:</p>
<pre class="brush: jscript;">
var x=document.getElementById('__tinyURL_script');
if (x){
document.body.removeChild(x);
}
</pre>
<p>After creating the script I used Dean Edward&#8217;s <a title="Dean's packer" href="http://dean.edwards.name/packer/" target="_blank">packer</a> to minify the code to a single line sans-whitespace suitable for using in the <code>href</code> attribute of an <code>a</code> tag.</p>
<p><span style="text-decoration: underline;"><strong>The full code</strong></span><br />
For reference, the listing below is the full non-minified sourcecode</p>
<pre class="brush: jscript;">
var el=document.getElementById('__tiny');
if (el){
document.body.removeChild(el);
} else {
var scriptTag = document.createElement('script');
scriptTag.type='text/javascript';
scriptTag.src='http://json-tinyurl.appspot.com/?callback=window.tinyCallback&amp;amp;amp;url=' + document.location;
scriptTag.id='__tinyURL_script';
document.body.appendChild(scriptTag);
}
if (!window['tinyCallback']) {
window.tinyCallback = function(data) {
var c=document.createElement('span');
c.id='__tiny';
c.style.opacity='0.7';
c.style.filter='alpha(opacity=70)';
c.style.border='solid 2px %23fff';
c.style.textDecoration='none';
c.style.textAlign='left';
c.style.position='fixed';
c.style.zIndex='9000';
c.style.top='15px';
c.style.right='20px';
c.style.background='#000';
c.style.styleFloat='right';
c.style.padding='7px 10px';
c.style.color='%23fff';
c.style.font='12px Lucida Grande,Helvetica,Tahoma';
c.style.MozBorderRadius='5px';
c.style.WebkitBorderRadius='5px';
c.style.WebkitBoxShadow='0px 0px 20px %23000';
c.style.MozBoxShadow='0px 0px 20px %23000';
document.body.appendChild(c);
c.innerHTML=data.tinyurl;
var x=document.getElementById('__tinyURL_script');
if (x){
document.body.removeChild(x);
}
}
}
void(0);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://droza.net/blog/2008/12/03/tinyurl-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
