From 29fb85658adb66e4b842ffe721b38e77f4ec5cd1 Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Sat, 15 Apr 2023 01:08:46 +0700 Subject: [PATCH] Fix #643 - Improve iOS detection + detect Slack (&Slackbot) --- src/ua-parser-extension.js | 20 +++++++++++++++----- src/ua-parser.js | 2 +- test/mocha-test-extension.js | 12 ++++++------ test/specs/os-all.json | 9 +++++++++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/ua-parser-extension.js b/src/ua-parser-extension.js index fed987eb3..329129ebd 100644 --- a/src/ua-parser-extension.js +++ b/src/ua-parser-extension.js @@ -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']] ] }); @@ -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 }; \ No newline at end of file diff --git a/src/ua-parser.js b/src/ua-parser.js index 5f306252a..13d5507c8 100755 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -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, diff --git a/test/mocha-test-extension.js b/test/mocha-test-extension.js index 250ee6442..c97c16d26 100644 --- a/test/mocha-test-extension.js +++ b/test/mocha-test-extension.js @@ -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', () => { @@ -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"}); diff --git a/test/specs/os-all.json b/test/specs/os-all.json index 3700e5339..5bf7630b0 100644 --- a/test/specs/os-all.json +++ b/test/specs/os-all.json @@ -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]",