From e3747826663fee9c591d69e4b0243aeb5ca3d0d2 Mon Sep 17 00:00:00 2001 From: airportyh Date: Sun, 6 Jun 2010 15:47:49 -0400 Subject: [PATCH] added support for delicious, rss/atom feeds --- delicious.html | 12 ++++++++++++ delicious.js | 4 ++++ feeds.html | 12 ++++++++++++ feeds.js | 6 ++++++ index.html | 3 +-- jsonp.js | 15 +++++++++++++++ marklet.js | 2 +- wordyclouds.js | 47 +++++++++++++++++++++++++++++++++++++++++------ 8 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 delicious.html create mode 100644 delicious.js create mode 100644 feeds.html create mode 100644 feeds.js create mode 100644 jsonp.js diff --git a/delicious.html b/delicious.html new file mode 100644 index 0000000..ec5d7a9 --- /dev/null +++ b/delicious.html @@ -0,0 +1,12 @@ + + + + Delicious + + + + + +

Delicious

+ + \ No newline at end of file diff --git a/delicious.js b/delicious.js new file mode 100644 index 0000000..3776566 --- /dev/null +++ b/delicious.js @@ -0,0 +1,4 @@ +function loadDeliciousTags(){ + WordyClouds.loadFromDelicious('airportyh') +} +window.onload = loadDeliciousTags \ No newline at end of file diff --git a/feeds.html b/feeds.html new file mode 100644 index 0000000..d928e5b --- /dev/null +++ b/feeds.html @@ -0,0 +1,12 @@ + + + + Feeds + + + + + +

Feeds

+ + \ No newline at end of file diff --git a/feeds.js b/feeds.js new file mode 100644 index 0000000..dcc969a --- /dev/null +++ b/feeds.js @@ -0,0 +1,6 @@ +function loadFeed(){ + var url = 'http://tobyho.com/user/airportyh/rss.xml' + var numEntries = 10 + WordyClouds.loadFromFeed(url, numEntries) +} +window.onload = loadFeed \ No newline at end of file diff --git a/index.html b/index.html index 59c3fd1..216edd1 100644 --- a/index.html +++ b/index.html @@ -2,10 +2,9 @@ WordyCloud - - + I've followed engadget's live blog, seen the keynote video, listened to Tom and Molly talk about it, even read others' opinions on engadget, gdgt and twitter, etc etc. I guess you could say I am pretty excited. The tech savy(engadget, gdgt, BOL, etc) are in general underwhelmed or disappointed. Below are some of the complaints I have heard: diff --git a/jsonp.js b/jsonp.js new file mode 100644 index 0000000..3b3b2d9 --- /dev/null +++ b/jsonp.js @@ -0,0 +1,15 @@ +var JSONP = {} +JSONP.get = function(url, callback){ + var scriptTag = document.createElement('script') + var callbackName = '_' + new Date().getTime() + window[callbackName] = function(){ + callback.apply(null, arguments) + delete window[callbackName] + } + if (url.indexOf('?') != -1) + url += '&callback=' + callbackName + else + url += '?callback=' + callbackName + scriptTag.src = url + document.head.appendChild(scriptTag) +} \ No newline at end of file diff --git a/marklet.js b/marklet.js index ac72fa7..f97af45 100644 --- a/marklet.js +++ b/marklet.js @@ -1 +1 @@ -main() \ No newline at end of file +WordyClouds.runBookmarklet() \ No newline at end of file diff --git a/wordyclouds.js b/wordyclouds.js index be44d00..056ecf9 100644 --- a/wordyclouds.js +++ b/wordyclouds.js @@ -111,6 +111,17 @@ function boxesOverlap(ax, ay, aw, ah, bx, by, bw, bh){ return true } +function stripHTML(oldString) { + var data = [] + var inTag = false; + for(var i = 0; i < oldString.length; i++) { + var chr = oldString.charAt(i) + if (chr == '<') inTag = true; + if (chr == '>') inTag = false; + if(!inTag) data.push(chr) + } + return data.join(''); +} /* ========== Layouts ============================ */ @@ -155,6 +166,7 @@ Layouts.randomAvoid = function RandomAvoidLayout(freq, canvas, colors, fontName) //words = words.slice(0, 200) words.forEach(function(word){ var textHeight = freq[word] * sizeScale + if (textHeight < 5) return //console.log('word[' + word + '].height = ' + textHeight) context.font = textHeight + 'px ' + fontName var textWidth = context.measureText(word).width @@ -208,18 +220,20 @@ Layouts.randomAvoid = function RandomAvoidLayout(freq, canvas, colors, fontName) }else context.fillText(word, x, y) - if (vertical) context.rotate(0) //console.log('box[' + word + ']: ' + box) }) } -/* =========== Main ============================== */ +/* =========== main entry point ========================= */ -function main(){ - var layout = Layouts.randomAvoid - var text = getText(document.body, ['script']) +WordyClouds = {} +WordyClouds.loadFromText = function(text){ var commonWords = CommonWords.english var freq = wordSummary(text, commonWords) + this.loadFromWordFreq(freq) +} +WordyClouds.loadFromWordFreq = function(freq){ + var layout = Layouts.randomAvoid var palate = ColorPalates.autumn var fontName = getComputedStyle(document.body)['font-family'] var canvas = document.createElement('canvas') @@ -232,4 +246,25 @@ function main(){ document.body.style.overflow = 'hidden' layout(freq, canvas, palate, fontName) } - +WordyClouds.loadFromDelicious = function(username){ + JSONP.get('http://feeds.delicious.com/feeds/json/tags/' + username, function(data){ + WordyClouds.loadFromWordFreq(data) + }) +} +WordyClouds.loadFromFeed = function(url, numEntries){ + numEntries = numEntries || 10 + var rand = new Date().getTime() + JSONP.get('http://www.google.com/uds/Gfeeds?context=1&num=' + numEntries + '&hl=en&output=json&q=' + url + '&v=1.0&nocache=' + rand, function(v, data){ + var entries = data.feed.entries + var text = '' + entries.forEach(function(entry){ + text += entry.title + '\n' + text += stripHTML(entry.content) + '\n' + }) + WordyClouds.loadFromText(text) + }) +} +WordyClouds.runBookmarklet = function(){ + var text = getText(document.body, ['script']) + this.loadFromText(text) +}