main.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. (function() {
  2. 'use strict';
  3. var initialized = false;
  4. var i18n;
  5. // i18n key prefix for MUC ("muc.") or 1:1 chat ("chat.")
  6. var key_prefix;
  7. var display_data = null;
  8. function show_clients(client_array) {
  9. var list = document.getElementById('client_list');
  10. for (var id = 0; id < client_array.length; id++) {
  11. var item = document.createElement('div');
  12. /* innerHTML needed for client links */
  13. item.innerHTML = client_array[id];
  14. list.appendChild(item);
  15. }
  16. // ensure that the client links are updated with the referrer
  17. rehash();
  18. }
  19. function hash_clients(referrer) {
  20. var list = document.getElementById('client_list').getElementsByTagName('a');
  21. if (referrer) {
  22. console.log(referrer);
  23. for (var i = 0; i < list.length; i++) {
  24. var a = list[i];
  25. console.log(a);
  26. if (a.href && a.href.startsWith('https://play.google.com/store/apps/details')) {
  27. a.href = a.href.replace(/\&referrer=[^&]/, '') + '&referrer=' + referrer;
  28. console.log(a);
  29. }
  30. }
  31. }
  32. }
  33. function load_clients(url) {
  34. var request = new XMLHttpRequest();
  35. request.open('GET', url);
  36. request.onreadystatechange = function () {
  37. if (request.readyState === 4) {
  38. if (request.status === 200 || (isLocalFileRequest(url) && request.responseText.length > 0)) {
  39. show_clients(JSON.parse(request.responseText));
  40. }
  41. }
  42. };
  43. request.send(null);
  44. }
  45. function load_hash() {
  46. var muc = false;
  47. key_prefix = "chat.";
  48. var xmpp_uri = window.location.search || window.location.hash;
  49. xmpp_uri = decodeURIComponent(xmpp_uri.substring(xmpp_uri.indexOf('#') + 1, xmpp_uri.length));
  50. if (xmpp_uri.indexOf("xmpp:") === 0) {
  51. xmpp_uri = xmpp_uri.slice(5);
  52. }
  53. try {
  54. base_decoded = window.atob(xmpp_uri);
  55. if (base_decoded.search('@') >= 0)
  56. xmpp_uri = base_decoded;
  57. } catch (err) {
  58. // ignore error, JID wasn't base64 encoded
  59. }
  60. if (xmpp_uri.search("\\?join") >= 0) {
  61. muc = true;
  62. key_prefix = "muc.";
  63. }
  64. // TODO: proper error checking / display / Creation of invitations
  65. if (xmpp_uri.search("@") <= 0) return {xmpp_uri:xmpp_uri, xmpp_uri_encoded:xmpp_uri, name: xmpp_uri};
  66. var xmpp_params = {};
  67. var xmpp_uri_parts = xmpp_uri.split("?");
  68. if (xmpp_uri_parts.length > 1) {
  69. let parameter, parameters = xmpp_uri_parts[1].split(";")
  70. for (parameter of parameters) {
  71. let key_value = parameter.split("=")
  72. xmpp_params[key_value[0]] = key_value.length > 1 ? key_value[1] : "";
  73. }
  74. }
  75. const jid_parts = xmpp_uri_parts[0].split("@");
  76. const local_part = jid_parts[0];
  77. xmpp_params["name"] = local_part.charAt(0).toUpperCase() + local_part.slice(1);
  78. const domain_part = jid_parts[1];
  79. const jid_encoded = encodeURIComponent(local_part) + "@" + encodeURIComponent(domain_part)
  80. xmpp_uri_parts[0] = jid_encoded
  81. const xmpp_uri_encoded = xmpp_uri_parts.join("?")
  82. return {xmpp_uri: xmpp_uri, xmpp_uri_encoded: xmpp_uri_encoded, name: xmpp_params["name"]};
  83. }
  84. function translate_ui() {
  85. // translation
  86. document.title = i18n.text(key_prefix + 'title', display_data);
  87. // MUC/chat specific
  88. ['heading', 'button'].forEach(function(id) {
  89. document.getElementById(id).innerText = i18n.text(key_prefix + id, display_data);
  90. });
  91. // and agnostic
  92. ['clients', 'recommend', 'checkfulllist', 'xmppis'].forEach(function(id) {
  93. /* innerHTML needed to display links and markup from translation */
  94. document.getElementById(id).innerHTML = i18n.text(id, {});
  95. });
  96. }
  97. function rehash() {
  98. display_data = load_hash();
  99. console.log(display_data);
  100. document.getElementById('button').href = "xmpp:" + display_data.xmpp_uri_encoded;
  101. document.getElementById('url_in').value = "xmpp:" + display_data.xmpp_uri;
  102. hash_clients("xmpp:" + display_data.xmpp_uri_encoded);
  103. translate_ui();
  104. }
  105. function createQR() {
  106. display_data = load_hash();
  107. new QRCode(document.getElementById("qrcode"), "xmpp:" + display_data.xmpp_uri_encoded);
  108. }
  109. function load_done() {
  110. if (initialized) return;
  111. initialized = true;
  112. // load i18n and perform translation
  113. i18n = new I18nText({path: 'lang'});
  114. i18n.once(I18nText.event.LOCALE_CHANGE, function (data) {
  115. rehash();
  116. });
  117. var preferredLocale, setLocale = false;
  118. for (preferredLocale of navigator.languages) {
  119. if (supportedLocales.includes(preferredLocale)) {
  120. i18n.setLocale(preferredLocale);
  121. setLocale = true;
  122. break;
  123. }
  124. }
  125. if (!setLocale) {
  126. i18n.setLocale(defaultLocale);
  127. }
  128. var rtlLangs = "ar, fa, he, ur"
  129. if (rtlLangs.includes(navigator.language)) {
  130. document.querySelector("body").dir = "rtl";
  131. }
  132. // functionality
  133. var ua = navigator.userAgent;
  134. switch (true) {
  135. case (ua.indexOf("Windows") >= 0):
  136. load_clients("clients_Windows.json")
  137. break;
  138. case (ua.indexOf("Android") >= 0):
  139. case (ua.indexOf("CrOS") >= 0):
  140. load_clients("clients_Android.json")
  141. createQR();
  142. break;
  143. case (ua.indexOf("iPad") >= 0):
  144. case (ua.indexOf("iPhone") >= 0):
  145. load_clients("clients_iOS.json")
  146. createQR();
  147. break;
  148. case (ua.indexOf("Mac OS X") >= 0):
  149. case (ua.indexOf("Macintosh") >= 0):
  150. load_clients("clients_OSX.json")
  151. break;
  152. case (ua.indexOf("Tizen") >= 0):
  153. load_clients("clients_Tizen.json")
  154. createQR();
  155. break;
  156. // just default
  157. case (true):
  158. case (ua.indexOf("Linux") >= 0):
  159. load_clients("clients_Linux.json");
  160. createQR();
  161. break;
  162. }
  163. window.addEventListener("hashchange", rehash, false);
  164. document.getElementById("url_in").addEventListener("focus", function(event) {
  165. event.target.select();
  166. });
  167. }
  168. // Wait for the DOM to be ready
  169. document.addEventListener('DOMContentLoaded', load_done, false);
  170. document.onreadystatechange = function() {
  171. if (document.readyState === 'interactive') {
  172. load_done();
  173. }
  174. };
  175. var logo = document.createElement('img');
  176. logo.src = 'assets/xmpp.svg';
  177. logo.alt= 'XMPP logo';
  178. logo.width = 60;
  179. var link = document.createElement('a');
  180. link.href = 'https://xmpp.org/';
  181. link.append(logo)
  182. var brand = document.getElementById('xmpp');
  183. brand.append(link)
  184. })();