You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BrowserDetection.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import bowser from 'bowser';
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import {
  4. CHROME,
  5. OPERA,
  6. FIREFOX,
  7. INTERNET_EXPLORER,
  8. EDGE,
  9. SAFARI,
  10. NWJS,
  11. ELECTRON,
  12. REACT_NATIVE
  13. } from './browsers';
  14. const logger = getLogger(__filename);
  15. /**
  16. * Maps the names of the browsers from bowser to the internal names defined in
  17. * ./browsers.js
  18. */
  19. const bowserNameToJitsiName = {
  20. 'Chrome': CHROME,
  21. 'Opera': OPERA,
  22. 'Firefox': FIREFOX,
  23. 'Internet Explorer': INTERNET_EXPLORER,
  24. 'Microsoft Edge': EDGE,
  25. 'Safari': SAFARI
  26. };
  27. /**
  28. * Detects Electron environment.
  29. *
  30. * @returns {Object} - The name (ELECTRON) and version.
  31. */
  32. function _detectElectron() {
  33. const userAgent = navigator.userAgent;
  34. if (userAgent.match(/Electron/)) {
  35. const version = userAgent.match(/Electron\/([\d.]+)/)[1];
  36. logger.info(`This appears to be Electron, ver: ${version}`);
  37. return {
  38. name: ELECTRON,
  39. version
  40. };
  41. }
  42. }
  43. /**
  44. * Detects NWJS environment.
  45. *
  46. * @returns {Object} - The name (NWJS) and version.
  47. */
  48. function _detectNWJS() {
  49. const userAgent = navigator.userAgent;
  50. if (userAgent.match(/JitsiMeetNW/)) {
  51. const version = userAgent.match(/JitsiMeetNW\/([\d.]+)/)[1];
  52. logger.info(`This appears to be JitsiMeetNW, ver: ${version}`);
  53. return {
  54. name: NWJS,
  55. version
  56. };
  57. }
  58. }
  59. /**
  60. * Detects React Native environment.
  61. * @returns {Object} - The name (REACT_NATIVE) and version
  62. */
  63. function _detectReactNative() {
  64. const match
  65. = navigator.userAgent.match(/\b(react[ \t_-]*native)(?:\/(\S+))?/i);
  66. let version;
  67. // If we're remote debugging a React Native app, it may be treated as
  68. // Chrome. Check navigator.product as well and always return some version
  69. // even if we can't get the real one.
  70. if (match || navigator.product === 'ReactNative') {
  71. let name;
  72. if (match && match.length > 2) {
  73. name = match[1];
  74. version = match[2];
  75. }
  76. name || (name = 'react-native');
  77. version || (version = 'unknown');
  78. logger.info(`This appears to be ${name}, ver: ${version}`);
  79. return {
  80. name: REACT_NATIVE,
  81. version
  82. };
  83. }
  84. }
  85. /**
  86. * Returns information about the current browser.
  87. *
  88. * @returns {Object} - The name and version of the browser.
  89. */
  90. function _detect() {
  91. let browserInfo;
  92. const detectors = [
  93. _detectReactNative,
  94. _detectElectron,
  95. _detectNWJS
  96. ];
  97. // Try all browser detectors
  98. for (let i = 0; i < detectors.length; i++) {
  99. browserInfo = detectors[i]();
  100. if (browserInfo) {
  101. return browserInfo;
  102. }
  103. }
  104. const { name, version } = bowser;
  105. if (name in bowserNameToJitsiName) {
  106. return {
  107. name: bowserNameToJitsiName[name],
  108. version
  109. };
  110. }
  111. logger.warn('Browser type defaults to Safari ver 1');
  112. return {
  113. name: SAFARI,
  114. version: '1'
  115. };
  116. }
  117. /**
  118. * Implements browser detection.
  119. */
  120. export default class BrowserDetection {
  121. /**
  122. * Creates new BrowserDetection instance.
  123. *
  124. * @param {Object} [browserInfo] - Information about the browser.
  125. * @param {string} browserInfo.name - The name of the browser.
  126. * @param {string} browserInfo.version - The version of the browser.
  127. */
  128. constructor(browserInfo = _detect()) {
  129. const { name, version } = browserInfo;
  130. this._name = name;
  131. this._version = version;
  132. }
  133. /**
  134. * Gets current browser name.
  135. * @returns {string}
  136. */
  137. getName() {
  138. return this._name;
  139. }
  140. /**
  141. * Checks if current browser is Chrome.
  142. * @returns {boolean}
  143. */
  144. isChrome() {
  145. return this._name === CHROME;
  146. }
  147. /**
  148. * Checks if current browser is Opera.
  149. * @returns {boolean}
  150. */
  151. isOpera() {
  152. return this._name === OPERA;
  153. }
  154. /**
  155. * Checks if current browser is Firefox.
  156. * @returns {boolean}
  157. */
  158. isFirefox() {
  159. return this._name === FIREFOX;
  160. }
  161. /**
  162. * Checks if current browser is Internet Explorer.
  163. * @returns {boolean}
  164. */
  165. isIExplorer() {
  166. return this._name === INTERNET_EXPLORER;
  167. }
  168. /**
  169. * Checks if current browser is Microsoft Edge.
  170. * @returns {boolean}
  171. */
  172. isEdge() {
  173. return this._name === EDGE;
  174. }
  175. /**
  176. * Checks if current browser is Safari.
  177. * @returns {boolean}
  178. */
  179. isSafari() {
  180. return this._name === SAFARI;
  181. }
  182. /**
  183. * Checks if current environment is NWJS.
  184. * @returns {boolean}
  185. */
  186. isNWJS() {
  187. return this._name === NWJS;
  188. }
  189. /**
  190. * Checks if current environment is Electron.
  191. * @returns {boolean}
  192. */
  193. isElectron() {
  194. return this._name === ELECTRON;
  195. }
  196. /**
  197. * Checks if current environment is React Native.
  198. * @returns {boolean}
  199. */
  200. isReactNative() {
  201. return this._name === REACT_NATIVE;
  202. }
  203. /**
  204. * Returns the version of the current browser.
  205. * @returns {string}
  206. */
  207. getVersion() {
  208. return this._version;
  209. }
  210. /**
  211. * Compares the passed version with the current browser version.
  212. * {@see https://github.com/lancedikson/bowser}
  213. */
  214. static compareVersions = bowser.compareVersions;
  215. /**
  216. * Compares the passed version with the current browser version.
  217. *
  218. * @param {string} version - The version to compare with.
  219. * @returns {number|undefined} - Returns 0 if the version is equal to the
  220. * current one, 1 if the version is greater than the current one, -1 if the
  221. * version is lower than the current one and undefined if the current
  222. * browser version is unknown.
  223. */
  224. compareVersion(version) {
  225. if (this._version) {
  226. return bowser.compareVersions([ version, this._version ]);
  227. }
  228. }
  229. /**
  230. * Compares the passed version with the current browser version.
  231. *
  232. * @param {string} version - The version to compare with.
  233. * @returns {boolean|undefined} - Returns true if the current version is
  234. * greater than the passed version and false otherwise.
  235. */
  236. isVersionGreaterThan(version) {
  237. if (this._version) {
  238. return this.compareVersion(version) === 1;
  239. }
  240. }
  241. /**
  242. * Compares the passed version with the current browser version.
  243. *
  244. * @param {string} version - The version to compare with.
  245. * @returns {boolean|undefined} - Returns true if the current version is
  246. * lower than the passed version and false otherwise.
  247. */
  248. isVersionLessThan(version) {
  249. if (this._version) {
  250. return this.compareVersion(version) === -1;
  251. }
  252. }
  253. /**
  254. * Compares the passed version with the current browser version.
  255. *
  256. * @param {string} version - The version to compare with.
  257. * @returns {boolean|undefined} - Returns true if the current version is
  258. * equal to the passed version and false otherwise.
  259. */
  260. isVersionEqualTo(version) {
  261. if (this._version) {
  262. return this.compareVersion(version) === 0;
  263. }
  264. }
  265. }