Language/JavaScript (WEB)

๐Ÿค– ๊ตฌ๊ธ€๋ด‡ / ํฌ๋กค๋Ÿฌ ๋ฐฉ๋ฌธ ๊ฐ์ง€ํ•˜๊ธฐ

์ธํŒŒ_ 2022. 10. 6. 00:39

๊ตฌ๊ธ€๋ด‡/ํฌ๋กค๋Ÿฌ

๊ฒ€์ƒ‰๋ด‡์ด ํฌ๋กค๋ง/์Šคํฌ๋žฉํ•‘ ํ•˜๋Š”์ง€ ๊ฐ์ง€ํ•˜๊ธฐ

์šฐ๋ฆฌ๊ฐ€ ์ž˜์•„๋Š” ๋Œ€ํ‘œ์ ์ธ ๊ฒ€์ƒ‰๋ด‡์€ ๊ตฌ๊ธ€๋ด‡์ด ์žˆ๋‹ค. ์ด ๊ตฌ๊ธ€๋ด‡์ด ์šฐ๋ฆฌ์˜ ํ‹ฐ์Šคํ† ๋ฆฌ๋‚˜ ์›น์‚ฌ์ดํŠธ๋ฅผ ํฌ๋กค๋งํ•ด์•ผ ๊ตฌ๊ธ€ ๋…ธ์ถœ์„ ์‹œ์ผœ์ค€๋‹ค. ๋‹ค๋งŒ, ๊ตฌ๊ธ€๋ด‡๋„ ํ•˜๋‚˜์˜ ์‚ฌ์šฉ์ž์ฒ˜๋Ÿผ ์›น์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•ด ์กฐํšŒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด ํ™ˆํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•œ๊ฒŒ ๋กœ๋ด‡์ธ์ง€ ์‚ฌ๋žŒ์ธ์ง€ ๊ตฌ๋ถ„ํ•  ํ•„์š”์„ฑ์ด ์ƒ๊ธธ ๋•Œ ๊ฐ€ ์žˆ๋‹ค.

์ด๋•Œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์›น๋ธŒ๋ผ์šฐ์ € ์ „๋ฐ˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” window ๊ฐ์ฒด์˜ navigator.userAgent ์— ์ ํžŒ ์ •๋ณด๋ฅผ ๋’ค์ ธ์„œ ์ก๊ทœ์‹์œผ๋กœ ๋ด‡์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•˜๋Š” ์‹์œผ๋กœ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

const detectRobot = (userAgent) => {
  const robots = new RegExp([
    /bot/,/spider/,/crawl/,                            // GENERAL TERMS
    /APIs-Google/,/AdsBot/,/Googlebot/,                // GOOGLE ROBOTS
    /mediapartners/,/Google Favicon/,
    /FeedFetcher/,/Google-Read-Aloud/,
    /DuplexWeb-Google/,/googleweblight/,
    /bing/,/yandex/,/baidu/,/duckduck/,/yahoo/,        // OTHER ENGINES
    /ecosia/,/ia_archiver/,
    /facebook/,/instagram/,/pinterest/,/reddit/,       // SOCIAL MEDIA
    /slack/,/twitter/,/whatsapp/,/youtube/,
    /semrush/,                                         // OTHER
  ].map((r) => r.source).join("|"),"i");               // BUILD REGEXP + "i" FLAG
  return robots.test(userAgent);
};

// ์ ‘์†์ž๊ฐ€ ์‚ฌ์šฉ์ž์ธ์ง€ ๋กœ๋ด‡์ธ์ง€ ์ฒดํฌ
const isrobot = detectRobot(navigator.userAgent) // true/false

isbot ๋ชจ๋“ˆ

npm ์ง„์˜์—๋Š” isbot ์ด๋ผ๋Š” ๋…ธ๋“œํŒจํ‚ค์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์„œ๋ฒ„์—์„œ ๋งŒ์ผ ๊ฒ€์ƒ‰ ๋ด‡์„ ๊ฐ์ง€ํ•˜๊ณ  ์‹ถ์„๋•Œ๋Š” ์ด ๋…ธ๋“œ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

isbot

๐Ÿค– detect bots/crawlers/spiders via the user agent.. Latest version: 3.4.3, last published: 17 days ago. Start using isbot in your project by running `npm i isbot`. There are 50 other projects in the npm registry using isbot.

www.npmjs.com

import isbot from 'isbot'

// Nodejs HTTP
isbot(request.getHeader('User-Agent'))

// ExpressJS
isbot(req.get('user-agent'))

// Browser
isbot(navigator.userAgent)