-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (119 loc) · 3.88 KB
/
Copy pathscript.js
File metadata and controls
139 lines (119 loc) · 3.88 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
const header = document.querySelector(".site-header");
const revealItems = document.querySelectorAll(".reveal");
function updateHeader() {
if (!header) return;
header.classList.toggle("scrolled", window.scrollY > 24);
}
if (revealItems.length) {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
observer.unobserve(entry.target);
}
});
},
{ threshold: 0.12, rootMargin: "0px 0px -40px" }
);
revealItems.forEach((item, index) => {
item.style.transitionDelay = `${Math.min(index % 4, 3) * 70}ms`;
observer.observe(item);
});
}
window.addEventListener("scroll", updateHeader, { passive: true });
updateHeader();
document.querySelectorAll('a[href^="#"]').forEach((link) => {
link.addEventListener("click", (event) => {
const target = document.querySelector(link.getAttribute("href"));
if (!target) return;
event.preventDefault();
target.scrollIntoView({ behavior: "smooth", block: "start" });
});
});
const modal = document.getElementById("cv-modal");
const form = document.getElementById("cv-form");
const input = document.getElementById("cv-code");
const error = document.getElementById("cv-error");
function openCvModal() {
if (!modal) return;
modal.hidden = false;
document.body.classList.add("modal-open");
error.hidden = true;
input.value = "";
input.focus();
}
function closeCvModal() {
if (!modal) return;
modal.hidden = true;
document.body.classList.remove("modal-open");
}
async function deriveKey(password, salt) {
const material = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(password),
"PBKDF2",
false,
["deriveKey"]
);
return crypto.subtle.deriveKey(
{ name: "PBKDF2", salt, iterations: 210000, hash: "SHA-256" },
material,
{ name: "AES-GCM", length: 256 },
false,
["decrypt"]
);
}
async function downloadResume(password) {
const response = await fetch("./files/cv.enc", { cache: "no-store" });
if (!response.ok) throw new Error("missing");
const bytes = new Uint8Array(await response.arrayBuffer());
const magic = new TextDecoder().decode(bytes.slice(0, 4));
if (magic !== "TSJ1") throw new Error("format");
const salt = bytes.slice(4, 20);
const iv = bytes.slice(20, 32);
const tag = bytes.slice(32, 48);
const encrypted = bytes.slice(48);
const payload = new Uint8Array(encrypted.length + tag.length);
payload.set(encrypted, 0);
payload.set(tag, encrypted.length);
const key = await deriveKey(password, salt);
const plain = await crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, payload);
const blob = new Blob([plain], { type: "application/pdf" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "童绍俊-港科AIE硕士-产品经理实习-2028届.pdf";
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
}
document.querySelectorAll("[data-cv-open]").forEach((button) => {
button.addEventListener("click", openCvModal);
});
document.querySelectorAll("[data-cv-close]").forEach((button) => {
button.addEventListener("click", closeCvModal);
});
modal?.addEventListener("click", (event) => {
if (event.target === modal) closeCvModal();
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape" && modal && !modal.hidden) closeCvModal();
});
form?.addEventListener("submit", async (event) => {
event.preventDefault();
error.hidden = true;
const submit = form.querySelector("[type='submit']");
submit.disabled = true;
submit.textContent = "解密中…";
try {
await downloadResume(input.value.trim());
closeCvModal();
} catch {
error.hidden = false;
} finally {
submit.disabled = false;
submit.textContent = "下载 PDF";
}
});