Code Monkey home page Code Monkey logo

isbot's Introduction

isbot

Detect bots/crawlers/spiders via the user agent.

install

$ npm i isbot

Usage

Simple detection

isBot(req.headers['user-agent'])

isBot('Googlebot/2.1 (+http://www.google.com/bot.html)') // true

isBot('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36') // false

Extending more user agents

Add rules to user agent match RegExp

isBot('Mozilla/5.0') // false

var myList = [
    'istat',
    'newspaper',
    'httpclient',
    '^mozilla/\\d\\.\\d$',
];

isBot.extend(myList);

isBot('Mozilla/5.0') // true

Excluding known crawlers

Remove rules to user agent match RegExp (see existing rules in list.json file)

isBot('Ceramic Tile Installation Guide') // true

var myList = [
	'Ceramic Tile Installation Guide',
	'NORAD National Defence Network'
];

isBot.exclude(myList);

isBot('Ceramic Tile Installation Guide') // false

extend and exclude use case

Use lookbehind assertion, introduced in V8 version 4.9 to exclude "Cubot" from "bot" rule

isBot.exclude(['bot']);
isBot.extend(['(?<! cu)bot']); // Recognise cubot browser as legit browser

isBot('Mozilla/5.0 (Linux; Android 8.0.0; CUBOT_P20) ...') // false
isBot('Googlebot/2.1 (+http://www.google.com/bot.html)') // true

Verbose result

Return the respective match for bot user agent rule

isBot.find('Googlebot/2.1 (+http://www.google.com/bot.html)') // 'bot'

isbot's People

Contributors

atomantic avatar chinesedfan avatar franzmoro avatar gorangajic avatar hijarian avatar interisti avatar jssullivan avatar maximn avatar niftylettuce avatar omgimalexis avatar omrilotan avatar orangeflame avatar oskarrisberg avatar rynmsh avatar timbowhite avatar typehorror avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.