-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
140 lines (120 loc) · 4.59 KB
/
Copy pathoptions.js
File metadata and controls
140 lines (120 loc) · 4.59 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
// ApplyPilot — options / profile page
const form = document.getElementById("profileForm");
const saveState = document.getElementById("saveState");
const toastEl = document.getElementById("toast");
const exportBtn = document.getElementById("exportBtn");
let saveTimer;
async function load() {
const { profile = {} } = await chrome.storage.local.get("profile");
for (const el of form.elements) {
if (el.name && profile[el.name] != null) el.value = profile[el.name];
}
}
function collect() {
const profile = {};
for (const el of form.elements) {
if (el.name) profile[el.name] = el.value.trim ? el.value.trim() : el.value;
}
return profile;
}
async function persist(showToast) {
await chrome.storage.local.set({ profile: collect() });
if (showToast) toast("Profile saved ✓");
saveState.textContent = "All changes saved";
setTimeout(() => (saveState.textContent = ""), 2500);
}
// Explicit save
form.addEventListener("submit", (e) => { e.preventDefault(); persist(true); });
// Autosave on edit (debounced) so nothing is lost
form.addEventListener("input", () => {
saveState.textContent = "Saving…";
clearTimeout(saveTimer);
saveTimer = setTimeout(() => persist(false), 700);
});
// Export as JSON download
exportBtn.addEventListener("click", async () => {
const { profile = {}, jobs = [], resume = null } = await chrome.storage.local.get(["profile", "jobs", "resume"]);
const blob = new Blob([JSON.stringify({ profile, jobs, resume }, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `applypilot-export-${new Date().toISOString().slice(0, 10)}.json`;
a.click();
URL.revokeObjectURL(url);
toast("Exported ✓");
});
// ---------- Résumé upload ----------
const resumeInput = document.getElementById("resumeInput");
const resumePick = document.getElementById("resumePick");
const resumeInfo = document.getElementById("resumeInfo");
const resumeRemove = document.getElementById("resumeRemove");
const MAX_RESUME = 5 * 1024 * 1024; // 5 MB
function fmtSize(bytes) {
if (bytes < 1024) return bytes + " B";
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(0) + " KB";
return (bytes / 1024 / 1024).toFixed(1) + " MB";
}
function showResume(resume) {
if (resume && resume.name) {
resumeInfo.textContent = `${resume.name} · ${fmtSize(resume.size)}`;
resumeInfo.classList.add("has-file");
resumeRemove.classList.remove("hidden");
resumePick.textContent = "Replace file…";
} else {
resumeInfo.textContent = "No résumé uploaded";
resumeInfo.classList.remove("has-file");
resumeRemove.classList.add("hidden");
resumePick.textContent = "Choose file…";
}
}
resumePick.addEventListener("click", () => resumeInput.click());
resumeInput.addEventListener("change", () => {
const file = resumeInput.files[0];
if (!file) return;
if (file.size > MAX_RESUME) { toast("File too large (max 5 MB)"); resumeInput.value = ""; return; }
const reader = new FileReader();
reader.onload = async () => {
const resume = { name: file.name, type: file.type, size: file.size, dataUrl: reader.result };
await chrome.storage.local.set({ resume });
showResume(resume);
toast("Résumé saved ✓");
};
reader.onerror = () => toast("Couldn't read that file");
reader.readAsDataURL(file);
resumeInput.value = "";
});
resumeRemove.addEventListener("click", async () => {
await chrome.storage.local.remove("resume");
showResume(null);
toast("Résumé removed");
});
async function loadResume() {
const { resume } = await chrome.storage.local.get("resume");
showResume(resume);
}
// ---------- First-run onboarding ----------
const welcome = document.getElementById("welcome");
const welcomeClose = document.getElementById("welcomeClose");
async function dismissWelcome() {
welcome.classList.add("hidden");
await chrome.storage.local.set({ onboarded: true });
}
welcomeClose.addEventListener("click", dismissWelcome);
async function loadWelcome() {
const { onboarded } = await chrome.storage.local.get("onboarded");
if (onboarded === false) {
welcome.classList.remove("hidden");
// Once they start filling things in, don't show the welcome again — but keep
// it visible for the rest of this session unless dismissed.
form.addEventListener("input", () => chrome.storage.local.set({ onboarded: true }), { once: true });
}
}
function toast(msg) {
toastEl.textContent = msg;
toastEl.classList.add("show");
clearTimeout(toast._t);
toast._t = setTimeout(() => toastEl.classList.remove("show"), 2000);
}
load();
loadResume();
loadWelcome();