Skip to content

Instantly share code, notes, and snippets.

@ariya
Created October 15, 2011 00:18
Show Gist options
  • Save ariya/1288757 to your computer and use it in GitHub Desktop.
Save ariya/1288757 to your computer and use it in GitHub Desktop.
PhantomJS script to check if a web page sniffs the user agent or not.
// Check if a web page sniffs the user agent or not.
var page = require('webpage').create(),
sniffing, address;
page.onInitialized = function () {
page.evaluate(function () {
window.originalNavigator = window.navigator;
navigator = JSON.parse(JSON.stringify(window.originalNavigator));
navigator.sniffing = false;
navigator.__defineGetter__('userAgent', function() {
navigator.sniffing = true;
return originalNavigator.userAgent;
});
});
};
if (phantom.args.length === 0) {
console.log('Usage: unsniff.js <some URL>');
phantom.exit();
} else {
address = phantom.args[0];
console.log('Checking ' + address + '...');
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
window.setTimeout(function () {
sniffing = page.evaluate(function () {
return navigator.sniffing;
});
if (sniffing) {
console.log('The page tried to sniff the user agent.');
} else {
console.log('The page did not try to sniff the user agent.');
}
phantom.exit();
}, 1500);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment