You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

polyfills-browser.js 11KB

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