I am using PhantomJS to get the html source of a page after the execution of some javascript code (namely highlight.js). Note that I'm relatively new to the use of javascript.
Here is the code of the html page ("aaaaa.html"):
<html>
<head>
<link rel="stylesheet" href="libs/highlight/styles/vs2015.min.css">
</head>
<body>
<pre>
<code class="language-c">
extern int myFunction (double toto1, double toto2);
</code>
</pre>
</body>
</html>
Here is the javascript part executed by phantomJS:
var system = require('system');
var page = require('webpage').create();
page.onLoadFinished = function(status) {
if (status === 'success') {
page.includeJs("./libs/highlight/highlight.min.js", function() {
var content = page.evaluate(function () {
var pretags = document.getElementsByTagName("code");
for (var i = 0; i < pretags.length; i++) {
if (pretags[i].parentNode.localName == "pre") {
hljs.highlightElement(pretags[i]);
}
}
system.stdout.write(page.content);
return page.content;
});
system.stdout.write(content);
phantom.exit(0);
})
} else {
system.stderr.write('ERROR Unable to load the url!');
phantom.exit(1);
}
};
page.open("aaaaa.html", function() {
page.evaluate(function() {});
});
When I execute this script I keep having the following error (using page.onError callback):
ReferenceError: Can't find variable: hljs
I don't understand how to fix this. I thought this was an issue with having to wait for the page to completely load before writing the content (hence the use of onLoadFinished) but apparently not, or I am missing something... I've also tried window.setTimeout I've checked that the path to highlight.min.js is correct (although I am not even sure that this is the right way to go to call this javascript inside the phantomjs script).
Any help will be much appreciated