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.

browser.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import { DOMParser } from '@xmldom/xmldom';
  2. import { atob, btoa } from 'abab';
  3. import { NativeModules, Platform } from 'react-native';
  4. import BackgroundTimer from 'react-native-background-timer';
  5. import { TextDecoder, TextEncoder } from 'text-encoding';
  6. import 'promise.allsettled/auto'; // Promise.allSettled.
  7. import 'react-native-url-polyfill/auto'; // Complete URL polyfill.
  8. import Storage from './Storage';
  9. const { AppInfo } = NativeModules;
  10. /**
  11. * Implements an absolute minimum of the common logic of
  12. * {@code Document.querySelector} and {@code Element.querySelector}. Implements
  13. * the most simple of selectors necessary to satisfy the call sites at the time
  14. * of this writing (i.e. Select by tagName).
  15. *
  16. * @param {Node} node - The Node which is the root of the tree to query.
  17. * @param {string} selectors - The group of CSS selectors to match on.
  18. * @returns {Element} - The first Element which is a descendant of the specified
  19. * node and matches the specified group of selectors.
  20. */
  21. function _querySelector(node, selectors) {
  22. let element = null;
  23. node && _visitNode(node, n => {
  24. if (n.nodeType === 1 /* ELEMENT_NODE */
  25. && n.nodeName === selectors) {
  26. element = n;
  27. return true;
  28. }
  29. return false;
  30. });
  31. return element;
  32. }
  33. /**
  34. * Visits each Node in the tree of a specific root Node (using depth-first
  35. * traversal) and invokes a specific callback until the callback returns true.
  36. *
  37. * @param {Node} node - The root Node which represents the tree of Nodes to
  38. * visit.
  39. * @param {Function} callback - The callback to invoke with each visited Node.
  40. * @returns {boolean} - True if the specified callback returned true for a Node
  41. * (at which point the visiting stopped); otherwise, false.
  42. */
  43. function _visitNode(node, callback) {
  44. if (callback(node)) {
  45. return true;
  46. }
  47. /* eslint-disable no-param-reassign, no-extra-parens */
  48. if ((node = node.firstChild)) {
  49. do {
  50. if (_visitNode(node, callback)) {
  51. return true;
  52. }
  53. } while ((node = node.nextSibling));
  54. }
  55. /* eslint-enable no-param-reassign, no-extra-parens */
  56. return false;
  57. }
  58. (global => {
  59. // DOMParser
  60. //
  61. // Required by:
  62. // - lib-jitsi-meet requires this if using WebSockets
  63. global.DOMParser = DOMParser;
  64. // addEventListener
  65. //
  66. // Required by:
  67. // - jQuery
  68. if (typeof global.addEventListener === 'undefined') {
  69. // eslint-disable-next-line no-empty-function
  70. global.addEventListener = () => {};
  71. }
  72. // removeEventListener
  73. //
  74. // Required by:
  75. // - features/base/conference/middleware
  76. if (typeof global.removeEventListener === 'undefined') {
  77. // eslint-disable-next-line no-empty-function
  78. global.removeEventListener = () => {};
  79. }
  80. // document
  81. //
  82. // Required by:
  83. // - jQuery
  84. // - Strophe
  85. if (typeof global.document === 'undefined') {
  86. const document
  87. = new DOMParser().parseFromString(
  88. '<html><head></head><body></body></html>',
  89. 'text/xml');
  90. // document.addEventListener
  91. //
  92. // Required by:
  93. // - jQuery
  94. if (typeof document.addEventListener === 'undefined') {
  95. // eslint-disable-next-line no-empty-function
  96. document.addEventListener = () => {};
  97. }
  98. // document.cookie
  99. //
  100. // Required by:
  101. // - herment
  102. if (typeof document.cookie === 'undefined') {
  103. document.cookie = '';
  104. }
  105. // document.implementation.createHTMLDocument
  106. //
  107. // Required by:
  108. // - jQuery
  109. if (typeof document.implementation.createHTMLDocument === 'undefined') {
  110. document.implementation.createHTMLDocument = function(title = '') {
  111. const htmlDocument
  112. = new DOMParser().parseFromString(
  113. `<html>
  114. <head><title>${title}</title></head>
  115. <body></body>
  116. </html>`,
  117. 'text/xml');
  118. Object.defineProperty(htmlDocument, 'body', {
  119. get() {
  120. return htmlDocument.getElementsByTagName('body')[0];
  121. }
  122. });
  123. return htmlDocument;
  124. };
  125. }
  126. // Element.querySelector
  127. //
  128. // Required by:
  129. // - lib-jitsi-meet/modules/xmpp
  130. const elementPrototype
  131. = Object.getPrototypeOf(document.documentElement);
  132. if (elementPrototype) {
  133. if (typeof elementPrototype.querySelector === 'undefined') {
  134. elementPrototype.querySelector = function(selectors) {
  135. return _querySelector(this, selectors);
  136. };
  137. }
  138. // Element.remove
  139. //
  140. // Required by:
  141. // - lib-jitsi-meet ChatRoom#onPresence parsing
  142. if (typeof elementPrototype.remove === 'undefined') {
  143. elementPrototype.remove = function() {
  144. if (this.parentNode !== null) {
  145. this.parentNode.removeChild(this);
  146. }
  147. };
  148. }
  149. // Element.innerHTML
  150. //
  151. // Required by:
  152. // - jQuery's .append method
  153. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  154. Object.defineProperty(elementPrototype, 'innerHTML', {
  155. get() {
  156. return this.childNodes.toString();
  157. },
  158. set(innerHTML) {
  159. // MDN says: removes all of element's children, parses
  160. // the content string and assigns the resulting nodes as
  161. // children of the element.
  162. // Remove all of element's children.
  163. this.textContent = '';
  164. // Parse the content string.
  165. const d
  166. = new DOMParser().parseFromString(
  167. `<div>${innerHTML}</div>`,
  168. 'text/xml');
  169. // Assign the resulting nodes as children of the
  170. // element.
  171. const documentElement = d.documentElement;
  172. let child;
  173. // eslint-disable-next-line no-cond-assign
  174. while (child = documentElement.firstChild) {
  175. this.appendChild(child);
  176. }
  177. }
  178. });
  179. }
  180. // Element.children
  181. //
  182. // Required by:
  183. // - lib-jitsi-meet ChatRoom#onPresence parsing
  184. if (!elementPrototype.hasOwnProperty('children')) {
  185. Object.defineProperty(elementPrototype, 'children', {
  186. get() {
  187. const nodes = this.childNodes;
  188. const children = [];
  189. let i = 0;
  190. let node = nodes[i];
  191. while (node) {
  192. if (node.nodeType === 1) {
  193. children.push(node);
  194. }
  195. i += 1;
  196. node = nodes[i];
  197. }
  198. return children;
  199. }
  200. });
  201. }
  202. }
  203. global.document = document;
  204. }
  205. // location
  206. if (typeof global.location === 'undefined') {
  207. global.location = {
  208. href: '',
  209. // Required by:
  210. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  211. search: ''
  212. };
  213. }
  214. const { navigator } = global;
  215. if (navigator) {
  216. // userAgent
  217. //
  218. // Required by:
  219. // - lib-jitsi-meet/modules/browser/BrowserDetection.js
  220. // React Native version
  221. const { reactNativeVersion } = Platform.constants;
  222. const rnVersion
  223. = `react-native/${reactNativeVersion.major}.${reactNativeVersion.minor}.${reactNativeVersion.patch}`;
  224. // (OS version)
  225. const os = `(${Platform.OS}/${Platform.Version})`;
  226. // SDK
  227. const liteTxt = AppInfo.isLiteSDK ? '-lite' : '';
  228. const sdkVersion = `JitsiMeetSDK/${AppInfo.sdkVersion}${liteTxt}`;
  229. const parts = [
  230. navigator.userAgent ?? '',
  231. sdkVersion,
  232. os,
  233. rnVersion
  234. ];
  235. navigator.userAgent = parts.filter(Boolean).join(' ');
  236. }
  237. // WebRTC
  238. require('./webrtc');
  239. // Performance API
  240. // RN only provides the now() method, since the polyfill refers the global
  241. // performance object itself we extract it here to avoid infinite recursion.
  242. const performanceNow = global.performance.now;
  243. const perf = require('react-native-performance');
  244. global.performance = perf.default;
  245. global.performance.now = performanceNow;
  246. global.PerformanceObserver = perf.PerformanceObserver;
  247. // Timers
  248. //
  249. // React Native's timers won't run while the app is in the background, this
  250. // is a known limitation. Replace them with a background-friendly alternative.
  251. if (Platform.OS === 'android') {
  252. global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  253. global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  254. global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
  255. global.setTimeout = (fn, ms = 0) => BackgroundTimer.setTimeout(fn, ms);
  256. }
  257. // localStorage
  258. if (typeof global.localStorage === 'undefined') {
  259. global.localStorage = new Storage('@jitsi-meet/');
  260. }
  261. // sessionStorage
  262. //
  263. // Required by:
  264. // - herment
  265. // - Strophe
  266. if (typeof global.sessionStorage === 'undefined') {
  267. global.sessionStorage = new Storage();
  268. }
  269. global.TextDecoder = TextDecoder;
  270. global.TextEncoder = TextEncoder;
  271. // atob
  272. //
  273. // Required by:
  274. // - Strophe
  275. if (typeof global.atob === 'undefined') {
  276. global.atob = atob;
  277. }
  278. // btoa
  279. //
  280. // Required by:
  281. // - Strophe
  282. if (typeof global.btoa === 'undefined') {
  283. global.btoa = btoa;
  284. }
  285. })(global || window || this); // eslint-disable-line no-invalid-this