Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🤖/👨‍🦰 Detect bots/crawlers/spiders using the user agent string

License

NotificationsYou must be signed in to change notification settings

omrilotan/isbot

Identify bots, crawlers, and spiders using the user agent string.

Usage

Install

npm i isbot

Straightforward usage

import{isbot}from"isbot";// Requestisbot(request.headers.get("User-Agent"));// Nodejs HTTPisbot(request.getHeader("User-Agent"));// ExpressJSisbot(req.get("user-agent"));// Browserisbot(navigator.userAgent);// User Agent stringisbot("Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",);// trueisbot("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",);// false

Use JSDeliver CDN you can import to the browser directly

See specific versions and instructionshttps://www.jsdelivr.com/package/npm/isbot

ESM

<scripttype="module">import{isbot}from"https://cdn.jsdelivr.net/npm/isbot@5/+esm";isbot(navigator.userAgent);</script>

UMD

<scriptsrc="https://cdn.jsdelivr.net/npm/isbot@5"></script><script>// isbot is now globalisbot(navigator.userAgent);</script>

All named imports

importTypeDescription
isbot(string?): booleanCheck if the user agent is a bot
isbotNaive(string?): booleanCheck if the user agent is a bot using a naive pattern (less accurate)
getPattern():RegExpThe regular expression used to identify bots
liststring[]List of all individual pattern parts
isbotMatch(string?): string | nullThe substring matched by the regular expression
isbotMatches(string?): string[]All substrings matched by the regular expression
isbotPattern(string?): string | nullThe regular expression used to identify bot substring in the user agent
isbotPatterns(string?): string[]All regular expressions used to identify bot substrings in the user agent
createIsbot(RegExp): (string?): booleanCreate a custom isbot function
createIsbotFromList(string[]): (string?): booleanCreate a custom isbot function from a list of string representation patterns

Example usages of helper functions

Create a customisbot that does not consider Chrome Lighthouse user agent as bots.

import{createIsbotFromList,isbotMatches,list}from"isbot";constChromeLighthouseUserAgentStrings:string[]=["mozilla/5.0 (macintosh; intel mac os x 10_15_7) applewebkit/537.36 (khtml, like gecko) chrome/94.0.4590.2 safari/537.36 chrome-lighthouse","mozilla/5.0 (linux; android 7.0; moto g (4)) applewebkit/537.36 (khtml, like gecko) chrome/94.0.4590.2 mobile safari/537.36 chrome-lighthouse",];constpatternsToRemove=newSet<string>(ChromeLighthouseUserAgentStrings.map(isbotMatches).flat(),);constisbot:(ua:string)=>boolean=createIsbotFromList(list.filter((record:string):boolean=>patternsToRemove.has(record)===false,),);

Create a custom isbot that considers another pattern as a bot, which is not included in the package originally.

import{createIsbotFromList,list}from"isbot";constisbot=createIsbotFromList(list.concat("shmulik"));

Definitions

  • Bot. Autonomous program imitating or replacing some aspect of a human behaviour, performing repetitive tasks much faster than human users could.
  • Good bot. Automated programs who visit websites in order to collect useful information. Web crawlers, site scrapers, stress testers, preview builders and other programs are welcomed on most websites because they serve purposes of mutual benefits.
  • Bad bot. Programs which are designed to perform malicious actions, ultimately hurting businesses. Testing credential databases, DDoS attacks, spam bots.

Clarifications

What does "isbot" do?

This package aims to identify "Good bots". Those who voluntarily identify themselves by setting a unique, preferably descriptive, user agent, usually by setting a dedicated request header.

What doesn't "isbot" do?

It does not try to recognise malicious bots or programs disguising themselves as real users.

Why would I want to identify good bots?

Recognising good bots such as web crawlers is useful for multiple purposes. Although it is not recommended to serve different content to web crawlers like Googlebot, you can still elect to

  • Flag pageviews to consider withbusiness analysis.
  • Prefer to serve cached content andrelieve service load.
  • Omit third party solutions' code (tags, pixels) andreduce costs.

It is not recommended towhitelist requests for any reason based on user agent header only. Instead, other methods of identification can be added such asreverse dns lookup.

Howisbot maintains accuracy

isbot is an asset when it can most accurately identify bots by the user agent string. It uses expansive and regularly updated lists of user agent strings to create a regular expression that matches bots and only bots.

And above everything else, it is maintained by a community of contributers who help keep the list up to date.

Fallback

The pattern uses lookbehind methods which are not supported in all environments. A fallback is provided for environments that do not support lookbehind. The fallback is less accurate. The test suite includes a percentage of false positives and false negatives which is deemed acceptable for the fallback: 1% false positive and 75% bot coverage.

Data sources

We use external data sources on top of our own lists to keep up to date

Crawlers user agents

Non bot user agents

Missing something? Pleaseopen an issue

Major releases breaking changes (full changelog)

Remove named export "pattern" from the interface, instead use "getPattern" method

Removeisbot function default export in favour of a named export.

import{isbot}from"isbot";

Remove testing for node 6 and 8

Change return value for isbot:true instead of matched string

No functional change


[8]ページ先頭

©2009-2025 Movatter.jp