Created
June 13, 2020 14:40
-
-
Save bes-internal/70d509ade489aa93999cdc3daca8e7a5 to your computer and use it in GitHub Desktop.
detect headless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//detect | |
// v:3 | |
function detect() { | |
try { | |
if (!(window.navigator.userAgent.search(/Chrome\//) > 0 && window.navigator.userAgent.search(/OPR|YaBrowser|Konqueror|Edge/) < 0)) {return 'notchrome'}; | |
var results = {}; | |
function test(name, fn) { | |
results[name] = fn(); | |
// console.log(results); | |
// console.log(results[name] + '\t' + name); | |
// document.body.innerHTML+=`<div>${detectionPassed} = ${name}</div>`; | |
// if (detectionPassed) {document.body.innerHTML+=`<div style=color:red>detected via ${name}</div>`;} | |
} | |
test('ua', function(){ | |
return /HeadlessChrome/.test(window.navigator.userAgent); | |
}); | |
test('dr', function(){ | |
return navigator.webdriver; | |
}); | |
test('ch', function(){ | |
return /Chrome\//.test(window.navigator.userAgent) && navigator.userAgent.search(/OPR|YaBrowser|Konqueror|Edge/) < 0 && !window.chrome; | |
}); | |
test('p', function(){ | |
const permissionStatus = navigator.permissions.query({name: 'notifications'}); | |
return Notification.permission === 'denied' && permissionStatus.state === 'prompt'; | |
}); | |
test('po', function(){ | |
if (!window.navigator.permissions) {return false;} | |
if (window.navigator.permissions.query.toString().replace(/\s/g,'') !== 'functionquery(){[nativecode]}') return true; | |
if (window.navigator.permissions.query.toString.toString().replace(/\s/g,'') !== 'functiontoString(){[nativecode]}') return true; | |
if ( | |
window.navigator.permissions.query.toString.hasOwnProperty('[[Handler]]') && | |
window.navigator.permissions.query.toString.hasOwnProperty('[[Target]]') && | |
window.navigator.permissions.query.toString.hasOwnProperty('[[IsRevoked]]') | |
) return true; | |
if (window.navigator.permissions.hasOwnProperty('query')) return true; | |
}); | |
// test('navigator.plugins empty', function(){ | |
// return navigator.plugins.length === 0; | |
// }); | |
test('la', function(){ | |
return navigator.languages === ''; | |
}); | |
// test('iFrame for fresh window object', function(){ | |
// // evaluateOnNewDocument scripts don't apply within [srcdoc] (or [sandbox]) iframes | |
// // https://github.com/GoogleChrome/puppeteer/issues/1106#issuecomment-359313898 | |
// const iframe = document.createElement('iframe'); | |
// iframe.srcdoc = 'page intentionally left blank'; | |
// document.body.appendChild(iframe); | |
// | |
// // Here we would need to rerun all tests with `iframe.contentWindow` as `window` | |
// // Example: | |
// return iframe.contentWindow.navigator.plugins.length === 0 | |
// }); | |
// // This detects that a devtools protocol agent is attached. | |
// // So it will also pass true in headful Chrome if the devtools window is attached | |
// test('toString', function(){ | |
// let gotYou = 0; | |
// const spooky = /./; | |
// spooky.toString = function() { | |
// gotYou++; | |
// return 'spooky'; | |
// } | |
// console.debug(spooky); | |
// return gotYou > 1; | |
// }); | |
for (var key in results) { | |
if (results[key]===true) {return 'true:'+key;} | |
} | |
return false; | |
} catch(e) {return 'detecterr';} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment