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

polyfills-browser.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. return p;
  23. }
  24. if ((p = Object.getPrototypeOf(b)) && (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').DOMParser;
  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. // Document.querySelector
  123. //
  124. // Required by:
  125. // - strophejs-plugins/caps/strophe.caps.jsonly.js
  126. const documentPrototype = Object.getPrototypeOf(document);
  127. if (documentPrototype) {
  128. if (typeof documentPrototype.querySelector === 'undefined') {
  129. documentPrototype.querySelector = function(selectors) {
  130. return _querySelector(this.elementNode, selectors);
  131. };
  132. }
  133. }
  134. // Element.querySelector
  135. //
  136. // Required by:
  137. // - strophejs-plugins/caps/strophe.caps.jsonly.js
  138. const elementPrototype
  139. = Object.getPrototypeOf(document.documentElement);
  140. if (elementPrototype) {
  141. if (typeof elementPrototype.querySelector === 'undefined') {
  142. elementPrototype.querySelector = function(selectors) {
  143. return _querySelector(this, selectors);
  144. };
  145. }
  146. // Element.innerHTML
  147. //
  148. // Required by:
  149. // - jQuery's .append method
  150. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  151. Object.defineProperty(elementPrototype, 'innerHTML', {
  152. get() {
  153. return this.childNodes.toString();
  154. },
  155. set(innerHTML) {
  156. // MDN says: removes all of element's children, parses
  157. // the content string and assigns the resulting nodes as
  158. // children of the element.
  159. // Remove all of element's children.
  160. this.textContent = '';
  161. // Parse the content string.
  162. const d
  163. = new DOMParser().parseFromString(
  164. `<div>${innerHTML}</div>`,
  165. 'text/xml');
  166. // Assign the resulting nodes as children of the
  167. // element.
  168. const documentElement = d.documentElement;
  169. let child;
  170. // eslint-disable-next-line no-cond-assign
  171. while (child = documentElement.firstChild) {
  172. this.appendChild(child);
  173. }
  174. }
  175. });
  176. }
  177. }
  178. // FIXME There is a weird infinite loop related to console.log and
  179. // Document and/or Element at the time of this writing. Work around it
  180. // by patching Node and/or overriding console.log.
  181. const nodePrototype
  182. = _getCommonPrototype(documentPrototype, elementPrototype);
  183. if (nodePrototype
  184. // XXX The intention was to find Node from which Document and
  185. // Element extend. If for whatever reason we've reached Object,
  186. // then it doesn't sound like what expected.
  187. && nodePrototype !== Object.getPrototypeOf({})) {
  188. // Override console.log.
  189. const { console } = global;
  190. if (console) {
  191. const loggerLevels = require('jitsi-meet-logger').levels;
  192. Object.keys(loggerLevels).forEach(key => {
  193. const level = loggerLevels[key];
  194. const consoleLog = console[level];
  195. /* eslint-disable prefer-rest-params */
  196. if (typeof consoleLog === 'function') {
  197. console[level] = function(...args) {
  198. const length = args.length;
  199. for (let i = 0; i < length; ++i) {
  200. let arg = args[i];
  201. if (arg
  202. && typeof arg !== 'string'
  203. // Limit the console.log override to
  204. // Node (instances).
  205. && nodePrototype.isPrototypeOf(arg)) {
  206. const toString = arg.toString;
  207. if (toString) {
  208. arg = toString.call(arg);
  209. }
  210. }
  211. args[i] = arg;
  212. }
  213. consoleLog.apply(this, args);
  214. };
  215. }
  216. /* eslint-enable prefer-rest-params */
  217. });
  218. }
  219. }
  220. global.document = document;
  221. }
  222. // localStorage
  223. if (typeof global.localStorage === 'undefined') {
  224. global.localStorage = new Storage('@jitsi-meet/');
  225. }
  226. // location
  227. if (typeof global.location === 'undefined') {
  228. global.location = {
  229. href: '',
  230. // Required by:
  231. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  232. search: ''
  233. };
  234. }
  235. const { navigator } = global;
  236. if (navigator) {
  237. // platform
  238. //
  239. // Required by:
  240. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  241. if (typeof navigator.platform === 'undefined') {
  242. navigator.platform = '';
  243. }
  244. // plugins
  245. //
  246. // Required by:
  247. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  248. if (typeof navigator.plugins === 'undefined') {
  249. navigator.plugins = [];
  250. }
  251. // userAgent
  252. //
  253. // Required by:
  254. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  255. // - lib-jitsi-meet/modules/RTC/RTCBrowserType.js
  256. (() => {
  257. const reactNativePackageJSON = require('react-native/package.json');
  258. let userAgent = reactNativePackageJSON.name || 'react-native';
  259. const version = reactNativePackageJSON.version;
  260. if (version) {
  261. userAgent += `/${version}`;
  262. }
  263. if (typeof navigator.userAgent !== 'undefined') {
  264. const s = navigator.userAgent.toString();
  265. if (s.length > 0 && s.indexOf(userAgent) === -1) {
  266. userAgent = `${s} ${userAgent}`;
  267. }
  268. }
  269. navigator.userAgent = userAgent;
  270. })();
  271. }
  272. // sessionStorage
  273. //
  274. // Required by:
  275. // - Strophe
  276. // - herment - requires a working sessionStorage, no empty impl. functions
  277. if (typeof global.sessionStorage === 'undefined') {
  278. let internalStorage = {};
  279. global.sessionStorage = {
  280. clear() {
  281. internalStorage = {};
  282. },
  283. getItem(key) {
  284. return internalStorage[key];
  285. },
  286. removeItem(key) {
  287. delete internalStorage[key];
  288. },
  289. setItem(key, value) {
  290. internalStorage[key] = value;
  291. }
  292. };
  293. }
  294. // WebRTC
  295. require('./polyfills-webrtc');
  296. require('react-native-callstats/csio-polyfill');
  297. // XMLHttpRequest
  298. if (global.XMLHttpRequest) {
  299. const prototype = global.XMLHttpRequest.prototype;
  300. // XMLHttpRequest.responseXML
  301. //
  302. // Required by:
  303. // - Strophe
  304. if (prototype && !prototype.hasOwnProperty('responseXML')) {
  305. Object.defineProperty(prototype, 'responseXML', {
  306. get() {
  307. const responseText = this.responseText;
  308. let responseXML;
  309. if (responseText) {
  310. responseXML
  311. = new DOMParser().parseFromString(
  312. responseText,
  313. 'text/xml');
  314. }
  315. return responseXML;
  316. }
  317. });
  318. }
  319. }
  320. // Timers
  321. //
  322. // React Native's timers won't run while the app is in the background, this
  323. // is a known limitation. Replace them with a background-friendly
  324. // alternative.
  325. //
  326. // Required by:
  327. // - lib-jitsi-meet
  328. // - Strophe
  329. global.clearTimeout
  330. = window.clearTimeout
  331. = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  332. global.clearInterval
  333. = window.clearInterval
  334. = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  335. global.setInterval
  336. = window.setInterval
  337. = BackgroundTimer.setInterval.bind(BackgroundTimer);
  338. global.setTimeout
  339. = window.setTimeout
  340. = BackgroundTimer.setTimeout.bind(BackgroundTimer);
  341. })(global || window || this); // eslint-disable-line no-invalid-this