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 11KB

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