public
Last active

  • Download Gist
digg.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/usr/bin/env node
 
var YUI = require("yui3").YUI;
 
YUI({
debug: true
}).use('node', 'io', function(Y) {
//Messing with the main page..
Y.one('title').set('innerHTML', 'Digg News Headlines');
//Creating the list that we will append the remote data to
var ul = Y.one('body').appendChild(Y.Node.create('<ul></ul>'));
//Creating a sandboxed instance that we will bind to the remote page that we fetch
YUI().use('node', function(remotePage) {
//The page we are fetching
var url = 'http://digg.com/news';
//This will call io under the hood and get the content of the URL,
//It will then dump the content of that page into this sandboxed document.
remotePage.fetch(url, function() {
//Get all the news items from the remote page.
var newsItems = remotePage.all('#story-items h3');
//Iterate them
newsItems.each(function(n) {
//Import this "A" node into the outside instances document
var a = ul.importNode(n.one('a'), true);
//Clean up the relative URL's of hrefs
a.set('href', 'http://digg.com' + a.get('href'));
//Append the new node to the list
ul.appendChild(Y.Node.create('<li></li>')).append(a);
});
//Now, we can print the "outer" instances html and drop it to the screen
console.log(Y.one('doc').get('outerHTML'));
});
});
 
});
output.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
<html>
<head>
<title>Digg News Headlines</title>
</head>
<body>
<ul>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/is_college_a_bad_investment">Is college a bad investment?</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/29_meals_where_the_presentation_is_more_important_than_the_actual_food_itself_photofind_com_can_t_read_have_fun_anyway">29 Meals Where The Presentation Is More Important Than The Actual Food Itself | PhotoFind.com - c...</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/famous_logos_with_hidden_images">Famous Logos with Hidden Images</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/nokia_replaces_ceo_with_microsoft_exec_to_save_itself">Nokia replaces CEO with Microsoft exec to save itself</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/14_incredible_wood_sculptures">14 Incredible Wood Sculptures</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/a_look_at_apple_s_updated_iphone_configuration_utility">A look at Apple&#039;s updated iPhone Configuration Utility</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/they_won_t_even_know_what_hit_em_pic">They Won&#039;t Even Know What Hit &#039;Em [Pic]</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/blow_job_supplier_nailed_by_uk_electronics_law">Blow Job Supplier Nailed by UK Electronics Law</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/6_movies_that_didn_t_realize_they_let_the_villain_win">6 Movies That Didn&#039;t Realize They Let The Villain Win</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/qvaq_a_key_to_easy_social_communication_online">Qvaq: A Key to Easy Social Communication Online</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/5_fictional_stories_you_were_taught_in_history_class">5 Fictional Stories You Were Taught in History Class</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/blooper_reels_from_the_original_star_trek_series">Blooper Reels From The Original Star Trek Series</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/top_ten_disney_properties_underrepresented_in_kingdom_hearts">Top Ten Disney Properties Underrepresented in Kingdom Hearts</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/9_awesome_slacker_jobs_you_could_actually_have">9 Awesome Slacker Jobs You Could Actually Have</a>
</li>
<li>
<a class="story-title" target="_blank" href="http://digg.com/story/r/microsoft_xbox_live_team_says_too_gay_to_play">Microsoft Xbox Live Team Says Too Gay To Play</a>
</li>
</ul>
</body>
</html>

Dav,

This stuff is freaking awesome. YUI3 is the only major framework that I've come across that's up and running with node.js.

I wanted to give you a heads up on a error that I just encountered. I'm using a clean install of node, npm and yui3 for node.js and I receive the following error while trying to execute the above example. I'm wondering if one of the yui3 modules has changed since this example was posted?

error: (nodejsyui3): ----------------------------------------------------------
error: (nodejsyui3): TypeError: Cannot call method 'addListener' of undefined
error: (nodejsyui3): at Object.send (/usr/local/lib/node/.npm/yui3/0.5.19/package/lib/yui3-io.js:42:32)
error: (nodejsyui3): at Function.xdr (/usr/local/lib/node/.npm/yui3-core/3.3.0/package/build/io/io-min.js:2:357)
error: (nodejsyui3): at A (/usr/local/lib/node/.npm/yui3-core/3.3.0/package/build/io/io-min.js:1:3332)
error: (nodejsyui3): at [object Object].io (/usr/local/lib/node/.npm/yui3/0.5.19/package/lib/yui3-io.js:138:16)
error: (nodejsyui3): at /usr/local/lib/node/.npm/yui3/0.5.19/package/lib/yui3-yui3.js:194:18
error: (nodejsyui3): at [object Object]._notify (/usr/local/lib/node/.npm/yui3-core/3.3.0/package/build/yui/yui-debug.js:642:17)
error: (nodejsyui3): at /usr/local/lib/node/.npm/yui3-core/3.3.0/package/build/yui/yui-debug.js:629:23
error: (nodejsyui3): at [object Object]._notify (/usr/local/lib/node/.npm/yui3-core/3.3.0/package/build/yui/yui-debug.js:642:17)
error: (nodejsyui3): at [object Object]. (/usr/local/lib/node/.npm/yui3-core/3.3.0/package/build/yui/yui-debug.js:756:27)
error: (nodejsyui3): at Object._finish (/usr/local/lib/node/.npm/yui3-core/3.3.0/package/build/loader/loader-debug.js:1632:19)
error: (nodejsyui3): ----------------------------------------------------------

If your running 0.3.7, you need to pull my latest from github and npm install it.. I haven't released a patch yet for the new HTTP Client in 0.3.7

Hi davglass,

I was wondering how i can load a local file/a string to use it as document.
I saw in this gist that you do it with fetch for remote url so I bet you have made some methods to load a string or a file can you enlight me on how to do it ?

Thanks.

Hi Dav,
I am getting the error when i am running the above example by using $node digg.js::

error: (nodejsyui3): Error: Cannot find module '../build/Release/contextify'
error: (nodejsyui3): at Function._resolveFilename (module.js:326:11)
error: (nodejsyui3): at Function._load (module.js:271:25)
error: (nodejsyui3): at require (module.js:355:19)
error: (nodejsyui3): at Object. (/home/abbas/.node_libraries/.npm/contextify/0.0.6/package/lib/contextify.js:2:22)
error: (nodejsyui3): at Module._compile (module.js:411:26)
error: (nodejsyui3): at Object..js (module.js:417:10)
error: (nodejsyui3): at Module.load (module.js:343:31)
error: (nodejsyui3): at Function._load (module.js:302:12)
error: (nodejsyui3): at require (module.js:355:19)
error: (nodejsyui3): at Object. (/home/abbas/.node_libraries/.npm/jsdom/0.2.9/node_modules/contextify/index.js:11:18)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.