I wrote a Node script that scrapes content from urbandictionary.com. It outputs an array of 10 words describing whatever page it's directed at.
Now I want to attach it to an HTML page that lets you write your name in an input box, then see the words that describe you. It worked great while I was running the script from the console, but now that I've included it as a script in an HTML file it doesn't work.
The Node script seems like it stops at the very beginning when it requires request, cheerio & lodash.
I tried including them in the HTML head, but that doesn't seem to be working either.
So my question is this: How do I give the Node script access to the resources it needs (lodash, request, cheerio) and still access it from the HTML file? Should I even be using Node? I thought about AJAX but all the intro tutorials on it said you can only request from the same domain you're on.
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>urbancloud</title>
<link rel="stylesheet" type="text/css" href="jqcloud.css" />
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="jqcloud-1.0.4.min.js"></script>
<script type="text/javascript" src="scrape.js"></script>
</head>
<body>
<input type="text">
<button id="nameRefresh">Click</button>
<div id="cloud" style="width: 550px; height: 350px";></div>
</body>
</html>
scrape.js
var request = require('request');
var cheerio = require('cheerio');
var _ = require('lodash-node');
var cloudWords = [];
var i;
var url = 'http://www.urbandictionary.com/define.php?term=James';
request(url, function(err, resp, body) {
var counts = {};
if (err)
throw err;
$ = cheerio.load(body); //setup the DOM
var descrip = $('.meaning').text() //.meaning is the user-submitted description on urbandictionary.com
// \/ \/ \/ parse the string, put the words into an array \/ \/ \/
descrip = descrip.replace(/[\r\n\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").toLowerCase().split(" ").sort(); /
descrip = _.uniq(descrip);
descrip = _.without(descrip, 'very', 'which', 'well', 'was', 'who', 'with', 'would', 'your', 'un', 'i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it', 'to', 'their', 'them', 'la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www',"a","able","about","across","after","all","almost","also","am","among","an","and","any","are","as","at","be","because","been","but","by","can","cannot","could","dear","did","do","does","either","else","ever","every","for","from","get","got","had","has","have","he","her","hers","him","his","how","however","i","if","in","into","is","it","its","just","least","let","like","likely","may","me","might","most","must","my","neither","no","nor","not","of","off","often","on","only","or","other","our","own","rather","said","say","says","she","should","since","so","some","than","that","the","their","them","then","there","these","they","this","tis","to","too","twas","us","wants","was","we","were","what","when","where","which","while","who","whom","why","will","with","would","yet","you","your","ain't","aren't","can't","could've","couldn't","didn't","doesn't","don't","hasn't","he'd","he'll","he's","how'd","how'll","how's","i'd","i'll","i'm","i've","isn't","it's","might've","mightn't","must've","mustn't","shan't","she'd","she'll","she's","should've","shouldn't","that'll","that's","there's","they'd","they'll","they're","they've","wasn't","we'd","we'll","we're","weren't","what'd","what's","when'd","when'll","when's","where'd","where'll","where's","who'd","who'll","who's","why'd","why'll","why's","won't","would've","wouldn't","you'd","you'll","you're","you've");
//take 10 random words to put into a text cloud
for (var i = 0; i < 10; i++) {
cloudWords[i] = descrip[Math.floor(Math.random()*descrip.length)];
}
console.log(cloudWords);