I have asked this question before but it seems i got misunderstood therefore i decided to ask again but this time with alot of information:
For the lazy reader my goal here is:
Is there a way to check if a javascript is enabled on an external site either from PHP or through Ajax
Detailed description:
Okay, so my company, has many clients, when these clients sign up on our website, they are handed a javascript
. They can then use this script
on their own website to get some extra features.
Now all of the websites, that a client has, is stored within our CMS system (like there is a list of all of the websites they have that are signed with us).
Now my boss gave me the following assignment:
We need a function so that the user can press the button "Test" and the system will test if the javascript is on their site and working without errors (i.e is there another script blocking ours).
Now the past couple of days, i have been trying to find similar cases on the web
and the only thing i have been suggested, was Phantomjs however there are NO examples on how to even get close to what i want to achieve.
So my question to you:
- is it possible to check if a site has a certain javascript.
- is it possible to check if the javascript is running without errors(if the console throws errors or not)
I hope, that i wont get misunderstood this time :) if you have any questions, please just leave a message, and i will respond as fast as possible!
The phantomJs Solution (origianlly posted by Elias)
This is my code:
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.marcrasmussen.com',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.apply(scripts, [function(elem)
{//check all loaded scripts
if (/jquery.js/.js.js.test(elem.getAttribute('src')))
{//this src attribute refers your script
usesOurScript = true;//set to true
}
}]);
return {page: location.href, found: usesOurScript};//return its href and the use-status
});
//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();
});
And there is no output it just keeps running forever when i run it from php:
Test.php
<?php echo shell_exec('phantomjs hello.js');
?>
See Question&Answers more detail:os