Skip to content

lightweight, client-only calendar web application. All data persists in the URL hash for instant sharing, No backend required. Optional AES-GCM password protection keeps shared links locked without a server

JavaScript

JSON modal + hash cleanup

Supun Lakmalf92458c92d2d
diff --git a/index.html b/index.html
index 1a1fdcf..206aefe 100644
--- a/index.html
+++ b/index.html
@@ -109,11 +109,12 @@
<strong id="url-length">0</strong>
</div>
<p id="url-warning" class="url-warning"></p>
- <div class="stacked-actions">
- <button id="export-json" type="button" class="ghost">Export JSON</button>
- <button id="import-ics" type="button" class="ghost">Import .ics</button>
- <input id="ics-input" type="file" accept=".ics,text/calendar" hidden>
- </div>
+ <div class="stacked-actions">
+ <button id="view-json" type="button" class="ghost">View JSON</button>
+ <button id="export-json" type="button" class="ghost">Export JSON</button>
+ <button id="import-ics" type="button" class="ghost">Import .ics</button>
+ <input id="ics-input" type="file" accept=".ics,text/calendar" hidden>
+ </div>
</div>
<div class="side-card danger">
@@ -182,9 +183,9 @@
</div>
</div>
- <div id="password-modal" class="modal hidden" role="dialog" aria-modal="true" aria-labelledby="password-title">
- <div class="modal-backdrop"></div>
- <div class="modal-card">
+ <div id="password-modal" class="modal hidden" role="dialog" aria-modal="true" aria-labelledby="password-title">
+ <div class="modal-backdrop"></div>
+ <div class="modal-card">
<header class="modal-header">
<h3 id="password-title">Unlock calendar</h3>
<button id="password-close" type="button" class="icon-btn" aria-label="Close">x</button>
@@ -205,10 +206,37 @@
<button id="password-submit" type="button" class="primary">Unlock</button>
</div>
</div>
- </div>
- </div>
-
- <div id="toast-container" class="toast-container" aria-live="polite" aria-atomic="true"></div>
+ </div>
+ </div>
+
+ <div id="json-modal" class="modal hidden" role="dialog" aria-modal="true" aria-labelledby="json-modal-title">
+ <div class="modal-backdrop"></div>
+ <div class="modal-card json-modal-card">
+ <header class="modal-header">
+ <h3 id="json-modal-title">Share data</h3>
+ <button id="json-close" type="button" class="icon-btn" aria-label="Close">x</button>
+ </header>
+ <div class="modal-form">
+ <label class="field">
+ <span>URL hash</span>
+ <input id="json-hash" type="text" readonly>
+ </label>
+ <label class="field">
+ <span>JSON data</span>
+ <textarea id="json-output" class="json-output" rows="10" readonly></textarea>
+ </label>
+ <div class="modal-actions">
+ <button id="json-copy-hash" type="button" class="ghost">Copy hash</button>
+ <div class="modal-actions-right">
+ <button id="json-copy" type="button" class="ghost">Copy JSON</button>
+ <button id="json-download" type="button" class="primary">Download JSON</button>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <div id="toast-container" class="toast-container" aria-live="polite" aria-atomic="true"></div>
<script src="modules/lz-string.min.js"></script>
<script type="module" src="script.js"></script>
diff --git a/modules/hashcalUrlManager.js b/modules/hashcalUrlManager.js
index e7d4bf4..778c69a 100644
--- a/modules/hashcalUrlManager.js
+++ b/modules/hashcalUrlManager.js
@@ -2,6 +2,54 @@ import { decryptString, encryptString } from "./cryptoManager.js";
const HASH_PREFIX = "ENC:";
+const DEFAULT_TITLE = "hash-calendar";
+const DEFAULT_COLORS = ["#ff6b6b", "#ffd43b", "#4dabf7", "#63e6be", "#9775fa"];
+const DEFAULT_SETTINGS = { d: 0, m: 0 };
+
+function arraysEqual(a, b) {
+ return a.length === b.length && a.every((v, i) => v === b[i]);
+}
+
+function compactState(state) {
+ const out = {};
+
+ if (state.t && state.t !== DEFAULT_TITLE) {
+ out.t = state.t;
+ }
+
+ if (Array.isArray(state.c) && !arraysEqual(state.c, DEFAULT_COLORS)) {
+ const diff = {};
+ state.c.forEach((c, i) => {
+ if (c !== DEFAULT_COLORS[i]) {
+ diff[i] = c.startsWith("#") ? c.slice(1) : c;
+ }
+ });
+ if (Object.keys(diff).length) {
+ out.c = diff;
+ }
+ }
+
+ if (Array.isArray(state.e) && state.e.length) {
+ out.e = state.e.map((ev) => {
+ const arr = [ev[0], ev[1], ev[2]];
+ const rule = ev[4] || "";
+ const colorIdx = ev[3] || 0;
+ if (rule) {
+ arr.push(colorIdx, rule);
+ } else if (colorIdx !== 0) {
+ arr.push(colorIdx);
+ }
+ return arr;
+ });
+ }
+
+ if (state.s && (state.s.d !== 0 || state.s.m !== 0)) {
+ out.s = state.s;
+ }
+
+ return out;
+}
+
function ensureLzString() {
if (!window.LZString) {
throw new Error("LZString missing");
@@ -39,7 +87,7 @@ export async function readStateFromHash(password = null) {
export async function writeStateToHash(state, password = null) {
const lz = ensureLzString();
- const json = JSON.stringify(state);
+ const json = JSON.stringify(compactState(state));
const compressed = lz.compressToEncodedURIComponent(json);
let nextHash = compressed;
diff --git a/script.js b/script.js
index 74b87e5..e20edd2 100644
--- a/script.js
+++ b/script.js
@@ -1,5 +1,5 @@
-import { readStateFromHash, writeStateToHash, isEncryptedHash } from "./modules/hashcalUrlManager.js";
-import { expandEvents } from "./modules/recurrenceEngine.js";
+import { readStateFromHash, writeStateToHash, isEncryptedHash, clearHash } from "./modules/hashcalUrlManager.js";
+import { expandEvents } from "./modules/recurrenceEngine.js";
import {
formatDateKey,
getMonthGridRange,
@@ -8,12 +8,12 @@ import {
renderTimeGrid,
renderWeekdayHeaders,
renderYearView,
-} from "./modules/calendarRender.js";
-import { parseIcs } from "./modules/icsImporter.js";
+} from "./modules/calendarRender.js";
+import { parseIcs } from "./modules/icsImporter.js";
const DEFAULT_COLORS = ["#ff6b6b", "#ffd43b", "#4dabf7", "#63e6be", "#9775fa"];
const DEFAULT_STATE = {
- t: "hash-calendar",
+ t: "hash-calendar",
c: DEFAULT_COLORS,
e: [],
s: {
@@ -101,14 +101,24 @@ function normalizeState(raw) {
next.t = raw.t.slice(0, MAX_TITLE_LENGTH);
}
- if (Array.isArray(raw.c) && raw.c.length) {
- next.c = raw.c.filter((color) => typeof color === "string" && /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(color));
+ if (raw.c && typeof raw.c === "object" && !Array.isArray(raw.c)) {
+ next.c = DEFAULT_COLORS.slice();
+ for (const [i, color] of Object.entries(raw.c)) {
+ const idx = Number(i);
+ if (idx >= 0 && idx < next.c.length && typeof color === "string" && /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(color)) {
+ next.c[idx] = color.startsWith("#") ? color : `#${color}`;
+ }
+ }
+ } else if (Array.isArray(raw.c) && raw.c.length) {
+ next.c = raw.c
+ .filter((color) => typeof color === "string" && /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(color))
+ .map((color) => (color.startsWith("#") ? color : `#${color}`));
if (!next.c.length) next.c = DEFAULT_COLORS.slice();
}
if (Array.isArray(raw.e)) {
next.e = raw.e
- .filter((entry) => Array.isArray(entry) && entry.length >= 4)
+ .filter((entry) => Array.isArray(entry) && entry.length >= 3)
.map((entry) => {
const startMin = Number(entry[0]);
const duration = Math.max(0, Number(entry[1]) || 0);
@@ -149,6 +159,7 @@ function cacheElements() {
ui.addEventInline = document.getElementById("add-event-inline");
ui.urlLength = document.getElementById("url-length");
ui.urlWarning = document.getElementById("url-warning");
+ ui.viewJson = document.getElementById("view-json");
ui.exportJson = document.getElementById("export-json");
ui.importIcs = document.getElementById("import-ics");
ui.icsInput = document.getElementById("ics-input");
@@ -182,6 +193,14 @@ function cacheElements() {
ui.passwordCancel = document.getElementById("password-cancel");
ui.passwordSubmit = document.getElementById("password-submit");
+ ui.jsonModal = document.getElementById("json-modal");
+ ui.jsonClose = document.getElementById("json-close");
+ ui.jsonHash = document.getElementById("json-hash");
+ ui.jsonOutput = document.getElementById("json-output");
+ ui.jsonCopy = document.getElementById("json-copy");
+ ui.jsonCopyHash = document.getElementById("json-copy-hash");
+ ui.jsonDownload = document.getElementById("json-download");
+
ui.toastContainer = document.getElementById("toast-container");
}
@@ -198,6 +217,14 @@ function showToast(message, type = "info") {
function scheduleSave() {
if (lockState.encrypted && !lockState.unlocked) return;
+ if (!state.e.length) {
+ if (window.location.hash) clearHash();
+ updateUrlLength();
+ if (ui.jsonModal && !ui.jsonModal.classList.contains("hidden")) {
+ updateJsonModal();
+ }
+ return;
+ }
if (saveTimer) window.clearTimeout(saveTimer);
saveTimer = window.setTimeout(async () => {
await writeStateToHash(state, password);
@@ -379,6 +406,9 @@ function render() {
renderEventList();
updateUrlLength();
+ if (ui.jsonModal && !ui.jsonModal.classList.contains("hidden")) {
+ updateJsonModal();
+ }
updateLockUI();
}
@@ -754,6 +784,46 @@ async function handleCopyLink() {
}
}
+function updateJsonModal() {
+ if (ui.jsonOutput) {
+ ui.jsonOutput.value = JSON.stringify(state, null, 2);
+ }
+ if (ui.jsonHash) {
+ ui.jsonHash.value = window.location.hash || "";
+ }
+}
+
+function openJsonModal() {
+ if (!ui.jsonModal) return;
+ updateJsonModal();
+ ui.jsonModal.classList.remove("hidden");
+}
+
+function closeJsonModal() {
+ if (!ui.jsonModal) return;
+ ui.jsonModal.classList.add("hidden");
+}
+
+async function handleCopyJson() {
+ if (!ui.jsonOutput) return;
+ try {
+ await navigator.clipboard.writeText(ui.jsonOutput.value);
+ showToast("JSON copied", "success");
+ } catch (error) {
+ showToast("Unable to copy JSON", "error");
+ }
+}
+
+async function handleCopyHash() {
+ if (!ui.jsonHash) return;
+ try {
+ await navigator.clipboard.writeText(ui.jsonHash.value);
+ showToast("Hash copied", "success");
+ } catch (error) {
+ showToast("Unable to copy hash", "error");
+ }
+}
+
function handleExportJson() {
const blob = new Blob([JSON.stringify(state, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
@@ -827,6 +897,7 @@ function bindEvents() {
if (ui.weekstartToggle) ui.weekstartToggle.addEventListener("click", handleWeekStartToggle);
if (ui.themeToggle) ui.themeToggle.addEventListener("click", handleThemeToggle);
if (ui.unlockBtn) ui.unlockBtn.addEventListener("click", attemptUnlock);
+ if (ui.viewJson) ui.viewJson.addEventListener("click", openJsonModal);
if (ui.exportJson) ui.exportJson.addEventListener("click", handleExportJson);
if (ui.importIcs) ui.importIcs.addEventListener("click", handleImportIcsClick);
if (ui.icsInput) ui.icsInput.addEventListener("change", handleIcsFile);
@@ -841,6 +912,10 @@ function bindEvents() {
if (ui.passwordClose) ui.passwordClose.addEventListener("click", closePasswordModal);
if (ui.passwordCancel) ui.passwordCancel.addEventListener("click", closePasswordModal);
if (ui.passwordSubmit) ui.passwordSubmit.addEventListener("click", submitPassword);
+ if (ui.jsonClose) ui.jsonClose.addEventListener("click", closeJsonModal);
+ if (ui.jsonCopy) ui.jsonCopy.addEventListener("click", handleCopyJson);
+ if (ui.jsonCopyHash) ui.jsonCopyHash.addEventListener("click", handleCopyHash);
+ if (ui.jsonDownload) ui.jsonDownload.addEventListener("click", handleExportJson);
window.addEventListener("hashchange", handleHashChange);
}
@@ -849,9 +924,8 @@ async function init() {
cacheElements();
bindEvents();
await loadStateFromHash();
-
- if (!window.location.hash) {
- await writeStateToHash(state, null);
+ if (!isEncryptedHash() && !state.e.length && window.location.hash) {
+ clearHash();
}
updateViewButtons();
diff --git a/styles.css b/styles.css
index 524aa82..ce71f37 100644
--- a/styles.css
+++ b/styles.css
@@ -600,15 +600,19 @@ button:disabled {
background: rgba(32, 33, 36, 0.6);
}
-.modal-card {
- position: relative;
- background: var(--panel);
- border: 1px solid var(--border);
- border-radius: 12px;
- padding: 20px;
- width: min(520px, 92vw);
- box-shadow: var(--shadow-md);
-}
+.modal-card {
+ position: relative;
+ background: var(--panel);
+ border: 1px solid var(--border);
+ border-radius: 12px;
+ padding: 20px;
+ width: min(520px, 92vw);
+ box-shadow: var(--shadow-md);
+}
+
+.json-modal-card {
+ width: min(720px, 94vw);
+}
.modal-header {
display: flex;
@@ -625,11 +629,18 @@ button:disabled {
padding: 0;
}
-.modal-form {
- display: flex;
- flex-direction: column;
- gap: 12px;
-}
+.modal-form {
+ display: flex;
+ flex-direction: column;
+ gap: 12px;
+}
+
+.json-output {
+ min-height: 220px;
+ font-family: "SFMono-Regular", "Menlo", "Consolas", "Liberation Mono", monospace;
+ resize: vertical;
+ white-space: pre;
+}
.field {
display: flex;
@@ -639,21 +650,23 @@ button:disabled {
color: var(--muted);
}
-.field input,
-.field select {
- padding: 8px 10px;
- border-radius: 6px;
- border: 1px solid var(--border);
- background: var(--panel-2);
- color: var(--text);
- font-size: 0.85rem;
-}
-
-.field input:focus,
-.field select:focus {
- outline: 2px solid var(--accent-soft);
- border-color: var(--accent);
-}
+.field input,
+.field select,
+.field textarea {
+ padding: 8px 10px;
+ border-radius: 6px;
+ border: 1px solid var(--border);
+ background: var(--panel-2);
+ color: var(--text);
+ font-size: 0.85rem;
+}
+
+.field input:focus,
+.field select:focus,
+.field textarea:focus {
+ outline: 2px solid var(--accent-soft);
+ border-color: var(--accent);
+}
.field-row {
display: grid;