-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
291 lines (247 loc) · 7.06 KB
/
app.js
File metadata and controls
291 lines (247 loc) · 7.06 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const notesList = document.getElementById("notesList");
const fab = document.getElementById("fab");
const editor = document.getElementById("editor");
const textInput = document.getElementById("textInput");
const unlockTimeInput = document.getElementById("unlockTime");
const lockBtn = document.getElementById("lockBtn");
const encoder = new TextEncoder();
const decoder = new TextDecoder();
let confirmStep = false;
/* ---------- MODAL ---------- */
function openModal() {
editor.classList.add("show");
document.body.classList.add("modal-open");
textInput.focus();
}
function closeModal() {
editor.classList.remove("show");
document.body.classList.remove("modal-open");
// Reset confirm state safely
confirmStep = false;
lockBtn.textContent = "Lock Note";
// Clear inputs
textInput.value = "";
unlockTimeInput.value = "";
}
fab.onclick = openModal;
editor.onclick = e => {
if (e.target === editor) closeModal();
};
// ESC key to close modal
document.addEventListener("keydown", e => {
if (e.key === "Escape" && editor.classList.contains("show")) {
closeModal();
}
});
/* ---------- CRYPTO ---------- */
async function deriveKey(ts) {
const base = await crypto.subtle.importKey(
"raw",
encoder.encode(String(ts)),
"PBKDF2",
false,
["deriveKey"]
);
return crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: encoder.encode("text-lock"),
iterations: 100000,
hash: "SHA-256"
},
base,
{ name: "AES-GCM", length: 256 },
false,
["encrypt", "decrypt"]
);
}
async function encrypt(text, ts) {
const iv = crypto.getRandomValues(new Uint8Array(12));
const key = await deriveKey(ts);
const encrypted = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv },
key,
encoder.encode(text)
);
return { iv: [...iv], data: [...new Uint8Array(encrypted)] };
}
async function decrypt(payload, ts) {
const key = await deriveKey(ts);
const decrypted = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv: new Uint8Array(payload.iv) },
key,
new Uint8Array(payload.data)
);
return decoder.decode(decrypted);
}
/* ---------- STORAGE ---------- */
function loadNotes() {
return JSON.parse(localStorage.getItem("notes") || "[]");
}
function saveNotes(notes) {
localStorage.setItem("notes", JSON.stringify(notes));
}
/* ---------- TIME FORMATTING ---------- */
function formatTimeRemaining(ms) {
const seconds = Math.ceil(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (days > 0) return `${days}d ${hours % 24}h remaining`;
if (hours > 0) return `${hours}h ${minutes % 60}m remaining`;
if (minutes > 0) return `${minutes}m ${seconds % 60}s remaining`;
return `${seconds}s remaining`;
}
/* ---------- UI ---------- */
let isLoading = true;
function showSkeletons(count = 5) {
notesList.innerHTML = "";
for (let i = 0; i < count; i++) {
const skeleton = document.createElement("li");
skeleton.className = "skeleton-note";
skeleton.innerHTML = `
<div class="skeleton-title"></div>
<div class="skeleton-text"></div>
`;
notesList.appendChild(skeleton);
}
}
async function renderNotes() {
const notes = loadNotes();
const now = Date.now();
// Show loading skeletons on first load
if (isLoading) {
showSkeletons();
isLoading = false;
// Short delay to show skeleton effect
await new Promise(resolve => setTimeout(resolve, 500));
}
notesList.innerHTML = "";
// Show empty state if no notes
if (notes.length === 0) {
notesList.innerHTML = `
<div class="empty-state">
<div class="empty-state-icon">🔒</div>
<p><strong>No locked notes yet</strong></p>
<p>Create your first time-locked note using the + button</p>
</div>
`;
return;
}
for (let i = notes.length - 1; i >= 0; i--) {
const note = notes[i];
const li = document.createElement("li");
li.className = "note";
/* Auto-remove after viewing window */
if (note.viewUntil && now > note.viewUntil) {
notes.splice(i, 1);
saveNotes(notes);
continue;
}
/* Still locked */
if (now < note.unlockTime) {
li.innerHTML = `
<strong>Locked</strong>
<small>${formatTimeRemaining(note.unlockTime - now)}</small>
`;
}
/* Ready to unlock */
else if (!note.unlocked) {
li.innerHTML = `
<strong>Ready to Unlock</strong>
<small>Unlock time has been reached</small>
`;
const btn = document.createElement("button");
btn.textContent = "🔓 Reveal Text";
btn.onclick = async () => {
const ok = confirm("Reveal this text? You will have 20 minutes to view it before it's permanently deleted.");
if (!ok) return;
try {
note.plaintext = await decrypt(note.encrypted, note.unlockTime);
note.unlocked = true;
note.viewUntil = Date.now() + 20 * 60 * 1000;
saveNotes(notes);
renderNotes();
} catch (error) {
alert("Failed to decrypt note. It may be corrupted.");
console.error(error);
}
};
li.appendChild(btn);
}
/* Unlocked + viewing */
else {
const timeLeft = note.viewUntil - now;
li.innerHTML = `
<strong>Unlocked</strong>
<small>View time remaining: ${formatTimeRemaining(timeLeft)}</small>
<pre>${escapeHtml(note.plaintext)}</pre>
`;
}
notesList.appendChild(li);
}
}
/* ---------- UTILITY ---------- */
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/* ---------- EVENTS ---------- */
lockBtn.onclick = async () => {
const text = textInput.value.trim();
const unlockTimeValue = unlockTimeInput.value;
if (!text) {
alert("Please enter some text to lock");
textInput.focus();
return;
}
if (!unlockTimeValue) {
alert("Please select an unlock time");
unlockTimeInput.focus();
return;
}
const ts = new Date(unlockTimeValue).getTime();
const now = Date.now();
if (ts <= now) {
alert("Unlock time must be in the future");
unlockTimeInput.focus();
return;
}
if (!confirmStep) {
confirmStep = true;
lockBtn.textContent = "✓ Confirm Lock";
lockBtn.style.background = "var(--success)";
return;
}
try {
const encrypted = await encrypt(text, ts);
const notes = loadNotes();
notes.push({ encrypted, unlockTime: ts });
saveNotes(notes);
closeModal();
renderNotes();
} catch (error) {
alert("Failed to encrypt note. Please try again.");
console.error(error);
}
};
// Reset confirm step when input changes
textInput.oninput = () => {
if (confirmStep) {
confirmStep = false;
lockBtn.textContent = "Lock Note";
lockBtn.style.background = "";
}
};
unlockTimeInput.onchange = () => {
if (confirmStep) {
confirmStep = false;
lockBtn.textContent = "Lock Note";
lockBtn.style.background = "";
}
};
// Update UI every second
setInterval(renderNotes, 1000);
// Initial render
renderNotes();