/* Copyright © 2025 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 . */ window.addEventListener("load", () => { 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(); }); }); const downloadJSONOld = downloadJSON; function downloadJSON(obj) { 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); } } }; xhr.send(JSON.stringify(window.allCards, null, 2)); const inputButton = document.getElementById('cards-input'); const saveButton = document.createElement('button'); saveButton.textContent = "Save JSON"; saveButton.addEventListener('click', () => { // We move the logic inside the click listener so that it // always saves the CURRENT state of 'obj' if it has changed. 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); }); inputButton.parentNode.insertBefore(saveButton, inputButton); }