Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BrowserDetection.js 7.1KB

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