Created
November 15, 2018 18:57
-
-
Save webislife/2c79733605eba4ebcd1f6ed6ad463e17 to your computer and use it in GitHub Desktop.
Bot browser detect
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
/** | |
* Проверка по косвенным параметрам на бота\selenium\headless browser | |
* @function botDetection | |
* @return {Boolean} isBot flag | |
*/ | |
botDetection () { | |
let isBot = false, | |
rules = [ | |
//Phantomjs как правило, не имеет внутри себя браузерных плагинов | |
() => (navigator.plugins instanceof PluginArray) === false || navigator.plugins.length == 0, | |
//PhantomJs 1.x прокидывает 2 свойства в глобальный объект, проверим их | |
() => (window.callPhantom || window._phantom), | |
//PhantomJs и многие другие боты не | |
() => (!Function.prototype.bind), | |
//Ошибки в коде, имеют stacktrace в котором можно отследить phantom, сделаем ошибку и проверим | |
() => { | |
let err; | |
try { null[0](); } catch (error) { err = error; } | |
return (err.indexOf('phantom') > -1) | |
}, | |
//Запуская код через nodejs, у window будет доступен Buffer | |
() => window.Buffer !== undefined, | |
//Запуская сайт через coachjs у window будет доступен emit | |
() => window.emit !== undefined, | |
//Запуская сайт через rhino - у window будет доступен spawn | |
() => window.spawn !== undefined, | |
//Не только селениум использует webdriver, но и другие боты | |
() => window.webdriver !== undefined, | |
//Проверяем chrome based dom automation - также инструмент ботов для работы с DOM | |
() => window.domAutomation !== undefined || window.domAutomationController !== undefined, | |
//Headless браузеры не имеют размера окна, проверим это | |
//WARNING: эту проверку не пройдут IOS 8 и ниже, а также sailfish webview | |
() => window.outerWidth === 0 && window.outerHeight === 0, | |
//Множество ботов не заботится проставлении navigator.online и ходят по сайту в оффлайн режиме | |
() => window.navigator.onLine === false || isBot === true | |
]; | |
//Проверяем правила | |
for (let i = 0; i < rules.length; i++) { | |
if(rules[i]() === true) isBot = true; | |
} | |
return isBot; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment