您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

polyfills-browser.js 14KB

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