Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

polyfills-browser.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. import Storage from './Storage';
  6. /**
  7. * Gets the first common prototype of two specified Objects (treating the
  8. * objects themselves as prototypes as well).
  9. *
  10. * @param {Object} a - The first prototype chain to climb in search of a common
  11. * prototype.
  12. * @param {Object} b - The second prototype chain to climb in search of a common
  13. * prototype.
  14. * @returns {Object|undefined} - The first common prototype of a and b.
  15. */
  16. function _getCommonPrototype(a, b) {
  17. // Allow the arguments to be prototypes themselves.
  18. if (a === b) {
  19. return a;
  20. }
  21. let p;
  22. if (((p = Object.getPrototypeOf(a)) && (p = _getCommonPrototype(b, p)))
  23. || ((p = Object.getPrototypeOf(b))
  24. && (p = _getCommonPrototype(a, p)))) {
  25. return p;
  26. }
  27. return undefined;
  28. }
  29. /**
  30. * Implements an absolute minimum of the common logic of Document.querySelector
  31. * and Element.querySelector. Implements the most simple of selectors necessary
  32. * to satisfy the call sites at the time 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. // addEventListener
  79. //
  80. // Required by:
  81. // - jQuery
  82. if (typeof global.addEventListener === 'undefined') {
  83. // eslint-disable-next-line no-empty-function
  84. global.addEventListener = () => {};
  85. }
  86. // Array.prototype[@@iterator]
  87. //
  88. // Required by:
  89. // - for...of statement use(s) in lib-jitsi-meet
  90. const arrayPrototype = Array.prototype;
  91. if (typeof arrayPrototype['@@iterator'] === 'undefined') {
  92. arrayPrototype['@@iterator'] = function() {
  93. return new Iterator(this);
  94. };
  95. }
  96. // document
  97. //
  98. // Required by:
  99. // - jQuery
  100. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  101. // - Strophe
  102. if (typeof global.document === 'undefined') {
  103. const document
  104. = new DOMParser().parseFromString(
  105. '<html><head></head><body></body></html>',
  106. 'text/xml');
  107. // document.addEventListener
  108. //
  109. // Required by:
  110. // - jQuery
  111. if (typeof document.addEventListener === 'undefined') {
  112. // eslint-disable-next-line no-empty-function
  113. document.addEventListener = () => {};
  114. }
  115. // document.cookie
  116. //
  117. // Required by:
  118. // - herment
  119. if (typeof document.cookie === 'undefined') {
  120. document.cookie = '';
  121. }
  122. // Element.querySelector
  123. //
  124. // Required by:
  125. // - lib-jitsi-meet/modules/xmpp
  126. const elementPrototype
  127. = Object.getPrototypeOf(document.documentElement);
  128. if (elementPrototype) {
  129. if (typeof elementPrototype.querySelector === 'undefined') {
  130. elementPrototype.querySelector = function(selectors) {
  131. return _querySelector(this, selectors);
  132. };
  133. }
  134. // Element.innerHTML
  135. //
  136. // Required by:
  137. // - jQuery's .append method
  138. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  139. Object.defineProperty(elementPrototype, 'innerHTML', {
  140. get() {
  141. return this.childNodes.toString();
  142. },
  143. set(innerHTML) {
  144. // MDN says: removes all of element's children, parses
  145. // the content string and assigns the resulting nodes as
  146. // children of the element.
  147. // Remove all of element's children.
  148. this.textContent = '';
  149. // Parse the content string.
  150. const d
  151. = new DOMParser().parseFromString(
  152. `<div>${innerHTML}</div>`,
  153. 'text/xml');
  154. // Assign the resulting nodes as children of the
  155. // element.
  156. const documentElement = d.documentElement;
  157. let child;
  158. // eslint-disable-next-line no-cond-assign
  159. while (child = documentElement.firstChild) {
  160. this.appendChild(child);
  161. }
  162. }
  163. });
  164. }
  165. }
  166. // FIXME There is a weird infinite loop related to console.log and
  167. // Document and/or Element at the time of this writing. Work around it
  168. // by patching Node and/or overriding console.log.
  169. const documentPrototype = Object.getPrototypeOf(document);
  170. const nodePrototype
  171. = _getCommonPrototype(documentPrototype, elementPrototype);
  172. if (nodePrototype
  173. // XXX The intention was to find Node from which Document and
  174. // Element extend. If for whatever reason we've reached Object,
  175. // then it doesn't sound like what expected.
  176. && nodePrototype !== Object.getPrototypeOf({})) {
  177. // Override console.log.
  178. const { console } = global;
  179. if (console) {
  180. const loggerLevels = require('jitsi-meet-logger').levels;
  181. console.disableYellowBox = true;
  182. Object.keys(loggerLevels).forEach(key => {
  183. const level = loggerLevels[key];
  184. const consoleLog = console[level];
  185. /* eslint-disable prefer-rest-params */
  186. if (typeof consoleLog === 'function') {
  187. console[level] = function(...args) {
  188. // XXX If console's disableYellowBox is truthy, then
  189. // react-native will not automatically display the
  190. // yellow box for the warn level. However, it will
  191. // still display the red box for the error level.
  192. // But I disable the yellow box when I don't want to
  193. // have react-native automatically show me the
  194. // console's output just like in the Release build
  195. // configuration. Because I didn't find a way to
  196. // disable the red box, downgrade the error level to
  197. // warn. The red box will still be displayed but not
  198. // for the error level.
  199. if (console.disableYellowBox && level === 'error') {
  200. console.warn(...args);
  201. return;
  202. }
  203. const { length } = args;
  204. for (let i = 0; i < length; ++i) {
  205. let arg = args[i];
  206. if (arg
  207. && typeof arg !== 'string'
  208. // Limit the console.log override to
  209. // Node (instances).
  210. && nodePrototype.isPrototypeOf(arg)) {
  211. const toString = arg.toString;
  212. if (toString) {
  213. arg = toString.call(arg);
  214. }
  215. }
  216. args[i] = arg;
  217. }
  218. consoleLog.apply(this, args);
  219. };
  220. }
  221. /* eslint-enable prefer-rest-params */
  222. });
  223. }
  224. }
  225. global.document = document;
  226. }
  227. // localStorage
  228. if (typeof global.localStorage === 'undefined') {
  229. global.localStorage = new Storage('@jitsi-meet/');
  230. }
  231. // location
  232. if (typeof global.location === 'undefined') {
  233. global.location = {
  234. href: '',
  235. // Required by:
  236. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  237. search: ''
  238. };
  239. }
  240. const { navigator } = global;
  241. if (navigator) {
  242. // platform
  243. //
  244. // Required by:
  245. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  246. if (typeof navigator.platform === 'undefined') {
  247. navigator.platform = '';
  248. }
  249. // plugins
  250. //
  251. // Required by:
  252. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  253. if (typeof navigator.plugins === 'undefined') {
  254. navigator.plugins = [];
  255. }
  256. // userAgent
  257. //
  258. // Required by:
  259. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  260. // - lib-jitsi-meet/modules/RTC/RTCBrowserType.js
  261. let userAgent = navigator.userAgent || '';
  262. // react-native/version
  263. const { name, version } = require('react-native/package.json');
  264. let rn = name || 'react-native';
  265. version && (rn += `/${version}`);
  266. if (userAgent.indexOf(rn) === -1) {
  267. userAgent = userAgent ? `${rn} ${userAgent}` : rn;
  268. }
  269. // (OS version)
  270. const os = `(${Platform.OS} ${Platform.Version})`;
  271. if (userAgent.indexOf(os) === -1) {
  272. userAgent = userAgent ? `${userAgent} ${os}` : os;
  273. }
  274. navigator.userAgent = userAgent;
  275. }
  276. // sessionStorage
  277. //
  278. // Required by:
  279. // - herment
  280. // - Strophe
  281. if (typeof global.sessionStorage === 'undefined') {
  282. global.sessionStorage = new Storage();
  283. }
  284. // WebRTC
  285. require('./polyfills-webrtc');
  286. require('react-native-callstats/csio-polyfill');
  287. // XMLHttpRequest
  288. if (global.XMLHttpRequest) {
  289. const { prototype } = global.XMLHttpRequest;
  290. // XMLHttpRequest.responseXML
  291. //
  292. // Required by:
  293. // - Strophe
  294. if (prototype && !prototype.hasOwnProperty('responseXML')) {
  295. Object.defineProperty(prototype, 'responseXML', {
  296. get() {
  297. const { responseText } = this;
  298. return (
  299. responseText
  300. && new DOMParser().parseFromString(
  301. responseText,
  302. 'text/xml'));
  303. }
  304. });
  305. }
  306. }
  307. // Timers
  308. //
  309. // React Native's timers won't run while the app is in the background, this
  310. // is a known limitation. Replace them with a background-friendly
  311. // alternative.
  312. //
  313. // Required by:
  314. // - lib-jitsi-meet
  315. // - Strophe
  316. global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  317. global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  318. global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
  319. global.setTimeout = BackgroundTimer.setTimeout.bind(BackgroundTimer);
  320. })(global || window || this); // eslint-disable-line no-invalid-this