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

polyfills-browser.js 15KB

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