diff --git a/scripts/pi-hole/js/settings-teleporter.js b/scripts/pi-hole/js/settings-teleporter.js new file mode 100644 index 00000000..a1dbacee --- /dev/null +++ b/scripts/pi-hole/js/settings-teleporter.js @@ -0,0 +1,72 @@ +/* Pi-hole: A black hole for Internet advertisements + * (c) 2023 Pi-hole, LLC (https://pi-hole.net) + * Network-wide ad blocking via your own hardware. + * + * This file is copyright under the latest version of the EUPL. + * Please see LICENSE file for your rights under this license. */ + +/* global utils:false */ + +// Add event listener to import button +document.getElementById("submit-import").addEventListener("click", function () { + importZIP(); +}); + +// Upload file to Pi-hole +function importZIP() { + var file = document.getElementById("file").files[0]; + if (file === undefined) { + alert("Please select a file to import."); + return; + } + + // https://caniuse.com/fetch - everything except IE + // This is fine, as we dropped support for IE a while ago + if (typeof fetch !== "function") { + alert("Importing Tricorder files is not supported with this browser!"); + return; + } + + const formData = new FormData(); + formData.append("file", file); + // eslint-disable-next-line compat/compat + fetch("/api/teleporter", { + method: "POST", + body: formData, + }) + .then(response => response.json()) + .then(data => { + $("#import-spinner").hide(); + $("#modal-import-success").hide(); + $("#modal-import-error").hide(); + $("#modal-import-info").hide(); + + if ("error" in data) { + $("#modal-import-error").show(); + $("#modal-import-error-title").text("Error: " + data.error.message); + if (data.error.hint !== null) $("#modal-import-error-message").text(data.error.hint); + } else if ("files" in data) { + $("#modal-import-success").show(); + $("#modal-import-success-title").text("Import successful"); + var text = "
Processed files:
Warning: This archive contains sensitive information about your Pi-hole installation, e.g. the API token and the 2FA-TOTP secret (if enabled). Please be careful with this file and do not share it with anyone even if they claim to help you.
+ if not mg.request_info.https then mg.write("Warning: You are currently not using an end-to-end encryption. This means that your API token and 2FA-TOTP secret will be transmitted in plain text. We recommend to use HTTPS when exporting your configuration.
") end ?> +When importing settings from a newer version of Pi-hole, not yet existing settings will be ignored. When importing from an older version of Pi-hole, settings for newer features will be initialized with their default values.
+