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

polyfills-browser.js 15KB

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