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 9.4KB

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