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

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. // Polyfill for URL constructor
  74. require('url-polyfill');
  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. /* source */ '<html><head></head><body></body></html>',
  94. /* mineType */ '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. && typeof elementPrototype.querySelector === 'undefined') {
  123. elementPrototype.querySelector = function(selectors) {
  124. return _querySelector(this, selectors);
  125. };
  126. }
  127. // FIXME There is a weird infinite loop related to console.log and
  128. // Document and/or Element at the time of this writing. Work around it
  129. // by patching Node and/or overriding console.log.
  130. const nodePrototype
  131. = _getCommonPrototype(documentPrototype, elementPrototype);
  132. if (nodePrototype
  133. // XXX The intention was to find Node from which Document and
  134. // Element extend. If for whatever reason we've reached Object,
  135. // then it doesn't sound like what expected.
  136. && nodePrototype !== Object.getPrototypeOf({})) {
  137. // Override console.log.
  138. const console = global.console;
  139. if (console) {
  140. const loggerLevels = require('jitsi-meet-logger').levels;
  141. Object.keys(loggerLevels).forEach(key => {
  142. const level = loggerLevels[key];
  143. const consoleLog = console[level];
  144. /* eslint-disable prefer-rest-params */
  145. if (typeof consoleLog === 'function') {
  146. console[level] = function(...args) {
  147. const length = args.length;
  148. for (let i = 0; i < length; ++i) {
  149. let arg = args[i];
  150. if (arg
  151. && typeof arg !== 'string'
  152. // Limit the console.log override to
  153. // Node (instances).
  154. && nodePrototype.isPrototypeOf(arg)) {
  155. const toString = arg.toString;
  156. if (toString) {
  157. arg = toString.call(arg);
  158. }
  159. }
  160. args[i] = arg;
  161. }
  162. consoleLog.apply(this, args);
  163. };
  164. }
  165. /* eslint-enable prefer-rest-params */
  166. });
  167. }
  168. }
  169. global.document = document;
  170. }
  171. // location
  172. if (typeof global.location === 'undefined') {
  173. global.location = {
  174. href: ''
  175. };
  176. }
  177. const navigator = global.navigator;
  178. if (navigator) {
  179. // platform
  180. //
  181. // Required by:
  182. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  183. if (typeof navigator.platform === 'undefined') {
  184. navigator.platform = '';
  185. }
  186. // plugins
  187. //
  188. // Required by:
  189. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  190. if (typeof navigator.plugins === 'undefined') {
  191. navigator.plugins = [];
  192. }
  193. // userAgent
  194. //
  195. // Required by:
  196. // - lib-jitsi-meet/modules/RTC/adapter.screenshare.js
  197. // - lib-jitsi-meet/modules/RTC/RTCBrowserType.js
  198. (() => {
  199. const reactNativePackageJSON = require('react-native/package.json');
  200. let userAgent = reactNativePackageJSON.name || 'react-native';
  201. const version = reactNativePackageJSON.version;
  202. if (version) {
  203. userAgent += `/${version}`;
  204. }
  205. if (typeof navigator.userAgent !== 'undefined') {
  206. const s = navigator.userAgent.toString();
  207. if (s.length > 0 && s.indexOf(userAgent) === -1) {
  208. userAgent = `${s} ${userAgent}`;
  209. }
  210. }
  211. navigator.userAgent = userAgent;
  212. })();
  213. }
  214. // performance
  215. if (typeof global.performance === 'undefined') {
  216. global.performance = {
  217. now() {
  218. return 0;
  219. }
  220. };
  221. }
  222. // sessionStorage
  223. //
  224. // Required by:
  225. // - Strophe
  226. if (typeof global.sessionStorage === 'undefined') {
  227. global.sessionStorage = {
  228. /* eslint-disable no-empty-function */
  229. getItem() {},
  230. removeItem() {},
  231. setItem() {}
  232. /* eslint-enable no-empty-function */
  233. };
  234. }
  235. // WebRTC
  236. require('./polyfills-webrtc');
  237. // XMLHttpRequest
  238. if (global.XMLHttpRequest) {
  239. const prototype = global.XMLHttpRequest.prototype;
  240. // XMLHttpRequest.responseXML
  241. //
  242. // Required by:
  243. // - Strophe
  244. if (prototype && typeof prototype.responseXML === 'undefined') {
  245. Object.defineProperty(prototype, 'responseXML', {
  246. configurable: true,
  247. enumerable: true,
  248. get() {
  249. const responseText = this.responseText;
  250. let responseXML;
  251. if (responseText) {
  252. responseXML = new DOMParser()
  253. .parseFromString(responseText);
  254. }
  255. return responseXML;
  256. }
  257. });
  258. }
  259. }
  260. })(global || window || this); // eslint-disable-line no-invalid-this