aboutsummaryrefslogtreecommitdiff
path: root/srs-anywhere-network.js
blob: 40291a800b88b2b7f2a4e116adc4d833fa23a96e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Copyright © 2025 - 2026 Jakob L. Kreuze

   This file is part of SRS Anywhere.

   SRS Anywhere is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   SRS Anywhere is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public
   License along with SRS Anywhere. If not, see <https://www.gnu.org/licenses/>. */

function enableInputs() {
  const target = document.querySelector('#cards-input');
  const el1 = document.createElement('input');
  el1.type = "text";
  el1.id = "network-password";
  const el2 = document.createElement('button');
  el2.id = "open-network";
  el2.innerText = "Go!";
  const el3 = document.createElement('br');
  target.before(el1, el2, el3);
}

function enableNetworkFeatures() {
  enableInputs();

  const passwordField = document.getElementById('network-password');

  const urlParams = new URLSearchParams(window.location.search);
  let password = localStorage.getItem('password') || urlParams.get('password');
  if (password) {
    passwordField.value = password;
  }

  const btn = document.getElementById('open-network');
  if (!btn) {
    console.warn('Button with id="open-network" not found.');
    return;
  }

  btn.addEventListener('click', function () {
    const password = passwordField.value;
    const xhr = new XMLHttpRequest();
    xhr.open('GET', '/cgi-bin/deck.py', true);
    xhr.setRequestHeader('PASSWORD', password);
    xhr.onreadystatechange = function () {
      if (xhr.readyState === XMLHttpRequest.DONE) {
        if (xhr.status >= 200 && xhr.status < 300) {
          localStorage.setItem('password', password);
          loadCards(JSON.parse(xhr.responseText));
        } else {
          reportException(xhr.statusText);
        }
      }
    };
    xhr.send();
  });
}

window.addEventListener("load", () => {
  if (SRS_ANYWHERE_CONFIG.network) {
    enableNetworkFeatures();
  }
});

const downloadJSONOld = downloadJSON;
function downloadJSON(obj) {
  try {
    const password = document.getElementById('network-password').value;
    const xhr = new XMLHttpRequest();
    xhr.open('POST', '/cgi-bin/deck.py', true);
    xhr.setRequestHeader('PASSWORD', password);
    xhr.onreadystatechange = function () {
      if (xhr.readyState === XMLHttpRequest.DONE) {
        if (xhr.status < 200 || xhr.status >= 300) {
          reportException(xhr.statusText);

          const jsonString = JSON.stringify(obj, null, 2);
          const blob = new Blob([jsonString], { type: 'application/json' });
          const url = URL.createObjectURL(blob);

          // Create a "ghost" anchor to trigger the download
          const a = document.createElement('a');
          a.href = url;
          a.download = 'data.json';

          // Append to body, click it, and remove it immediately
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);

          //  Clean up the URL object to free up memory
          URL.revokeObjectURL(url);
        }
      }
    };
    xhr.send(JSON.stringify(window.allCards, null, 2));
  } catch (e) {
    reportException(e);
    downloadJSONOld(obj);
  }
}

© 2015 - 2026 Jakob L. Kreuze