I'll post it here, too. Since I was the guy that put you on to phantomjs, it's only fair to provide you with, what I think, is the answer to your problems.
The following script opens a page in the headless phantom browser, logs any console messages that page would yield in an actual browser, (using the onConsoleMessage
event), it also logs the error if the page were to be unable to load, and, if the page was opened without issues, it checks all <script>
tags on that page, looking for your javascript file.
var page = require('webpage').create();
page.onConsoleMessage = function( message, line, srcId)
{//show console errors for page
console.log('Page has errors showing in console: ' + msg
+ ' (line: ' + (line || '?') + ', id: ' + srcId + ')');
};
page.open('http://www.your-url-here.org',function(status)
{
if (status != 'success')
{//can't load page
console.log('Failed to open page: ' + status);
phantom.exit();
return;
}
var reslt = page.evaluate(function()
{
var usesOurScript = false,
scripts = document.querySelectorAll('script');//get all script tags
/* Array.forEach is causing errors, as it turns out...
Array.forEach.apply(scripts, [function(elem)
{//check all loaded scripts
if (/yourScriptName.js/.test(elem.getAttribute('src')))
{//this src attribute refers your script
usesOurScript = true;//set to true
}
}]);*/
for (var i=0;i<scripts.length;i++)
{
if (/yourScriptName.js/.test(scripts[i].getAttribute('src')))
{
return {page: location.href, found: true};
}
}
return {page: location.href, found: false};
});
//script was found? adjust message
reslt.found = (reslt.found ? ' uses ' : ' does not use ') + 'our script!';
console.log(reslt.page + reslt.found);//logs url does not use || uses our script
phantom.exit();
});
If that file is does something like jQ (create a global reference to some object) you might even add this to the page.evaluate
callback:
var reslt = page.evaluate(function()
{//this code will behave like it's running on the target page
var libs = {jQuery: ($ || jQuery),
myLib: ourLibsGlobalName};
return libs;
});
if (reslt.jQuery instanceof Object)
{
console.log('client uses jQ');
}
if (reslt.myLib instanceof Object)
{
console.log('client uses our lib, successfuly');//wouldn't be accessible if it contained errors
}
console.log(reslt.myLib);//shows full object, so you can verify it's the right one
Note:
This code is un-tested, I just wrote it off the top of my head. It may contain silly typo's. It's not a copy-pastable answer to all of your prayers.
I do believe the page.evaluate
callback is able to return something, however: it's running in the context of the loaded page, and is sandboxed, so you may have to force an error:
page.evaluate(function()
{
Array.forEach(document.querySelectorAll('script'), [function(elem)
{
if (/yourScript.js/.test(elem.getAttribute('src')))
{
throw {message: 'Script found',
nodeSrc: elem.getAttribute('src'),
pageUrl: location.href,
myLib : yourGlobalVar}//<-- cf remarks on jQ-like usage
}
}]);
});
By throwing this custom error, which isn't being caught, so it'll end up in the console, you force the page.onConsoleMessage
handler to deal with this exception. That could be a way around the sandbox restrictions, in a way, if they're causing the infinite loop issue to occur.