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.

functions.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* @flow */
  2. declare var config: Object;
  3. /**
  4. * Builds and returns the room name.
  5. *
  6. * @returns {string}
  7. */
  8. export function getRoomName(): ?string {
  9. const { getroomnode } = config;
  10. const path = window.location.pathname;
  11. let roomName;
  12. // Determine the room node from the URL.
  13. if (getroomnode && typeof getroomnode === 'function') {
  14. roomName = getroomnode.call(config, path);
  15. } else {
  16. // Fall back to the default strategy of making assumptions about how the
  17. // URL maps to the room (name). It currently assumes a deployment in
  18. // which the last non-directory component of the path (name) is the
  19. // room.
  20. roomName
  21. = path.substring(path.lastIndexOf('/') + 1).toLowerCase()
  22. || undefined;
  23. }
  24. return roomName;
  25. }
  26. /**
  27. * Parses the parameters from the URL and returns them as a JS object.
  28. *
  29. * @param {string} url - URL to parse.
  30. * @param {boolean} dontParse - If false or undefined some transformations
  31. * (for parsing the value as JSON) are going to be executed.
  32. * @param {string} source - Values - "hash"/"search" if "search" the parameters
  33. * will parsed from location.search otherwise from location.hash.
  34. * @returns {Object}
  35. */
  36. export function parseURLParams(
  37. url: URL,
  38. dontParse: boolean = false,
  39. source: string = 'hash'): Object {
  40. const paramStr = source === 'search' ? url.search : url.hash;
  41. const params = {};
  42. // eslint-disable-next-line newline-per-chained-call
  43. paramStr && paramStr.substr(1).split('&').forEach(part => {
  44. const param = part.split('=');
  45. let value;
  46. try {
  47. value = param[1];
  48. if (!dontParse) {
  49. value
  50. = JSON.parse(
  51. decodeURIComponent(param[1]).replace(/\\&/, '&'));
  52. }
  53. } catch (e) {
  54. const msg = `Failed to parse URL parameter value: ${String(value)}`;
  55. console.warn(msg, e);
  56. window.onerror && window.onerror(msg, null, null, null, e);
  57. return;
  58. }
  59. params[param[0]] = value;
  60. });
  61. return params;
  62. }