通过JavaScript检测搜索爬虫

我想知道我该怎么做才能发现搜索爬虫?我问的原因是,如果用户代理是bot,我想禁止某些javascript调用。
我找到了一个如何检测特定浏览器的示例,但无法找到如何检测搜索爬虫的示例:
/MSIE (\d+\.\d+);/.test(navigator.userAgent); //test for MSIE x.x
我要阻止的搜索爬虫示例:

Google 
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) 
Googlebot/2.1 (+http://www.googlebot.com/bot.html) 
Googlebot/2.1 (+http://www.google.com/bot.html) 

Baidu 
Baiduspider+(+http://www.baidu.com/search/spider_jp.html) 
Baiduspider+(+http://www.baidu.com/search/spider.htm) 
BaiDuSpider 


最佳答案:

这是RubyUAagent_orange库用来测试是否存在bot的regex。您可以通过参考bot userAgent list here缩小特定机器人程序的范围:

/bot|crawler|spider|crawling/i

例如,您有一些对象,您可以存储用户所使用的设备类型:
util.browser = {
   bot: /bot|googlebot|crawler|spider|robot|crawling/i.test(navigator.userAgent),
   mobile: ...,
   desktop: ...
}