detectsniff.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Detect if a web page sniffs the user agent or not.
  2. var page = require('webpage').create(),
  3. system = require('system'),
  4. sniffed,
  5. address;
  6. page.onInitialized = function () {
  7. page.evaluate(function () {
  8. (function () {
  9. var userAgent = window.navigator.userAgent,
  10. platform = window.navigator.platform;
  11. window.navigator = {
  12. appCodeName: 'Mozilla',
  13. appName: 'Netscape',
  14. cookieEnabled: false,
  15. sniffed: false
  16. };
  17. window.navigator.__defineGetter__('userAgent', function () {
  18. window.navigator.sniffed = true;
  19. return userAgent;
  20. });
  21. window.navigator.__defineGetter__('platform', function () {
  22. window.navigator.sniffed = true;
  23. return platform;
  24. });
  25. })();
  26. });
  27. };
  28. if (system.args.length === 1) {
  29. console.log('Usage: detectsniff.js <some URL>');
  30. phantom.exit(1);
  31. } else {
  32. address = system.args[1];
  33. console.log('Checking ' + address + '...');
  34. page.open(address, function (status) {
  35. if (status !== 'success') {
  36. console.log('FAIL to load the address');
  37. phantom.exit();
  38. } else {
  39. window.setTimeout(function () {
  40. sniffed = page.evaluate(function () {
  41. return navigator.sniffed;
  42. });
  43. if (sniffed) {
  44. console.log('The page tried to sniff the user agent.');
  45. } else {
  46. console.log('The page did not try to sniff the user agent.');
  47. }
  48. phantom.exit();
  49. }, 1500);
  50. }
  51. });
  52. }