Main
Latest
- Tinylink API: Consumer PHP Shell Script
- Teh First Law of Pwnage
- Lithium, Batteries & Recycling
- Muti.co.za & Censoring
- Milkshakes
- Breakfast: Yogurt, ja?
- Aphophysis
- South African Telecoms Industry Violates Hacker Et...
- More Joomla! Updates
- IT Salaries
Archives
- June 2004
- July 2004
- August 2004
- September 2004
- October 2004
- November 2004
- December 2004
- January 2005
- February 2005
- March 2005
- April 2005
- May 2005
- June 2005
- July 2005
- August 2005
- September 2005
- October 2005
- November 2005
- December 2005
- January 2006
- February 2006
- March 2006
- April 2006
- May 2006
- June 2006
- July 2006
- August 2006
- September 2006
- November 2006
- December 2006
- January 2007
- February 2007
- March 2007
- April 2007
- May 2007
- June 2007
- July 2007
- August 2007
- September 2007
- October 2007
- November 2007
- December 2007
- January 2008
- February 2008
- March 2008
- April 2008
- May 2008
- June 2008
- July 2008
- August 2008
- September 2008
- October 2008
- November 2008
- December 2008
- January 2009
TinyLink, Ajaxified!
I made use of TinyLink's JSON API.
I put the following links at the bottom of my posts:
<p class="post-tinylink"><a href="http://tinylink.co.za/create.php?url=http://blog.charlvn.za.net/2007/10/tinylink-api-consumer-php-shell-script.html">Get TinyLink</a></p>
The url parameter is basically just the permalink to the post.
Then I call the following JavaScript after all the links have been loaded:
var tinylink = Array();
var tinylinkA = document.getElementsByTagName("a");
var tinylinkExpected = 0;
function tinylinkDisplay() {
tinylink.forEach(function(element, index) {
if (element != null) {
var textnode = document.createTextNode(" " + element["url"]);
tinylinkA[index].parentNode.insertBefore(textnode, tinylinkA[index].nextSibling);
tinylinkExpected--;
tinylink[index] = null;
}
});
if (tinylinkExpected > 0) {
window.setTimeout(tinylinkDisplay, 200);
}
}
function tinylinkFetch(event, count) {
event.currentTarget.removeEventListener("click", tinylinkFetch, false);
event.preventDefault();
var href = event.currentTarget.href.replace("create.php", "api/json.php");
var i = 0;
while (i < tinylinkA.length && tinylinkA[i] != event.currentTarget) i++;
href += "&name=tinylink[" + i + "]";
var script = document.createElement("script");
script.setAttribute("type", "application/javascript");
script.setAttribute("src", href);
document.getElementsByTagName("body")[0].appendChild(script);
tinylinkExpected++;
tinylinkDisplay();
}
for (var i = 0; i < tinylinkA.length; i++) {
if (tinylinkA[i].href.indexOf("http://tinylink.co.za/create.php?url=") == 0) {
tinylinkA[i].addEventListener("click", tinylinkFetch, false);
}
}
Ok, I realise this still needs a lot of work, but at least it functions in Gecko. What needs to be done:
- Test in other browsers.
- Add comments.
This script is built on the concept of "ideal" semantics where the content and behaviour is totally separated. This is also a good example of graceful degradation.
Anyway, now I go to bed. Expect improvements to this script to be published sometime into the future whenever I get time (not like I really had time now but anyway).
Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.


0 Comments
Post a Comment