Skip to content

Instantly share code, notes, and snippets.

@skipjac
Created January 28, 2013 23:44
Show Gist options
  • Save skipjac/4660392 to your computer and use it in GitHub Desktop.
Save skipjac/4660392 to your computer and use it in GitHub Desktop.
var UrbanDictionaryWidget = Class.create({
getRandomTerm: function() {
var url = '/proxy/direct?url=http://www.urbandictionary.com/random.php'
new Ajax.Request(url, {
method:'post',
postBody: '',
onSuccess: function(transport) {
this.getDocument(transport.responseText);
}.bind(this),
onFailure: function() {
alert("Failed");
}.bind(this)
});
},
getDocument: function(document) {
var reg = /\S*define.php\?term=(\S*)"/;
var arr = reg.exec(document);
var term = unescape(arr[1].replace(/\+/g, " "));
var targeturl = 'http://www.urbandictionary.com/define.php?term=' + escape(term);
var url = '/proxy/direct?url=' + escape(targeturl);
new Ajax.Request(url, {
method:'get',
onSuccess: function(transport) {
this.parseDocument(transport.responseText);
}.bind(this),
onFailure: function() {
alert("Failed");
}.bind(this)
});
},
parseDocument: function(document) {
var reg = /<td class=\'word\'>([\w\s]*)<\/td>/;
var term = reg.exec(document)[1];
var reg = /<div class='definition'>([\S\s]*?)<\/div>/;
var definition = reg.exec(document)[1];
var result = this.RENDER_TEMPLATE.evaluate({ term: term, definition: definition });
$('urban_content').update(result);
},
RENDER_TEMPLATE: new Template(
'<h4>#{term}</h4>#{definition}'
)
});
var urbanWidget = new UrbanDictionaryWidget();
urbanWidget.getRandomTerm();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment