detectsniff.js 1.7 KB

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