Skip to content

Commit

Permalink
Add new helper method: isBot() to detect whether current browser is…
Browse files Browse the repository at this point in the history
… a bot
  • Loading branch information
faisalman committed Nov 2, 2024
1 parent 7a754ef commit 5a1d031
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/helpers/ua-parser-helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IResult } from "../main/ua-parser";

declare function getDeviceVendor(model: string): string | undefined;
declare function isAppleSilicon(res: IResult): boolean;
declare function isBot(res: IResult): boolean;
declare function isChromeFamily(res: IResult): boolean;
declare function isElectron(): boolean;
declare function isFromEU(): boolean;
Expand All @@ -15,6 +16,7 @@ declare function isStandalonePWA(): boolean;
export {
getDeviceVendor,
isAppleSilicon,
isBot,
isChromeFamily,
isElectron,
isFromEU,
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/ua-parser-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const isAppleSilicon = (res) => {
return false;
}

const isBot = (res) => ['cli', 'crawler', 'fetcher', 'module'].includes(res.browser.type);

const isChromeFamily = (res) => res.engine.is(Engine.BLINK);

const isElectron = () => !!(process?.versions?.hasOwnProperty('electron') || // node.js
Expand All @@ -53,6 +55,7 @@ const isStandalonePWA = () => window && (window.matchMedia('(display-mode: stand
module.exports = {
getDeviceVendor,
isAppleSilicon,
isBot,
isChromeFamily,
isElectron,
isFromEU,
Expand Down
20 changes: 19 additions & 1 deletion test/mocha-test-helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert');
const { UAParser } = require('../src/main/ua-parser');
const { getDeviceVendor, isAppleSilicon, isChromeFamily } = require('../src/helpers/ua-parser-helpers');
const { getDeviceVendor, isAppleSilicon, isBot, isChromeFamily } = require('../src/helpers/ua-parser-helpers');
const { Bots, Emails } = require('../src/extensions/ua-parser-extensions');

describe('getDeviceVendor', () => {
it('Can guess the device vendor from a model name', () => {
Expand Down Expand Up @@ -31,6 +32,23 @@ describe('isAppleSilicon', () => {
});
});

describe('isBot', () => {
it('Can detect Bots', () => {

// non-real ua
const ahrefsBot = 'Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)';
const firefox = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0';
const scrapy = 'Scrapy/1.5.0 (+https://scrapy.org)';
const thunderbird = 'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0';

const botParser = new UAParser(firefox, { Bots, Emails });
assert.equal(isBot(botParser.getResult()), false);
assert.equal(isBot(botParser.setUA(ahrefsBot).getResult()), true);
assert.equal(isBot(botParser.setUA(scrapy).getResult()), true);
assert.equal(isBot(botParser.setUA(thunderbird).getResult()), false);
});
});

describe('isChromeFamily', () => {
it('Can detect Chromium-based browser', () => {

Expand Down

0 comments on commit 5a1d031

Please sign in to comment.