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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import { Platform } from 'react-native';
  2. import BackgroundTimer from 'react-native-background-timer';
  3. import '@webcomponents/url'; // Polyfill for URL constructor
  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. // removeEventListener
  92. //
  93. // Required by:
  94. // - features/base/conference/middleware
  95. if (typeof global.removeEventListener === 'undefined') {
  96. // eslint-disable-next-line no-empty-function
  97. global.removeEventListener = () => {};
  98. }
  99. // document
  100. //
  101. // Required by:
  102. // - jQuery
  103. // - Strophe
  104. if (typeof global.document === 'undefined') {
  105. const document
  106. = new DOMParser().parseFromString(
  107. '<html><head></head><body></body></html>',
  108. 'text/xml');
  109. // document.addEventListener
  110. //
  111. // Required by:
  112. // - jQuery
  113. if (typeof document.addEventListener === 'undefined') {
  114. // eslint-disable-next-line no-empty-function
  115. document.addEventListener = () => {};
  116. }
  117. // document.cookie
  118. //
  119. // Required by:
  120. // - herment
  121. if (typeof document.cookie === 'undefined') {
  122. document.cookie = '';
  123. }
  124. // document.implementation.createHTMLDocument
  125. //
  126. // Required by:
  127. // - jQuery
  128. if (typeof document.implementation.createHTMLDocument === 'undefined') {
  129. document.implementation.createHTMLDocument = function(title = '') {
  130. const htmlDocument
  131. = new DOMParser().parseFromString(
  132. `<html>
  133. <head><title>${title}</title></head>
  134. <body></body>
  135. </html>`,
  136. 'text/xml');
  137. Object.defineProperty(htmlDocument, 'body', {
  138. get() {
  139. return htmlDocument.getElementsByTagName('body')[0];
  140. }
  141. });
  142. return htmlDocument;
  143. };
  144. }
  145. // Element.querySelector
  146. //
  147. // Required by:
  148. // - lib-jitsi-meet/modules/xmpp
  149. const elementPrototype
  150. = Object.getPrototypeOf(document.documentElement);
  151. if (elementPrototype) {
  152. if (typeof elementPrototype.querySelector === 'undefined') {
  153. elementPrototype.querySelector = function(selectors) {
  154. return _querySelector(this, selectors);
  155. };
  156. }
  157. // Element.remove
  158. //
  159. // Required by:
  160. // - lib-jitsi-meet ChatRoom#onPresence parsing
  161. if (typeof elementPrototype.remove === 'undefined') {
  162. elementPrototype.remove = function() {
  163. if (this.parentNode !== null) {
  164. this.parentNode.removeChild(this);
  165. }
  166. };
  167. }
  168. // Element.innerHTML
  169. //
  170. // Required by:
  171. // - jQuery's .append method
  172. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  173. Object.defineProperty(elementPrototype, 'innerHTML', {
  174. get() {
  175. return this.childNodes.toString();
  176. },
  177. set(innerHTML) {
  178. // MDN says: removes all of element's children, parses
  179. // the content string and assigns the resulting nodes as
  180. // children of the element.
  181. // Remove all of element's children.
  182. this.textContent = '';
  183. // Parse the content string.
  184. const d
  185. = new DOMParser().parseFromString(
  186. `<div>${innerHTML}</div>`,
  187. 'text/xml');
  188. // Assign the resulting nodes as children of the
  189. // element.
  190. const documentElement = d.documentElement;
  191. let child;
  192. // eslint-disable-next-line no-cond-assign
  193. while (child = documentElement.firstChild) {
  194. this.appendChild(child);
  195. }
  196. }
  197. });
  198. }
  199. // Element.children
  200. //
  201. // Required by:
  202. // - lib-jitsi-meet ChatRoom#onPresence parsing
  203. if (!elementPrototype.hasOwnProperty('children')) {
  204. Object.defineProperty(elementPrototype, 'children', {
  205. get() {
  206. const nodes = this.childNodes;
  207. const children = [];
  208. let i = 0;
  209. let node = nodes[i];
  210. while (node) {
  211. if (node.nodeType === 1) {
  212. children.push(node);
  213. }
  214. i += 1;
  215. node = nodes[i];
  216. }
  217. return children;
  218. }
  219. });
  220. }
  221. }
  222. // FIXME There is a weird infinite loop related to console.log and
  223. // Document and/or Element at the time of this writing. Work around it
  224. // by patching Node and/or overriding console.log.
  225. const documentPrototype = Object.getPrototypeOf(document);
  226. const nodePrototype
  227. = _getCommonPrototype(documentPrototype, elementPrototype);
  228. if (nodePrototype
  229. // XXX The intention was to find Node from which Document and
  230. // Element extend. If for whatever reason we've reached Object,
  231. // then it doesn't sound like what expected.
  232. && nodePrototype !== Object.getPrototypeOf({})) {
  233. // Override console.log.
  234. const { console } = global;
  235. if (console) {
  236. const loggerLevels = require('jitsi-meet-logger').levels;
  237. Object.keys(loggerLevels).forEach(key => {
  238. const level = loggerLevels[key];
  239. const consoleLog = console[level];
  240. /* eslint-disable prefer-rest-params */
  241. if (typeof consoleLog === 'function') {
  242. console[level] = function(...args) {
  243. // XXX If console's disableYellowBox is truthy, then
  244. // react-native will not automatically display the
  245. // yellow box for the warn level. However, it will
  246. // still display the red box for the error level.
  247. // But I disable the yellow box when I don't want to
  248. // have react-native automatically show me the
  249. // console's output just like in the Release build
  250. // configuration. Because I didn't find a way to
  251. // disable the red box, downgrade the error level to
  252. // warn. The red box will still be displayed but not
  253. // for the error level.
  254. if (console.disableYellowBox && level === 'error') {
  255. console.warn(...args);
  256. return;
  257. }
  258. const { length } = args;
  259. for (let i = 0; i < length; ++i) {
  260. let arg = args[i];
  261. if (arg
  262. && typeof arg !== 'string'
  263. // Limit the console.log override to
  264. // Node (instances).
  265. && nodePrototype.isPrototypeOf(arg)) {
  266. const toString = arg.toString;
  267. if (toString) {
  268. arg = toString.call(arg);
  269. }
  270. }
  271. args[i] = arg;
  272. }
  273. consoleLog.apply(this, args);
  274. };
  275. }
  276. /* eslint-enable prefer-rest-params */
  277. });
  278. }
  279. }
  280. global.document = document;
  281. }
  282. // location
  283. if (typeof global.location === 'undefined') {
  284. global.location = {
  285. href: '',
  286. // Required by:
  287. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  288. search: ''
  289. };
  290. }
  291. const { navigator } = global;
  292. if (navigator) {
  293. // userAgent
  294. //
  295. // Required by:
  296. // - lib-jitsi-meet/modules/browser/BrowserDetection.js
  297. let userAgent = navigator.userAgent || '';
  298. // react-native/version
  299. const { name, version } = require('react-native/package.json');
  300. let rn = name || 'react-native';
  301. version && (rn += `/${version}`);
  302. if (userAgent.indexOf(rn) === -1) {
  303. userAgent = userAgent ? `${rn} ${userAgent}` : rn;
  304. }
  305. // (OS version)
  306. const os = `(${Platform.OS} ${Platform.Version})`;
  307. if (userAgent.indexOf(os) === -1) {
  308. userAgent = userAgent ? `${userAgent} ${os}` : os;
  309. }
  310. navigator.userAgent = userAgent;
  311. }
  312. // WebRTC
  313. require('./webrtc');
  314. // CallStats
  315. //
  316. // Required by:
  317. // - lib-jitsi-meet
  318. require('react-native-callstats/csio-polyfill');
  319. global.callstats = require('react-native-callstats/callstats');
  320. // XMLHttpRequest
  321. if (global.XMLHttpRequest) {
  322. const { prototype } = global.XMLHttpRequest;
  323. // XMLHttpRequest.responseXML
  324. //
  325. // Required by:
  326. // - Strophe
  327. if (prototype && !prototype.hasOwnProperty('responseXML')) {
  328. Object.defineProperty(prototype, 'responseXML', {
  329. get() {
  330. const { responseText } = this;
  331. return (
  332. responseText
  333. && new DOMParser().parseFromString(
  334. responseText,
  335. 'text/xml'));
  336. }
  337. });
  338. }
  339. }
  340. // Timers
  341. //
  342. // React Native's timers won't run while the app is in the background, this
  343. // is a known limitation. Replace them with a background-friendly
  344. // alternative.
  345. //
  346. // Required by:
  347. // - lib-jitsi-meet
  348. // - Strophe
  349. if (Platform.OS === 'android') {
  350. global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  351. global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  352. global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
  353. global.setTimeout = (fn, ms = 0) => BackgroundTimer.setTimeout(fn, ms);
  354. }
  355. // localStorage
  356. if (typeof global.localStorage === 'undefined') {
  357. global.localStorage = new Storage('@jitsi-meet/');
  358. }
  359. // sessionStorage
  360. //
  361. // Required by:
  362. // - herment
  363. // - Strophe
  364. if (typeof global.sessionStorage === 'undefined') {
  365. global.sessionStorage = new Storage();
  366. }
  367. })(global || window || this); // eslint-disable-line no-invalid-this