Path: blob/master/6-Selenium/phantomjs/examples/detectsniff.js
164 views
// Detect if a web page sniffs the user agent or not.12var page = require('webpage').create(),3system = require('system'),4sniffed,5address;67page.onInitialized = function () {8page.evaluate(function () {910(function () {11var userAgent = window.navigator.userAgent,12platform = window.navigator.platform;1314window.navigator = {15appCodeName: 'Mozilla',16appName: 'Netscape',17cookieEnabled: false,18sniffed: false19};2021window.navigator.__defineGetter__('userAgent', function () {22window.navigator.sniffed = true;23return userAgent;24});2526window.navigator.__defineGetter__('platform', function () {27window.navigator.sniffed = true;28return platform;29});30})();31});32};3334if (system.args.length === 1) {35console.log('Usage: detectsniff.js <some URL>');36phantom.exit(1);37} else {38address = system.args[1];39console.log('Checking ' + address + '...');40page.open(address, function (status) {41if (status !== 'success') {42console.log('FAIL to load the address');43phantom.exit();44} else {45window.setTimeout(function () {46sniffed = page.evaluate(function () {47return navigator.sniffed;48});49if (sniffed) {50console.log('The page tried to sniff the user agent.');51} else {52console.log('The page did not try to sniff the user agent.');53}54phantom.exit();55}, 1500);56}57});58}596061