Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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