Skip to content

Commit

Permalink
Fix #643 - Improve iOS detection + detect Slack (&Slackbot)
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalman committed Apr 14, 2023
1 parent c3be732 commit 29fb856
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
20 changes: 15 additions & 5 deletions src/ua-parser-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ const VERSION = 'version';
const MOBILE = 'mobile';
const TABLET = 'tablet';

const Apps = Object.freeze({
browser : [
[/chatlyio\/([\d\.]+)/i], [VERSION, 'Slack', [TYPE, 'app']]
]
});

const Bots = Object.freeze({
browser : [
// Googlebot / BingBot / MSNBot / FacebookBot
[/((?:google|bing|msn|facebook)bot(?:\-[imagevdo]{5})?|bingpreview)\/([\w\.]+)/i], [NAME, VERSION, [TYPE, 'bot']]
// Googlebot / BingBot / MSNBot / FacebookBot
[/((?:google|bing|msn|facebook)bot(?:\-[imagevdo]{5})?|bingpreview)\/([\w\.]+)/i], [NAME, VERSION, [TYPE, 'bot']],

// Slackbot - https://api.slack.com/robots
[/(slack(?:bot)?(?:-imgproxy|-linkexpanding)?) ([\w\.]+)/i], [NAME, VERSION, [TYPE, 'bot']]
]
});

Expand Down Expand Up @@ -107,16 +116,17 @@ const Emails = Object.freeze({
]
});

const Tools = Object.freeze({
const CLI = Object.freeze({
browser : [
// wget / curl / lynx
[/(wget|curl|lynx)\/([\w\.]+)/i], [NAME, VERSION, [TYPE, 'tool']]
[/(wget|curl|lynx)\/([\w\.]+)/i], [NAME, VERSION, [TYPE, 'cli']]
]
});

module.exports = {
Apps,
Bots,
ExtraDevices,
Emails,
Tools
CLI
};
2 changes: 1 addition & 1 deletion src/ua-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@

// iOS/macOS
/ip[honead]{2,4}\b(?:.*os ([\w]+) like mac|; opera)/i, // iOS
/ios;fbsv\/([\d\.]+)/i,
/(?:ios;fbsv\/|iphone.+ios[\/ ])([\d\.]+)/i,
/cfnetwork\/.+darwin/i
], [[VERSION, /_/g, '.'], [NAME, 'iOS']], [
/(mac os x) ?([\w\. ]*)/i,
Expand Down
12 changes: 6 additions & 6 deletions test/mocha-test-extension.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert');
const safeRegex = require('safe-regex');
const UAParser = require('ua-parser-js');
const { Bots, Emails, Tools } = require('ua-parser-js/extensions');
const { Bots, Emails, CLI } = require('ua-parser-js/extensions');

describe('Bots', () => {
it('Can detect bots', () => {
Expand All @@ -20,11 +20,11 @@ describe('Bots', () => {
assert.deepEqual(botParser.setUA(bingPreview).getBrowser(), {name: "BingPreview", version: "1.0b", major: "1", type: "bot"});
assert.deepEqual(botParser.setUA(opera).getBrowser(), {name: "Opera", version: "8.5", major: "8"});

// try merging Bots & Tools
const botsAndTools = { browser : [...Bots.browser, ...Tools.browser]};
const botolParser = new UAParser(botsAndTools);
assert.deepEqual(botolParser.setUA(wget).getBrowser(), {name: "Wget", version: "1.21.1", major: "1", type:"tool"});
assert.deepEqual(botolParser.setUA(facebookBot).getBrowser(), {name: "FacebookBot", version: "1.0", major: "1", type:"bot"});
// try merging Bots & CLIs
const botsAndCLIs = { browser : [...Bots.browser, ...CLI.browser]};
const botsAndCLIsParser = new UAParser(botsAndCLIs);
assert.deepEqual(botsAndCLIsParser.setUA(wget).getBrowser(), {name: "Wget", version: "1.21.1", major: "1", type:"cli"});
assert.deepEqual(botsAndCLIsParser.setUA(facebookBot).getBrowser(), {name: "FacebookBot", version: "1.0", major: "1", type:"bot"});

const emailParser = new UAParser(Emails);
assert.deepEqual(emailParser.setUA(outlook).getBrowser(), {name: "Microsoft Outlook", version: "16.0.9126", major: "16", type: "email"});
Expand Down
9 changes: 9 additions & 0 deletions test/specs/os-all.json
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,15 @@
"version" : "13.6.1"
}
},
{
"desc": "iOS with Slack App",
"ua": "com.tinyspeck.chatlyio/23.04.10 (iPhone; iOS 16.4.1; Scale/3.00)",
"expect":
{
"name" : "iOS",
"version" : "16.4.1"
}
},
{
"desc" : "watchOS",
"ua" : "server-bag [Watch OS,8.4,19S546,Watch3,4]",
Expand Down

0 comments on commit 29fb856

Please sign in to comment.