Reputation: 4597
I have a bash script using curl that download a page, then use grep and sed to extract javascript inside the html block to a file, so after it I use node to evaluate and use javascript downloaded. is something like:
curl 'http://...' ... | grep -E "(varxpto\(|fnxpto)" | sed 's|<[/]\?script[^>]*>||g' > fn.js
x="$(node -pe "var fs = require('fs'); eval( fs.readFileSync('fn.js')+'' );
var val=fnxpto('${PW}'); val;")"
it works like a charm using bash. but I need to expose it as a service, so I trying to do it in nodejs.
My problem is... how to do it? I tried xpath but seems it needs xmldoc as prereq and xmldoc do not parse my html (it think it's exclusive for xml, not html).
Not what I want, but I trying to exec the grep/sed too as workarround for my problem.
NOTE: I have the html text recovered using require('http') I don't need help here. Only on extract javascript from the html and import/evaluate it.
Anyone has any idea of how can I extract javascript text script from a html and evaluate it in node?
Upvotes: 0
Views: 1296
Reputation: 106726
You could use something like cheerio
to parse the HTML and then query the document for script tags:
// `data` is the entire string response from `http.request()`
var cheerio = require('cheerio'),
$ = cheerio.load(data);
$('script').each(function(i, elem) {
console.dir($(this).text());
// do eval() or whatever else here
});
Upvotes: 2