diff --git a/README.md b/README.md index c00352b..8f8ec12 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,17 @@ console.log(BrowserInfo.version); // print detected browser's version string console.log(BrowserInfo.os); // print detected OS name ``` +### Auto Detect and Embed + +In `dist` directory there is a file with `.embed` sufix. Importing this library into your UI will cause calling +`detect()` method and assigning `BrowserInfo` to `window.navigator.browserInfo` - this way it is accessible globally. + +```js +import '@smartbear/browser-info/dist/smartbear-browser-info.embed.min' + +console.log(window.navigator.browserInfo); +``` + ## License This project is licensed under the ISC License - see the [LICENSE](LICENSE) file for details. diff --git a/dist/smartbear-browser-info.embed.min.js b/dist/smartbear-browser-info.embed.min.js new file mode 100644 index 0000000..9f8ec8c --- /dev/null +++ b/dist/smartbear-browser-info.embed.min.js @@ -0,0 +1,3 @@ +/* @smartbear/browser-info v1.1.0 | Copyright 2020 (c) SmartBear Software and contributors | https://github.com/SmartBear/browser-info/blob/master/LICENSE */ +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self)["@smartbear/browser-info"]=n()}(this,(function(){"use strict";const e=/^\d+/;class n{constructor(e,n){this.name=e,this.pattern=n}match(n){const r=n.match(this.pattern);if(null===r)return null;const t={name:this.name,version:null,release:null};return void 0!==r[1]&&(t.version=r[1].replace("_","."),t.release=parseInt(t.version.match(e)[0],10)),t}}const r=[new n("Firefox",/Firefox\/([0-9.]+)/),new n("Opera",/OPR\/([0-9.]+)/),new n("Edge",/Edge\/([0-9.]+)/),new n("Maxthon",/Maxthon\/([0-9.]+)/),new n("Chrome",/Chrome\/([0-9.]+)/),new n("Safari",/Version\/([0-9.]+) Safari\//),new n("InternetExplorer",/MSIE ([0-9.]+)/)],t=[new n("Windows",/Windows NT ([0-9.]+)/),new n("Windows Phone",/Windows Phone ([0-9.]+)/),new n("MacOS",/OS X ([0-9._]+)/),new n("iOS",/iPhone OS ([0-9_.]+)/),new n("iPadOS",/iPad.+?OS ([0-9_,]+)/),new n("ChromeOS",/CrOS [^ ]+ ([0-9.]+)/),new n("Android",/(?:Android|Adr) ([0-9.]+)/),new n("BlackBerry",/BlackBerry|BB10/),new n("webOS",/webOS\/([0-9.]+)/),new n("Linux",/Linux/)],o={os:"?",name:"?",release:"?",version:"?",detect(e=null){const n="string"==typeof e?e:window.navigator.userAgent;let o=!1;for(const e of r){let r=e.match(n);if(null!==r){this.name=r.name,this.release=r.release,this.version=r.version,o=!0;break}}for(const e of t){let r=e.match(n);if(null!==r){this.os=r.name;break}}return o},toString(){return`${this.name} ${this.versionString}`},get versionAsString(){return`${this.release}.${this.version}`},get versionAsNumber(){return parseInt(this.versionString.replace(/\./g,""))}};return window.navigator.browserInfo=o,o.detect(),o})); +//# sourceMappingURL=smartbear-browser-info.embed.min.js.map diff --git a/dist/smartbear-browser-info.embed.min.js.map b/dist/smartbear-browser-info.embed.min.js.map new file mode 100644 index 0000000..05b7dd8 --- /dev/null +++ b/dist/smartbear-browser-info.embed.min.js.map @@ -0,0 +1 @@ +{"version":3,"file":"smartbear-browser-info.embed.min.js","sources":["../src/Entity.js","../src/known-browsers.js","../src/known-platforms.js","../src/BrowserInfo.js","../src/embed.js"],"sourcesContent":["const MAJOR_VERSION = /^\\d+/;\n\nclass Entity {\n constructor (name, pattern) {\n this.name = name;\n this.pattern = pattern;\n }\n\n match (str) {\n const m = str.match(this.pattern);\n if (m === null) {\n return null;\n }\n\n const info = {\n name: this.name,\n version: null,\n release: null\n };\n\n if (typeof m[1] !== 'undefined') {\n info.version = m[1].replace('_', '.');\n info.release = parseInt(info.version.match(MAJOR_VERSION)[0], 10);\n }\n\n return info;\n }\n}\n\nexport { Entity };\n","import { Entity } from './Entity';\n\nconst browsers = [\n new Entity('Firefox', /Firefox\\/([0-9.]+)/),\n new Entity('Opera', /OPR\\/([0-9.]+)/),\n new Entity('Edge', /Edge\\/([0-9.]+)/),\n new Entity('Maxthon', /Maxthon\\/([0-9.]+)/),\n new Entity('Chrome', /Chrome\\/([0-9.]+)/),\n new Entity('Safari', /Version\\/([0-9.]+) Safari\\//),\n new Entity('InternetExplorer', /MSIE ([0-9.]+)/)\n];\n\nexport { browsers };\n","import { Entity } from './Entity';\n\nconst platforms = [\n new Entity('Windows', /Windows NT ([0-9.]+)/),\n new Entity('Windows Phone', /Windows Phone ([0-9.]+)/),\n new Entity('MacOS', /OS X ([0-9._]+)/),\n new Entity('iOS', /iPhone OS ([0-9_.]+)/),\n new Entity('iPadOS', /iPad.+?OS ([0-9_,]+)/),\n new Entity('ChromeOS', /CrOS [^ ]+ ([0-9.]+)/),\n new Entity('Android', /(?:Android|Adr) ([0-9.]+)/),\n new Entity('BlackBerry', /BlackBerry|BB10/),\n new Entity('webOS', /webOS\\/([0-9.]+)/),\n new Entity('Linux', /Linux/)\n];\n\nexport { platforms };\n","import { browsers } from './known-browsers';\nimport { platforms } from './known-platforms';\n\nconst BrowserInfo = {\n os: '?',\n name: '?',\n release: '?',\n version: '?',\n\n detect (userAgent = null) {\n const ua = typeof userAgent === 'string' ? userAgent : window.navigator.userAgent;\n\n let found = false;\n for(const browser of browsers) {\n let match = browser.match(ua);\n\n if (match !== null) {\n this.name = match.name;\n this.release = match.release;\n this.version = match.version;\n found = true;\n break;\n }\n }\n\n for(const platform of platforms) {\n let match = platform.match(ua);\n\n if (match !== null) {\n this.os = match.name;\n break;\n }\n }\n\n return found;\n },\n\n toString () {\n return `${this.name} ${this.versionString}`;\n },\n\n get versionAsString () {\n return `${this.release}.${this.version}`;\n },\n\n get versionAsNumber() {\n return parseInt(this.versionString.replace(/\\./g, ''));\n }\n};\n\nexport { BrowserInfo };\n","import BrowserInfo from './index';\nwindow.navigator.browserInfo = BrowserInfo;\nBrowserInfo.detect();\nexport default BrowserInfo;\n"],"names":["MAJOR_VERSION","Entity","constructor","name","pattern","match","str","m","this","info","version","release","replace","parseInt","browsers","platforms","BrowserInfo","os","detect","userAgent","ua","window","navigator","found","browser","platform","toString","versionString","browserInfo"],"mapp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Z,EAAMF,mBAKboB,GAGTG,iBACU,GAAElB,KAAKL,QAAQK,KAAKmB,6CAIpB,GAAEnB,KAAKG,WAAWH,KAAKE,wCAIxBG,SAASL,KAAKmB,cAAcf,QAAQ,MAAO,cC7CtDS,OAAOC,UAAUM,YAAcZ,EAC/BA,EAAYE"} \ No newline at end of file diff --git a/dist/smartbear-browser-info.js b/dist/smartbear-browser-info.js index 068221e..8c6d4b5 100644 --- a/dist/smartbear-browser-info.js +++ b/dist/smartbear-browser-info.js @@ -1,4 +1,4 @@ -/* @smartbear/browser-info v1.0.1 | Copyright 2020 (c) SmartBear Software and contributors | https://github.com/SmartBear/browser-info/blob/master/LICENSE */ +/* @smartbear/browser-info v1.1.0 | Copyright 2020 (c) SmartBear Software and contributors | https://github.com/SmartBear/browser-info/blob/master/LICENSE */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : diff --git a/dist/smartbear-browser-info.min.js b/dist/smartbear-browser-info.min.js index 1baecae..e0978ec 100644 --- a/dist/smartbear-browser-info.min.js +++ b/dist/smartbear-browser-info.min.js @@ -1,3 +1,3 @@ -/* @smartbear/browser-info v1.0.1 | Copyright 2020 (c) SmartBear Software and contributors | https://github.com/SmartBear/browser-info/blob/master/LICENSE */ +/* @smartbear/browser-info v1.1.0 | Copyright 2020 (c) SmartBear Software and contributors | https://github.com/SmartBear/browser-info/blob/master/LICENSE */ !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self)["@smartbear/browser-info"]=n()}(this,(function(){"use strict";const e=/^\d+/;class n{constructor(e,n){this.name=e,this.pattern=n}match(n){const r=n.match(this.pattern);if(null===r)return null;const t={name:this.name,version:null,release:null};return void 0!==r[1]&&(t.version=r[1].replace("_","."),t.release=parseInt(t.version.match(e)[0],10)),t}}const r=[new n("Firefox",/Firefox\/([0-9.]+)/),new n("Opera",/OPR\/([0-9.]+)/),new n("Edge",/Edge\/([0-9.]+)/),new n("Maxthon",/Maxthon\/([0-9.]+)/),new n("Chrome",/Chrome\/([0-9.]+)/),new n("Safari",/Version\/([0-9.]+) Safari\//),new n("InternetExplorer",/MSIE ([0-9.]+)/)],t=[new n("Windows",/Windows NT ([0-9.]+)/),new n("Windows Phone",/Windows Phone ([0-9.]+)/),new n("MacOS",/OS X ([0-9._]+)/),new n("iOS",/iPhone OS ([0-9_.]+)/),new n("iPadOS",/iPad.+?OS ([0-9_,]+)/),new n("ChromeOS",/CrOS [^ ]+ ([0-9.]+)/),new n("Android",/(?:Android|Adr) ([0-9.]+)/),new n("BlackBerry",/BlackBerry|BB10/),new n("webOS",/webOS\/([0-9.]+)/),new n("Linux",/Linux/)];return{os:"?",name:"?",release:"?",version:"?",detect(e=null){const n="string"==typeof e?e:window.navigator.userAgent;let i=!1;for(const e of r){let r=e.match(n);if(null!==r){this.name=r.name,this.release=r.release,this.version=r.version,i=!0;break}}for(const e of t){let r=e.match(n);if(null!==r){this.os=r.name;break}}return i},toString(){return`${this.name} ${this.versionString}`},get versionAsString(){return`${this.release}.${this.version}`},get versionAsNumber(){return parseInt(this.versionString.replace(/\./g,""))}}})); //# sourceMappingURL=smartbear-browser-info.min.js.map diff --git a/package-lock.json b/package-lock.json index d390474..5a232cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@smartbear/browser-info", - "version": "1.0.1", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index be0b516..cda3750 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@smartbear/browser-info", - "version": "1.0.1", + "version": "1.1.0", "description": "Tiny developer-friendly JS library that provides information about host browser", "main": "dist/smartbear-browser-info.min.js", "scripts": { diff --git a/rollup.config.js b/rollup.config.js index 4fc0ec5..9c409f3 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -15,9 +15,10 @@ for(const ext of external) { } const safeName = $package.name.replace(/@/g, '').replace(/\//g, '-'); -const output = function(min) { + +function getOutput(min, sufix = '') { return { - file: 'dist/' + safeName + (min ? '.min' : '') + '.js', + file: `dist/${safeName}${sufix}${(min ? '.min' : '')}.js`, format: 'umd', name: $package.name, banner: `/* ${$package.name} v${$package.version} ` + @@ -27,9 +28,9 @@ const output = function(min) { globals, sourcemap: true }; -}; +} -const getBabelPlugin = function() { +function getBabelPlugin() { return babel({ exclude: 'node_modules/**', presets: [ @@ -43,13 +44,28 @@ const getBabelPlugin = function() { ] ] }); -}; +} + +function getTerserPlugin() { + return terser({ + output: { + comments: (node, comment) => { + if (comment.type === "comment2") { + // multiline comment + return /LICENSE|\(c\)/.test(comment.value); + } + return false; + } + } + }); +} + export default [ // Uncompressed config { input, - output: output(), + output: getOutput(), plugins: [ del({ targets: 'dist/*', @@ -63,7 +79,7 @@ export default [ // Compressed config { input, - output: output(true), + output: getOutput(true), plugins: [ getBabelPlugin(), terser({ @@ -79,5 +95,16 @@ export default [ }) ], external + }, + + // Embed version + { + input: 'src/embed.js', + output: getOutput(true, '.embed'), + plugins: [ + getBabelPlugin(), + getTerserPlugin() + ], + external } ]; diff --git a/src/embed.js b/src/embed.js new file mode 100644 index 0000000..914ef7b --- /dev/null +++ b/src/embed.js @@ -0,0 +1,4 @@ +import BrowserInfo from './index'; +window.navigator.browserInfo = BrowserInfo; +BrowserInfo.detect(); +export default BrowserInfo;