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

polyfills-browser.js 12KB

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