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

polyfills-browser.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import BackgroundTimer from 'react-native-background-timer';
  2. /**
  3. * Gets the first common prototype of two specified Objects (treating the
  4. * objects themselves as prototypes as well).
  5. *
  6. * @param {Object} a - The first prototype chain to climb in search of a common
  7. * prototype.
  8. * @param {Object} b - The second prototype chain to climb in search of a common
  9. * prototype.
  10. * @returns {Object|undefined} - The first common prototype of a and b.
  11. */
  12. function _getCommonPrototype(a, b) {
  13. // Allow the arguments to be prototypes themselves.
  14. if (a === b) {
  15. return a;
  16. }
  17. let p;
  18. if ((p = Object.getPrototypeOf(a)) && (p = _getCommonPrototype(b, p))) {
  19. return p;
  20. }
  21. if ((p = Object.getPrototypeOf(b)) && (p = _getCommonPrototype(a, p))) {
  22. return p;
  23. }
  24. return undefined;
  25. }
  26. /**
  27. * Implements an absolute minimum of the common logic of Document.querySelector
  28. * and Element.querySelector. Implements the most simple of selectors necessary
  29. * to satisfy the call sites at the time of this writing i.e. select by tagName.
  30. *
  31. * @param {Node} node - The Node which is the root of the tree to query.
  32. * @param {string} selectors - The group of CSS selectors to match on.
  33. * @returns {Element} - The first Element which is a descendant of the specified
  34. * node and matches the specified group of selectors.
  35. */
  36. function _querySelector(node, selectors) {
  37. let element = null;
  38. node && _visitNode(node, n => {
  39. if (n.nodeType === 1 /* ELEMENT_NODE */
  40. && n.nodeName === selectors) {
  41. element = n;
  42. return true;
  43. }
  44. return false;
  45. });
  46. return element;
  47. }
  48. /**
  49. * Visits each Node in the tree of a specific root Node (using depth-first
  50. * traversal) and invokes a specific callback until the callback returns true.
  51. *
  52. * @param {Node} node - The root Node which represents the tree of Nodes to
  53. * visit.
  54. * @param {Function} callback - The callback to invoke with each visited Node.
  55. * @returns {boolean} - True if the specified callback returned true for a Node
  56. * (at which point the visiting stopped); otherwise, false.
  57. */
  58. function _visitNode(node, callback) {
  59. if (callback(node)) {
  60. return true;
  61. }
  62. /* eslint-disable no-param-reassign, no-extra-parens */
  63. if ((node = node.firstChild)) {
  64. do {
  65. if (_visitNode(node, callback)) {
  66. return true;
  67. }
  68. } while ((node = node.nextSibling));
  69. }
  70. /* eslint-enable no-param-reassign, no-extra-parens */
  71. return false;
  72. }
  73. (global => {
  74. // Polyfill for URL constructor
  75. require('url-polyfill');
  76. const DOMParser = require('xmldom').DOMParser;
  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. // document
  86. //
  87. // Required by:
  88. // - jQuery
  89. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  90. // - Strophe
  91. if (typeof global.document === 'undefined') {
  92. const document
  93. = new DOMParser().parseFromString(
  94. /* source */ '<html><head></head><body></body></html>',
  95. /* mineType */ 'text/xml');
  96. // document.addEventListener
  97. //
  98. // Required by:
  99. // - jQuery
  100. if (typeof document.addEventListener === 'undefined') {
  101. // eslint-disable-next-line no-empty-function
  102. document.addEventListener = () => {};
  103. }
  104. // Document.querySelector
  105. //
  106. // Required by:
  107. // - strophejs-plugins/caps/strophe.caps.jsonly.js
  108. const documentPrototype = Object.getPrototypeOf(document);
  109. if (documentPrototype) {
  110. if (typeof documentPrototype.querySelector === 'undefined') {
  111. documentPrototype.querySelector = function(selectors) {
  112. return _querySelector(this.elementNode, selectors);
  113. };
  114. }
  115. }
  116. // Element.querySelector
  117. //
  118. // Required by:
  119. // - strophejs-plugins/caps/strophe.caps.jsonly.js
  120. const elementPrototype
  121. = Object.getPrototypeOf(document.documentElement);
  122. if (elementPrototype) {
  123. if (typeof elementPrototype.querySelector === 'undefined') {
  124. elementPrototype.querySelector = function(selectors) {
  125. return _querySelector(this, selectors);
  126. };
  127. }
  128. // Element.innerHTML
  129. //
  130. // Required by:
  131. // - jQuery's .append method
  132. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  133. Object.defineProperty(elementPrototype, 'innerHTML', {
  134. get() {
  135. return this.childNodes.toString();
  136. },
  137. set(innerHTML) {
  138. // MDN says: removes all of element's children, parses
  139. // the content string and assigns the resulting nodes as
  140. // children of the element.
  141. // Remove all of element's children.
  142. this.textContent = '';
  143. // Parse the content string.
  144. const d
  145. = new DOMParser().parseFromString(
  146. `<div>${innerHTML}</div>`,
  147. 'text/xml');
  148. // Assign the resulting nodes as children of the
  149. // element.
  150. const documentElement = d.documentElement;
  151. let child;
  152. // eslint-disable-next-line no-cond-assign
  153. while (child = documentElement.firstChild) {
  154. this.appendChild(child);
  155. }
  156. }
  157. });
  158. }
  159. }
  160. // FIXME There is a weird infinite loop related to console.log and
  161. // Document and/or Element at the time of this writing. Work around it
  162. // by patching Node and/or overriding console.log.
  163. const nodePrototype
  164. = _getCommonPrototype(documentPrototype, elementPrototype);
  165. if (nodePrototype
  166. // XXX The intention was to find Node from which Document and
  167. // Element extend. If for whatever reason we've reached Object,
  168. // then it doesn't sound like what expected.
  169. && nodePrototype !== Object.getPrototypeOf({})) {
  170. // Override console.log.
  171. const console = global.console;
  172. if (console) {
  173. const loggerLevels = require('jitsi-meet-logger').levels;
  174. Object.keys(loggerLevels).forEach(key => {
  175. const level = loggerLevels[key];
  176. const consoleLog = console[level];
  177. /* eslint-disable prefer-rest-params */
  178. if (typeof consoleLog === 'function') {
  179. console[level] = function(...args) {
  180. const length = args.length;
  181. for (let i = 0; i < length; ++i) {
  182. let arg = args[i];
  183. if (arg
  184. && typeof arg !== 'string'
  185. // Limit the console.log override to
  186. // Node (instances).
  187. && nodePrototype.isPrototypeOf(arg)) {
  188. const toString = arg.toString;
  189. if (toString) {
  190. arg = toString.call(arg);
  191. }
  192. }
  193. args[i] = arg;
  194. }
  195. consoleLog.apply(this, args);
  196. };
  197. }
  198. /* eslint-enable prefer-rest-params */
  199. });
  200. }
  201. }
  202. global.document = document;
  203. }
  204. // location
  205. if (typeof global.location === 'undefined') {
  206. global.location = {
  207. href: ''
  208. };
  209. }
  210. const navigator = global.navigator;
  211. if (navigator) {
  212. // platform
  213. //
  214. // Required by:
  215. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  216. if (typeof navigator.platform === 'undefined') {
  217. navigator.platform = '';
  218. }
  219. // plugins
  220. //
  221. // Required by:
  222. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  223. if (typeof navigator.plugins === 'undefined') {
  224. navigator.plugins = [];
  225. }
  226. // userAgent
  227. //
  228. // Required by:
  229. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  230. // - lib-jitsi-meet/modules/RTC/RTCBrowserType.js
  231. (() => {
  232. const reactNativePackageJSON = require('react-native/package.json');
  233. let userAgent = reactNativePackageJSON.name || 'react-native';
  234. const version = reactNativePackageJSON.version;
  235. if (version) {
  236. userAgent += `/${version}`;
  237. }
  238. if (typeof navigator.userAgent !== 'undefined') {
  239. const s = navigator.userAgent.toString();
  240. if (s.length > 0 && s.indexOf(userAgent) === -1) {
  241. userAgent = `${s} ${userAgent}`;
  242. }
  243. }
  244. navigator.userAgent = userAgent;
  245. })();
  246. }
  247. // performance
  248. if (typeof global.performance === 'undefined') {
  249. global.performance = {
  250. now() {
  251. return 0;
  252. }
  253. };
  254. }
  255. // sessionStorage
  256. //
  257. // Required by:
  258. // - Strophe
  259. if (typeof global.sessionStorage === 'undefined') {
  260. global.sessionStorage = {
  261. /* eslint-disable no-empty-function */
  262. getItem() {},
  263. removeItem() {},
  264. setItem() {}
  265. /* eslint-enable no-empty-function */
  266. };
  267. }
  268. // WebRTC
  269. require('./polyfills-webrtc');
  270. // XMLHttpRequest
  271. if (global.XMLHttpRequest) {
  272. const prototype = global.XMLHttpRequest.prototype;
  273. // XMLHttpRequest.responseXML
  274. //
  275. // Required by:
  276. // - Strophe
  277. if (prototype && typeof prototype.responseXML === 'undefined') {
  278. Object.defineProperty(prototype, 'responseXML', {
  279. configurable: true,
  280. enumerable: true,
  281. get() {
  282. const responseText = this.responseText;
  283. let responseXML;
  284. if (responseText) {
  285. responseXML = new DOMParser()
  286. .parseFromString(responseText);
  287. }
  288. return responseXML;
  289. }
  290. });
  291. }
  292. }
  293. // Timers
  294. //
  295. // React Native's timers won't run while the app is in the background,
  296. // this is a known limitation. Replace them with a background-friendly
  297. // alternative.
  298. //
  299. // Required by:
  300. // - lib-jitsi-meet
  301. // - Strophe
  302. global.setTimeout = window.setTimeout = BackgroundTimer.setTimeout;
  303. global.clearTimeout = window.clearTimeout = BackgroundTimer.clearTimeout;
  304. global.setInterval = window.setInterval = BackgroundTimer.setInterval;
  305. global.clearInterval = window.clearInterval = BackgroundTimer.clearInterval;
  306. })(global || window || this); // eslint-disable-line no-invalid-this