Detect Search Crawlers via JavaScript

前端 未结 5 1375
天命终不由人
天命终不由人 2020-12-12 23:55

I am wondering how would I go abouts in detecting search crawlers? The reason I ask is because I want to suppress certain JavaScript calls if the user agent is a bot.

<
5条回答
  •  -上瘾入骨i
    2020-12-13 00:26

    The following regex will match the biggest search engines according to this post.

    /bot|google|baidu|bing|msn|teoma|slurp|yandex/i
        .test(navigator.userAgent)
    

    The matches search engines are:

    • Baidu
    • Bingbot/MSN
    • DuckDuckGo (duckduckbot)
    • Google
    • Teoma
    • Yahoo!
    • Yandex

    Additionally, I've added bot as a catchall for smaller crawlers/bots.

提交回复
热议问题