Skip to content

Instantly share code, notes, and snippets.

@germanattanasio
Created November 17, 2023 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germanattanasio/25c7ae684117072331e44371a2bedef8 to your computer and use it in GitHub Desktop.
Save germanattanasio/25c7ae684117072331e44371a2bedef8 to your computer and use it in GitHub Desktop.
Check if user agent is a bot
<script>
const checkIfUserAgentIsBot = (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/,
/checkly/, /headless/,
/semrush/, // OTHER
]).map((r) => r.source).join("|"),"i"); // BUILD REGEXP + "i" FLAG
return robots.test(userAgent);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment