2014-08-04 12:54:42 +00:00
|
|
|
/**
|
|
|
|
|
* Created by staticfunction on 8/4/14.
|
|
|
|
|
*/
|
2019-03-25 16:39:20 +00:00
|
|
|
import htmlparser = require('htmlparser2');
|
2014-08-04 12:54:42 +00:00
|
|
|
|
2019-03-25 16:39:20 +00:00
|
|
|
const options: htmlparser.DomHandlerOptions = { withEndIndices: false, withDomLvl1: true }
|
|
|
|
|
const dh = new htmlparser.DomHandler((err: Error, dom: htmlparser.DomElement[]) => {
|
|
|
|
|
if(err) {
|
|
|
|
|
throw err;
|
2014-08-04 12:54:42 +00:00
|
|
|
}
|
2019-03-25 16:39:20 +00:00
|
|
|
|
|
|
|
|
// Use DomUtils to get name of first element in dom
|
|
|
|
|
console.log(htmlparser.DomUtils.getName(dom[0]));
|
|
|
|
|
}, options);
|
|
|
|
|
dh.onopentag = (name:string, attribs:{[s:string]:string}) => {
|
|
|
|
|
if(name === "script" && attribs['type'] === "text/javascript"){
|
|
|
|
|
console.log("JS! Hooray!");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
dh.ontext = (text: string) => {
|
|
|
|
|
console.log("-->", text);
|
|
|
|
|
};
|
|
|
|
|
dh.onclosetag = () => {
|
|
|
|
|
console.log("That's it?!");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var parser = new htmlparser.Parser(dh);
|
2014-08-04 12:54:42 +00:00
|
|
|
|
|
|
|
|
parser.write("Xyz <script type='text/javascript'>var foo = '<<bar>>';</script>");
|
2014-08-07 13:10:37 +00:00
|
|
|
parser.end();
|