123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- (function() {
- 'use strict';
- var initialized = false;
- var i18n;
- // i18n key prefix for MUC ("muc.") or 1:1 chat ("chat.")
- var key_prefix;
- var display_data = null;
- function show_clients(client_array) {
- var list = document.getElementById('client_list');
- for (var id = 0; id < client_array.length; id++) {
- var item = document.createElement('div');
- /* innerHTML needed for client links */
- item.innerHTML = client_array[id];
- list.appendChild(item);
- }
- // ensure that the client links are updated with the referrer
- rehash();
- }
- function hash_clients(referrer) {
- var list = document.getElementById('client_list').getElementsByTagName('a');
- if (referrer) {
- console.log(referrer);
- for (var i = 0; i < list.length; i++) {
- var a = list[i];
- console.log(a);
- if (a.href && a.href.startsWith('https://play.google.com/store/apps/details')) {
- a.href = a.href.replace(/\&referrer=[^&]/, '') + '&referrer=' + referrer;
- console.log(a);
- }
- }
- }
- }
- function load_clients(url) {
- var request = new XMLHttpRequest();
- request.open('GET', url);
- request.onreadystatechange = function () {
- if (request.readyState === 4) {
- if (request.status === 200 || (isLocalFileRequest(url) && request.responseText.length > 0)) {
- show_clients(JSON.parse(request.responseText));
- }
- }
- };
- request.send(null);
- }
- function load_hash() {
- var muc = false;
- key_prefix = "chat.";
- var xmpp_uri = window.location.search || window.location.hash;
- xmpp_uri = decodeURIComponent(xmpp_uri.substring(xmpp_uri.indexOf('#') + 1, xmpp_uri.length));
- if (xmpp_uri.indexOf("xmpp:") === 0) {
- xmpp_uri = xmpp_uri.slice(5);
- }
- try {
- base_decoded = window.atob(xmpp_uri);
- if (base_decoded.search('@') >= 0)
- xmpp_uri = base_decoded;
- } catch (err) {
- // ignore error, JID wasn't base64 encoded
- }
- if (xmpp_uri.search("\\?join") >= 0) {
- muc = true;
- key_prefix = "muc.";
- }
- // TODO: proper error checking / display / Creation of invitations
- if (xmpp_uri.search("@") <= 0) return {xmpp_uri:xmpp_uri, xmpp_uri_encoded:xmpp_uri, name: xmpp_uri};
- var xmpp_params = {};
- var xmpp_uri_parts = xmpp_uri.split("?");
- if (xmpp_uri_parts.length > 1) {
- let parameter, parameters = xmpp_uri_parts[1].split(";")
- for (parameter of parameters) {
- let key_value = parameter.split("=")
- xmpp_params[key_value[0]] = key_value.length > 1 ? key_value[1] : "";
- }
- }
- const jid_parts = xmpp_uri_parts[0].split("@");
- const local_part = jid_parts[0];
- xmpp_params["name"] = local_part.charAt(0).toUpperCase() + local_part.slice(1);
- const domain_part = jid_parts[1];
- const jid_encoded = encodeURIComponent(local_part) + "@" + encodeURIComponent(domain_part)
- xmpp_uri_parts[0] = jid_encoded
- const xmpp_uri_encoded = xmpp_uri_parts.join("?")
- return {xmpp_uri: xmpp_uri, xmpp_uri_encoded: xmpp_uri_encoded, name: xmpp_params["name"]};
- }
- function translate_ui() {
- // translation
- document.title = i18n.text(key_prefix + 'title', display_data);
- // MUC/chat specific
- ['heading', 'button'].forEach(function(id) {
- document.getElementById(id).innerText = i18n.text(key_prefix + id, display_data);
- });
- // and agnostic
- ['clients', 'recommend', 'checkfulllist', 'xmppis'].forEach(function(id) {
- /* innerHTML needed to display links and markup from translation */
- document.getElementById(id).innerHTML = i18n.text(id, {});
- });
- }
- function rehash() {
- display_data = load_hash();
- console.log(display_data);
- document.getElementById('button').href = "xmpp:" + display_data.xmpp_uri_encoded;
- document.getElementById('url_in').value = "xmpp:" + display_data.xmpp_uri;
- hash_clients("xmpp:" + display_data.xmpp_uri_encoded);
- translate_ui();
- }
- function createQR() {
- display_data = load_hash();
- new QRCode(document.getElementById("qrcode"), "xmpp:" + display_data.xmpp_uri_encoded);
- }
- function load_done() {
- if (initialized) return;
- initialized = true;
- // load i18n and perform translation
- i18n = new I18nText({path: 'lang'});
- i18n.once(I18nText.event.LOCALE_CHANGE, function (data) {
- rehash();
- });
- var preferredLocale, setLocale = false;
- for (preferredLocale of navigator.languages) {
- if (supportedLocales.includes(preferredLocale)) {
- i18n.setLocale(preferredLocale);
- setLocale = true;
- break;
- }
- }
- if (!setLocale) {
- i18n.setLocale(defaultLocale);
- }
- var rtlLangs = "ar, fa, he, ur"
- if (rtlLangs.includes(navigator.language)) {
- document.querySelector("body").dir = "rtl";
- }
- // functionality
- var ua = navigator.userAgent;
- switch (true) {
- case (ua.indexOf("Windows") >= 0):
- load_clients("clients_Windows.json")
- break;
- case (ua.indexOf("Android") >= 0):
- case (ua.indexOf("CrOS") >= 0):
- load_clients("clients_Android.json")
- createQR();
- break;
- case (ua.indexOf("iPad") >= 0):
- case (ua.indexOf("iPhone") >= 0):
- load_clients("clients_iOS.json")
- createQR();
- break;
- case (ua.indexOf("Mac OS X") >= 0):
- case (ua.indexOf("Macintosh") >= 0):
- load_clients("clients_OSX.json")
- break;
- case (ua.indexOf("Tizen") >= 0):
- load_clients("clients_Tizen.json")
- createQR();
- break;
- // just default
- case (true):
- case (ua.indexOf("Linux") >= 0):
- load_clients("clients_Linux.json");
- createQR();
- break;
- }
- window.addEventListener("hashchange", rehash, false);
- document.getElementById("url_in").addEventListener("focus", function(event) {
- event.target.select();
- });
- }
- // Wait for the DOM to be ready
- document.addEventListener('DOMContentLoaded', load_done, false);
- document.onreadystatechange = function() {
- if (document.readyState === 'interactive') {
- load_done();
- }
- };
-
- var logo = document.createElement('img');
- logo.src = 'assets/xmpp.svg';
- logo.alt= 'XMPP logo';
- logo.width = 60;
-
- var link = document.createElement('a');
- link.href = 'https://xmpp.org/';
- link.append(logo)
-
- var brand = document.getElementById('xmpp');
- brand.append(link)
- })();
|