is-web-crawler
Version:
A light weight JS library to check if a user agent is a web crawler.
35 lines (34 loc) • 1.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.WebCrawlerService = void 0;
var crawler_user_agents_1 = __importDefault(require("crawler-user-agents"));
var WebCrawlerService = /** @class */ (function () {
function WebCrawlerService() {
this.cache = {};
}
WebCrawlerService.prototype.isCrawlerUserAgent = function (topCrawlersToCheck) {
var cacheKey = "" + (topCrawlersToCheck || '');
if (cacheKey in this.cache) {
return this.cache[cacheKey];
}
return this.isCrawler(window.navigator.userAgent, topCrawlersToCheck);
};
WebCrawlerService.prototype.isCrawler = function (userAgent, topCrawlersToCheck) {
return Boolean(this.getCrawler(userAgent, topCrawlersToCheck));
};
WebCrawlerService.prototype.getCrawler = function (userAgent, max) {
var len = max ? Math.min(crawler_user_agents_1["default"].length, max) : crawler_user_agents_1["default"].length;
for (var i = 0; i < len; i++) {
var entry = crawler_user_agents_1["default"][i];
if (RegExp(entry.pattern).test(userAgent)) {
return entry;
}
}
return null;
};
return WebCrawlerService;
}());
exports.WebCrawlerService = WebCrawlerService;