Christian Heilmann

Author Archive

Introduction to W3C widgets – my presentation at Brighton Barcamp 4

Sunday, September 6th, 2009

This saturday I went down to Brighton to attend the fourth Brighton Barcamp. As you are supposed to give a talk and PPK had stopped over on his way to the airport I thought the time right to give a talk about what I learnt about W3C widgets working with PPK and Vodafone the last few weeks:

  • Whilst W3C widgets are nothing groundbreakingly new, they allow you to use web technologies (CSS, HTML, JS) on lots and lots of mobile phones
  • The main idea about a widget is not what technology you use but how useful the end product is. My submission for the summer of widgets competition is neither a visual nor a technical masterpiece, but it still won a weekly prize because of its usefulness.
  • W3C Widgets are a great opportunity to bring our web development skillsets to a new market and break into a very closed environment
  • It is a lot of fun to build them, as you are not tethered by bad browser implementations and you can use clever things in your JavaScript

Check out the slidecast on SlideShare:

The audio quality is not the best mainly because of the location (in the basement) and Simon Willison delivering a very loud talk about 5 meters down the hall.

You can also get the audio recording – 17MB MP3 from archive.org

TTMMHTM: Apptember, fonts to embed, time API, mobile frameworks and charity hacks

Wednesday, September 2nd, 2009

Things that made me happy this morning:

  • There is a new cafe in my street: Maria e Munti - new cafe around my streetMaria e Munti - new cafe around my street
  • Font Squirrel has a repository of free fonts to embed via @font-face
  • Apptember is a one-month hackathon, no rules, no business model, no prices – just build some good stuff
  • Man builds house from Lego – now this is dedication.
  • Some very impressive photorealistic Star Wars Illustrations
  • Blockchalk has an API now and I am impressed with the ease of approach to keep it totally open
  • Both jQTouch and Pretty Mobile are frameworks to make mobile web development easier
  • Baseline is a typographical CSS framework
  • The Time API is a very clever semantic API that understands natural language to turn it into timestamps
  • Browser for the better is a Microsoft campaign that provides food for homeless people when you upgrade to IE8. Seems a bit stretched, but hey upgrade is good!
  • Charity Hack is a hackday by Paypal an Justgiving on 19-20th of September in London
  • Top Trumps 3D is an interesting concept of Top Trumps with 2D barcodes that turn into augmented reality games.

Converting a data table on the web to an autocomplete translator with YQL and YUI

Monday, August 31st, 2009

During the Summer of Widgets hack event last weekend, Tomas Caspers, Nina Wieland and Jens Grochdreis had the idea of creating a translation tool to translate from the local Cologne accent to German and back.

For this, they found a pretty impressive data source on the web, namely this web site by Reinhard Kaaden. The task was now to turn this into a fancy interface to make it easy for people to enter a “Kölsch” term and get the German equivalent and vice versa. For this, I proposed YQL und YUI and here is a step-by-step explanation of how you can do it.

You can see the final outcome here: Deutsch-Kölsch übersetzer
or by clicking the screenshot:

Deutsch-Koelsch Uebersetzer by  you.

Step 1: Retrieve and convert the data

A very easy way to get data from the web is using YQL. In order to get the whole HTML of the source page all we had to do is select * from html where url='http://www.magicvillage.de/~reinhard_kaaden/d-k.html'. That gave us the whole data though and we only wanted to get the content of the tables.

Using Firebug and looking up some XPATH we came up with the following statement that would give us the language pairs as German-Koelsch inside paragraphs: //table[1]/tr/td/p[not(a)]. The not(a) statement is needed to filter out the A-Z navigation table cells. We chose JSON as the output format in YQL and dktrans as the callback function name.

All in all this gave us a URL that would load the data we wanted and send it to the function dktrans once it has been pulled:



All that had to go in there to create the Autocomplete controls was more or less 100% copied from the simple Autocomplete example on the YUI site.
First thing is to get some handlers to the input fields I want to populate with the translation data:

var di = YAHOO.util.Dom.get('deutschinput');
var ci = YAHOO.util.Dom.get('koelschinput');

Then you need to instantiate the data source for the autocomplete and give it the language array. As a responseSchema you can define a field called term:

dktransdata.cologneDS = new YAHOO.util.LocalDataSource(
dktransdata.koelsch
);
dktransdata.cologneDS.responseSchema = {fields:['term']};

Next you need to instantiate the AutoComplete widget. This one gets three parameters: the input element, the output container and the data source. You can set useShadow to get a small dropshadow on the container:

dktransdata.cologneAC = new YAHOO.widget.AutoComplete(
'koelschinput','koelschoutput',dktransdata.cologneDS
);
dktransdata.cologneAC.useShadow = true;

This turns the input of the Cologne language into an Autocomplete, but it doesn’t yet populate the other field. For this we need to subscribe to the itemSelectEvent of the AutoComplete widget. The event handler of that event gets a few parameters, the text content of the chosen element is the first element of the third element in the second parameter (this is explained in detail on the YUI site). All you need to do is set the value of the other field to the corresponding element of the translation maps we defined:

dktransdata.cologneAC.itemSelectEvent.subscribe(cologneHandler);
function cologneHandler(s,a){
di.value = dktransdata.dk[a[2][0]];
}

All that is left is to do the same for the German to Cologne field:


dktransdata.germanDS = new YAHOO.util.LocalDataSource(
dktransdata.deutsch
);
dktransdata.germanDS.responseSchema = {fields:['term']};
dktransdata.germanAC = new YAHOO.widget.AutoComplete(
'deutschinput','deutschoutput',dktransdata.germanDS
);
dktransdata.germanAC.useShadow = true;
dktransdata.germanAC.itemSelectEvent.subscribe(germanHandler);
function germanHandler(s,a){
ci.value = dktransdata.kd[a[2][0]];
}

Step 5:Putting it all together

You can see the full source of the translation tool on GitHub and can download it there, too.
Of course we are not really finished here as this only works in JavaScript environments. As the translator was meant to be a widget though, this was not an issue. That the autocomplete does not seem to work on mobiles is one, though :).

Making this work without JavaScript would be pretty easy, too. As the data is returned in JSON we can also use this in PHP and write a simple form script If wanted, I can do that later.

Tutorial: scraping and turning a web site into a widget with YQL

Tuesday, August 25th, 2009

During the mentoring sessions at last weekend’s Young Rewired State one of the most asked questions was how you can easily re-use content on the web. The answer I gave was by using YQL and I promised a short introduction to the topic so here it is. What we are going to do here and now is to turn a web sites into a widget with YQL and a few lines of JavaScript:

Turning a web page into a widget with yql by  you.

Say you have a web site with a list of content and you want to turn it into widget to include in other web sites. For example this list of funny TV facts (which is really a Usenet classic). The first thing you need to do with this is to find out its structure, either by looking at the source code of the page or by using Firebug:

Note: The original joke site is dead, so I fixed the widget to use another one. The concept still works though.

Finding out the HTML structure by using firebug by  you.

If you right-click on the item in Firebug you can get the XPATH to the element you want to reach – we’ll need this later. In this case the xpath is /html/body/ul/li[92] which gets us that single element. If we want all TV facts, then we need to shorten this to //ul/li.

Copying the XPATH in firebug by  you.

The next step is to go to the YQL console and enter the following statement.

select * from html where url='http://www.dcs.gla.ac.uk/~joy/fun/jokes/TV.html' and xpath='//ul/li'

This follows the syntax select * from html where url='{url}' and xpath='{xpath}'. This will result in YQL pulling the page content and giving it back to us as XML:

Yahoo! Query Language - YDN by  you.

Notice that YQL has inserted P elements in the results. This is because YQL runs the XML through HTML Tidy to remove invalid HTML. This means that we need to alter our XPATH to be //ul/li/p to get to the texts.

The next step is to define the output format as JSON, define a callback function with the name funfacts, hit the test button, wait for the results and copy and paste the REST query.

Steps to get the HTML in the right format by  you.

That’s all you need to do. You will now have the HTML as a JavaScript-readable object and all you need to do is to define a function called funfacts that gets the data from YQL and add another SCRIPT node with the REST URL you copied from YQL as the src attribute:


The function will get the data from YQL as you were able to see in the console. Therefore getting to the TV facts is as easy as accessing o.query.results.p.

The rest of the functionality is plain and simple DOM Scripting. Check the comments for explanations:

Funny TV facts

Some funny TV facts

Add a bit of styling and you’ll end up with quite a cool little widget powered by the data on the jokes site. Check the source of the demo to see all the CSS needed.

That is all there is to it – get scraping!

TTMMHTM: 8 bit lego animation, blind phreaker, code collaboration, uk postcodes and SVG for IE

Monday, August 24th, 2009

Things that made me happy this morning: