Skip to content

Instantly share code, notes, and snippets.

@skipjac
Created January 27, 2011 04:42
Show Gist options
  • Save skipjac/798076 to your computer and use it in GitHub Desktop.
Save skipjac/798076 to your computer and use it in GitHub Desktop.
this is the sidebar widget for the urban dictionary
<script type="text/javascript" id="urban_source">Widget.require('http://www.techassistant.net/urban_widget/urban.js', {type: 'text/javascript'});</script>
<div id="urban_content"></div>
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