tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

app.js (1753B)


      1 (function () {
      2  const messages = document.querySelector('#messages');
      3  const wsButton = document.querySelector('#wsButton');
      4  const wsSendButton = document.querySelector('#wsSendButton');
      5  const logout = document.querySelector('#logout');
      6  const login = document.querySelector('#login');
      7 
      8  function showMessage(message) {
      9    messages.textContent += `\n${message}`;
     10    messages.scrollTop = messages.scrollHeight;
     11  }
     12 
     13  function handleResponse(response) {
     14    return response.ok
     15      ? response.json().then((data) => JSON.stringify(data, null, 2))
     16      : Promise.reject(new Error('Unexpected response'));
     17  }
     18 
     19  login.onclick = function () {
     20    fetch('/login', { method: 'POST', credentials: 'same-origin' })
     21      .then(handleResponse)
     22      .then(showMessage)
     23      .catch(function (err) {
     24        showMessage(err.message);
     25      });
     26  };
     27 
     28  logout.onclick = function () {
     29    fetch('/logout', { method: 'DELETE', credentials: 'same-origin' })
     30      .then(handleResponse)
     31      .then(showMessage)
     32      .catch(function (err) {
     33        showMessage(err.message);
     34      });
     35  };
     36 
     37  let ws;
     38 
     39  wsButton.onclick = function () {
     40    if (ws) {
     41      ws.onerror = ws.onopen = ws.onclose = null;
     42      ws.close();
     43    }
     44 
     45    ws = new WebSocket(`ws://${location.host}`);
     46    ws.onerror = function () {
     47      showMessage('WebSocket error');
     48    };
     49    ws.onopen = function () {
     50      showMessage('WebSocket connection established');
     51    };
     52    ws.onclose = function () {
     53      showMessage('WebSocket connection closed');
     54      ws = null;
     55    };
     56  };
     57 
     58  wsSendButton.onclick = function () {
     59    if (!ws) {
     60      showMessage('No WebSocket connection');
     61      return;
     62    }
     63 
     64    ws.send('Hello World!');
     65    showMessage('Sent "Hello World!"');
     66  };
     67 })();