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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import { Platform } from 'react-native';
  2. import BackgroundTimer from 'react-native-background-timer';
  3. import 'react-native-url-polyfill/auto'; // Complete URL polyfill.
  4. import Storage from './Storage';
  5. /**
  6. * Gets the first common prototype of two specified Objects (treating the
  7. * objects themselves as prototypes as well).
  8. *
  9. * @param {Object} a - The first prototype chain to climb in search of a common
  10. * prototype.
  11. * @param {Object} b - The second prototype chain to climb in search of a common
  12. * prototype.
  13. * @returns {Object|undefined} - The first common prototype of a and b.
  14. */
  15. function _getCommonPrototype(a, b) {
  16. // Allow the arguments to be prototypes themselves.
  17. if (a === b) {
  18. return a;
  19. }
  20. let p;
  21. if (((p = Object.getPrototypeOf(a)) && (p = _getCommonPrototype(b, p)))
  22. || ((p = Object.getPrototypeOf(b))
  23. && (p = _getCommonPrototype(a, p)))) {
  24. return p;
  25. }
  26. return undefined;
  27. }
  28. /**
  29. * Implements an absolute minimum of the common logic of
  30. * {@code Document.querySelector} and {@code Element.querySelector}. Implements
  31. * the most simple of selectors necessary to satisfy the call sites at the time
  32. * of this writing (i.e. Select by tagName).
  33. *
  34. * @param {Node} node - The Node which is the root of the tree to query.
  35. * @param {string} selectors - The group of CSS selectors to match on.
  36. * @returns {Element} - The first Element which is a descendant of the specified
  37. * node and matches the specified group of selectors.
  38. */
  39. function _querySelector(node, selectors) {
  40. let element = null;
  41. node && _visitNode(node, n => {
  42. if (n.nodeType === 1 /* ELEMENT_NODE */
  43. && n.nodeName === selectors) {
  44. element = n;
  45. return true;
  46. }
  47. return false;
  48. });
  49. return element;
  50. }
  51. /**
  52. * Visits each Node in the tree of a specific root Node (using depth-first
  53. * traversal) and invokes a specific callback until the callback returns true.
  54. *
  55. * @param {Node} node - The root Node which represents the tree of Nodes to
  56. * visit.
  57. * @param {Function} callback - The callback to invoke with each visited Node.
  58. * @returns {boolean} - True if the specified callback returned true for a Node
  59. * (at which point the visiting stopped); otherwise, false.
  60. */
  61. function _visitNode(node, callback) {
  62. if (callback(node)) {
  63. return true;
  64. }
  65. /* eslint-disable no-param-reassign, no-extra-parens */
  66. if ((node = node.firstChild)) {
  67. do {
  68. if (_visitNode(node, callback)) {
  69. return true;
  70. }
  71. } while ((node = node.nextSibling));
  72. }
  73. /* eslint-enable no-param-reassign, no-extra-parens */
  74. return false;
  75. }
  76. (global => {
  77. const { DOMParser } = require('xmldom');
  78. // DOMParser
  79. //
  80. // Required by:
  81. // - lib-jitsi-meet requires this if using WebSockets
  82. global.DOMParser = DOMParser;
  83. // addEventListener
  84. //
  85. // Required by:
  86. // - jQuery
  87. if (typeof global.addEventListener === 'undefined') {
  88. // eslint-disable-next-line no-empty-function
  89. global.addEventListener = () => {};
  90. }
  91. // Promise.allSettled is supported from RN 0.63 onwards, use a polyfill for that.
  92. // Invokes its shim method to shim Promise.allSettled if it is unavailable or noncompliant.
  93. //
  94. // Required by:
  95. // lib-jitsi-meet/JitsiConference.js
  96. require('promise.allsettled').shim();
  97. // removeEventListener
  98. //
  99. // Required by:
  100. // - features/base/conference/middleware
  101. if (typeof global.removeEventListener === 'undefined') {
  102. // eslint-disable-next-line no-empty-function
  103. global.removeEventListener = () => {};
  104. }
  105. // document
  106. //
  107. // Required by:
  108. // - jQuery
  109. // - Strophe
  110. if (typeof global.document === 'undefined') {
  111. const document
  112. = new DOMParser().parseFromString(
  113. '<html><head></head><body></body></html>',
  114. 'text/xml');
  115. // document.addEventListener
  116. //
  117. // Required by:
  118. // - jQuery
  119. if (typeof document.addEventListener === 'undefined') {
  120. // eslint-disable-next-line no-empty-function
  121. document.addEventListener = () => {};
  122. }
  123. // document.cookie
  124. //
  125. // Required by:
  126. // - herment
  127. if (typeof document.cookie === 'undefined') {
  128. document.cookie = '';
  129. }
  130. // document.implementation.createHTMLDocument
  131. //
  132. // Required by:
  133. // - jQuery
  134. if (typeof document.implementation.createHTMLDocument === 'undefined') {
  135. document.implementation.createHTMLDocument = function(title = '') {
  136. const htmlDocument
  137. = new DOMParser().parseFromString(
  138. `<html>
  139. <head><title>${title}</title></head>
  140. <body></body>
  141. </html>`,
  142. 'text/xml');
  143. Object.defineProperty(htmlDocument, 'body', {
  144. get() {
  145. return htmlDocument.getElementsByTagName('body')[0];
  146. }
  147. });
  148. return htmlDocument;
  149. };
  150. }
  151. // Element.querySelector
  152. //
  153. // Required by:
  154. // - lib-jitsi-meet/modules/xmpp
  155. const elementPrototype
  156. = Object.getPrototypeOf(document.documentElement);
  157. if (elementPrototype) {
  158. if (typeof elementPrototype.querySelector === 'undefined') {
  159. elementPrototype.querySelector = function(selectors) {
  160. return _querySelector(this, selectors);
  161. };
  162. }
  163. // Element.remove
  164. //
  165. // Required by:
  166. // - lib-jitsi-meet ChatRoom#onPresence parsing
  167. if (typeof elementPrototype.remove === 'undefined') {
  168. elementPrototype.remove = function() {
  169. if (this.parentNode !== null) {
  170. this.parentNode.removeChild(this);
  171. }
  172. };
  173. }
  174. // Element.innerHTML
  175. //
  176. // Required by:
  177. // - jQuery's .append method
  178. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  179. Object.defineProperty(elementPrototype, 'innerHTML', {
  180. get() {
  181. return this.childNodes.toString();
  182. },
  183. set(innerHTML) {
  184. // MDN says: removes all of element's children, parses
  185. // the content string and assigns the resulting nodes as
  186. // children of the element.
  187. // Remove all of element's children.
  188. this.textContent = '';
  189. // Parse the content string.
  190. const d
  191. = new DOMParser().parseFromString(
  192. `<div>${innerHTML}</div>`,
  193. 'text/xml');
  194. // Assign the resulting nodes as children of the
  195. // element.
  196. const documentElement = d.documentElement;
  197. let child;
  198. // eslint-disable-next-line no-cond-assign
  199. while (child = documentElement.firstChild) {
  200. this.appendChild(child);
  201. }
  202. }
  203. });
  204. }
  205. // Element.children
  206. //
  207. // Required by:
  208. // - lib-jitsi-meet ChatRoom#onPresence parsing
  209. if (!elementPrototype.hasOwnProperty('children')) {
  210. Object.defineProperty(elementPrototype, 'children', {
  211. get() {
  212. const nodes = this.childNodes;
  213. const children = [];
  214. let i = 0;
  215. let node = nodes[i];
  216. while (node) {
  217. if (node.nodeType === 1) {
  218. children.push(node);
  219. }
  220. i += 1;
  221. node = nodes[i];
  222. }
  223. return children;
  224. }
  225. });
  226. }
  227. }
  228. // FIXME There is a weird infinite loop related to console.log and
  229. // Document and/or Element at the time of this writing. Work around it
  230. // by patching Node and/or overriding console.log.
  231. const documentPrototype = Object.getPrototypeOf(document);
  232. const nodePrototype
  233. = _getCommonPrototype(documentPrototype, elementPrototype);
  234. if (nodePrototype
  235. // XXX The intention was to find Node from which Document and
  236. // Element extend. If for whatever reason we've reached Object,
  237. // then it doesn't sound like what expected.
  238. && nodePrototype !== Object.getPrototypeOf({})) {
  239. // Override console.log.
  240. const { console } = global;
  241. if (console) {
  242. const loggerLevels = require('jitsi-meet-logger').levels;
  243. Object.keys(loggerLevels).forEach(key => {
  244. const level = loggerLevels[key];
  245. const consoleLog = console[level];
  246. /* eslint-disable prefer-rest-params */
  247. if (typeof consoleLog === 'function') {
  248. console[level] = function(...args) {
  249. // XXX If console's disableYellowBox is truthy, then
  250. // react-native will not automatically display the
  251. // yellow box for the warn level. However, it will
  252. // still display the red box for the error level.
  253. // But I disable the yellow box when I don't want to
  254. // have react-native automatically show me the
  255. // console's output just like in the Release build
  256. // configuration. Because I didn't find a way to
  257. // disable the red box, downgrade the error level to
  258. // warn. The red box will still be displayed but not
  259. // for the error level.
  260. if (console.disableYellowBox && level === 'error') {
  261. console.warn(...args);
  262. return;
  263. }
  264. const { length } = args;
  265. for (let i = 0; i < length; ++i) {
  266. let arg = args[i];
  267. if (arg
  268. && typeof arg !== 'string'
  269. // Limit the console.log override to
  270. // Node (instances).
  271. && nodePrototype.isPrototypeOf(arg)) {
  272. const toString = arg.toString;
  273. if (toString) {
  274. arg = toString.call(arg);
  275. }
  276. }
  277. args[i] = arg;
  278. }
  279. consoleLog.apply(this, args);
  280. };
  281. }
  282. /* eslint-enable prefer-rest-params */
  283. });
  284. }
  285. }
  286. global.document = document;
  287. }
  288. // location
  289. if (typeof global.location === 'undefined') {
  290. global.location = {
  291. href: '',
  292. // Required by:
  293. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  294. search: ''
  295. };
  296. }
  297. const { navigator } = global;
  298. if (navigator) {
  299. // userAgent
  300. //
  301. // Required by:
  302. // - lib-jitsi-meet/modules/browser/BrowserDetection.js
  303. let userAgent = navigator.userAgent || '';
  304. // react-native/version
  305. const { name, version } = require('react-native/package.json');
  306. let rn = name || 'react-native';
  307. version && (rn += `/${version}`);
  308. if (userAgent.indexOf(rn) === -1) {
  309. userAgent = userAgent ? `${rn} ${userAgent}` : rn;
  310. }
  311. // (OS version)
  312. const os = `(${Platform.OS} ${Platform.Version})`;
  313. if (userAgent.indexOf(os) === -1) {
  314. userAgent = userAgent ? `${userAgent} ${os}` : os;
  315. }
  316. navigator.userAgent = userAgent;
  317. }
  318. // WebRTC
  319. require('./webrtc');
  320. // CallStats
  321. //
  322. // Required by:
  323. // - lib-jitsi-meet
  324. require('react-native-callstats/csio-polyfill');
  325. global.callstats = require('react-native-callstats/callstats');
  326. // XMLHttpRequest
  327. if (global.XMLHttpRequest) {
  328. const { prototype } = global.XMLHttpRequest;
  329. // XMLHttpRequest.responseXML
  330. //
  331. // Required by:
  332. // - Strophe
  333. if (prototype && !prototype.hasOwnProperty('responseXML')) {
  334. Object.defineProperty(prototype, 'responseXML', {
  335. get() {
  336. const { responseText } = this;
  337. return (
  338. responseText
  339. && new DOMParser().parseFromString(
  340. responseText,
  341. 'text/xml'));
  342. }
  343. });
  344. }
  345. }
  346. // Timers
  347. //
  348. // React Native's timers won't run while the app is in the background, this
  349. // is a known limitation. Replace them with a background-friendly
  350. // alternative.
  351. //
  352. // Required by:
  353. // - lib-jitsi-meet
  354. // - Strophe
  355. if (Platform.OS === 'android') {
  356. global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  357. global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  358. global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
  359. global.setTimeout = (fn, ms = 0) => BackgroundTimer.setTimeout(fn, ms);
  360. }
  361. // localStorage
  362. if (typeof global.localStorage === 'undefined') {
  363. global.localStorage = new Storage('@jitsi-meet/');
  364. }
  365. // sessionStorage
  366. //
  367. // Required by:
  368. // - herment
  369. // - Strophe
  370. if (typeof global.sessionStorage === 'undefined') {
  371. global.sessionStorage = new Storage();
  372. }
  373. })(global || window || this); // eslint-disable-line no-invalid-this