Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

polyfills-browser.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. Object.keys(loggerLevels).forEach(key => {
  182. const level = loggerLevels[key];
  183. const consoleLog = console[level];
  184. /* eslint-disable prefer-rest-params */
  185. if (typeof consoleLog === 'function') {
  186. console[level] = function(...args) {
  187. // XXX If console's disableYellowBox is truthy, then
  188. // react-native will not automatically display the
  189. // yellow box for the warn level. However, it will
  190. // still display the red box for the error level.
  191. // But I disable the yellow box when I don't want to
  192. // have react-native automatically show me the
  193. // console's output just like in the Release build
  194. // configuration. Because I didn't find a way to
  195. // disable the red box, downgrade the error level to
  196. // warn. The red box will still be displayed but not
  197. // for the error level.
  198. if (console.disableYellowBox && level === 'error') {
  199. console.warn(...args);
  200. return;
  201. }
  202. const { length } = args;
  203. for (let i = 0; i < length; ++i) {
  204. let arg = args[i];
  205. if (arg
  206. && typeof arg !== 'string'
  207. // Limit the console.log override to
  208. // Node (instances).
  209. && nodePrototype.isPrototypeOf(arg)) {
  210. const toString = arg.toString;
  211. if (toString) {
  212. arg = toString.call(arg);
  213. }
  214. }
  215. args[i] = arg;
  216. }
  217. consoleLog.apply(this, args);
  218. };
  219. }
  220. /* eslint-enable prefer-rest-params */
  221. });
  222. }
  223. }
  224. global.document = document;
  225. }
  226. // localStorage
  227. if (typeof global.localStorage === 'undefined') {
  228. global.localStorage = new Storage('@jitsi-meet/');
  229. }
  230. // location
  231. if (typeof global.location === 'undefined') {
  232. global.location = {
  233. href: '',
  234. // Required by:
  235. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  236. search: ''
  237. };
  238. }
  239. const { navigator } = global;
  240. if (navigator) {
  241. // platform
  242. //
  243. // Required by:
  244. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  245. if (typeof navigator.platform === 'undefined') {
  246. navigator.platform = '';
  247. }
  248. // plugins
  249. //
  250. // Required by:
  251. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  252. if (typeof navigator.plugins === 'undefined') {
  253. navigator.plugins = [];
  254. }
  255. // userAgent
  256. //
  257. // Required by:
  258. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  259. // - lib-jitsi-meet/modules/RTC/RTCBrowserType.js
  260. let userAgent = navigator.userAgent || '';
  261. // react-native/version
  262. const { name, version } = require('react-native/package.json');
  263. let rn = name || 'react-native';
  264. version && (rn += `/${version}`);
  265. if (userAgent.indexOf(rn) === -1) {
  266. userAgent = userAgent ? `${rn} ${userAgent}` : rn;
  267. }
  268. // (OS version)
  269. const os = `(${Platform.OS} ${Platform.Version})`;
  270. if (userAgent.indexOf(os) === -1) {
  271. userAgent = userAgent ? `${userAgent} ${os}` : os;
  272. }
  273. navigator.userAgent = userAgent;
  274. }
  275. // sessionStorage
  276. //
  277. // Required by:
  278. // - herment
  279. // - Strophe
  280. if (typeof global.sessionStorage === 'undefined') {
  281. global.sessionStorage = new Storage();
  282. }
  283. // WebRTC
  284. require('./polyfills-webrtc');
  285. require('react-native-callstats/csio-polyfill');
  286. // XMLHttpRequest
  287. if (global.XMLHttpRequest) {
  288. const { prototype } = global.XMLHttpRequest;
  289. // XMLHttpRequest.responseXML
  290. //
  291. // Required by:
  292. // - Strophe
  293. if (prototype && !prototype.hasOwnProperty('responseXML')) {
  294. Object.defineProperty(prototype, 'responseXML', {
  295. get() {
  296. const { responseText } = this;
  297. return (
  298. responseText
  299. && new DOMParser().parseFromString(
  300. responseText,
  301. 'text/xml'));
  302. }
  303. });
  304. }
  305. }
  306. // Timers
  307. //
  308. // React Native's timers won't run while the app is in the background, this
  309. // is a known limitation. Replace them with a background-friendly
  310. // alternative.
  311. //
  312. // Required by:
  313. // - lib-jitsi-meet
  314. // - Strophe
  315. global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  316. global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  317. global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
  318. global.setTimeout = BackgroundTimer.setTimeout.bind(BackgroundTimer);
  319. })(global || window || this); // eslint-disable-line no-invalid-this