Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
109 lines (99 loc) · 3.33 KB
/
Copy pathpopup.js
File metadata and controls
109 lines (99 loc) · 3.33 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
const enabledCheckbox = document.getElementById('enabled');
const autoRenameCheckbox = document.getElementById('autoRename');
const profileSelect = document.getElementById('profileSelect');
const profileNameInput = document.getElementById('profileName');
const primerTextarea = document.getElementById('primerText');
const newProfileBtn = document.getElementById('newProfile');
const deleteProfileBtn = document.getElementById('deleteProfile');
const statusEl = document.getElementById('status');
let profiles = [];
let activeProfileId = null;
let saveTimer = null;
function renderProfileSelect() {
profileSelect.innerHTML = '';
profiles.forEach(p => {
const opt = document.createElement('option');
opt.value = p.id;
opt.textContent = p.name;
profileSelect.appendChild(opt);
});
profileSelect.value = activeProfileId ?? '';
deleteProfileBtn.disabled = profiles.length <= 1;
}
function loadActiveProfile() {
const profile = profiles.find(p => p.id === activeProfileId);
if (profile) {
profileNameInput.value = profile.name;
primerTextarea.value = profile.text;
autoRenameCheckbox.checked = profile.autoRename ?? false;
} else {
profileNameInput.value = '';
primerTextarea.value = '';
autoRenameCheckbox.checked = false;
}
}
function showStatus(msg) {
statusEl.textContent = msg;
setTimeout(() => { statusEl.textContent = ''; }, 1500);
}
function save() {
clearTimeout(saveTimer);
saveTimer = setTimeout(() => {
// Update current profile's name and text from inputs
const profile = profiles.find(p => p.id === activeProfileId);
if (profile) {
profile.name = profileNameInput.value;
profile.text = primerTextarea.value;
profile.autoRename = autoRenameCheckbox.checked;
// Update dropdown label
const opt = profileSelect.querySelector(`option[value="${profile.id}"]`);
if (opt) opt.textContent = profile.name;
}
chrome.storage.local.set({
enabled: enabledCheckbox.checked,
profiles,
activeProfileId
}, () => showStatus('Saved'));
}, 300);
}
// Load saved settings
chrome.storage.local.get(['enabled', 'primerText', 'profiles', 'activeProfileId'], (result) => {
const migrated = migrateIfNeeded(result);
enabledCheckbox.checked = migrated.enabled ?? false;
profiles = migrated.profiles;
activeProfileId = migrated.activeProfileId;
renderProfileSelect();
loadActiveProfile();
});
// Profile switching
profileSelect.addEventListener('change', () => {
activeProfileId = profileSelect.value;
loadActiveProfile();
save();
});
// New profile
newProfileBtn.addEventListener('click', () => {
const id = crypto.randomUUID();
const name = 'Profile ' + (profiles.length + 1);
profiles.push({ id, name, text: '' });
activeProfileId = id;
renderProfileSelect();
loadActiveProfile();
profileNameInput.focus();
profileNameInput.select();
save();
});
// Delete profile
deleteProfileBtn.addEventListener('click', () => {
if (profiles.length <= 1) return;
profiles = profiles.filter(p => p.id !== activeProfileId);
activeProfileId = profiles[0].id;
renderProfileSelect();
loadActiveProfile();
save();
});
// Auto-save on edits
enabledCheckbox.addEventListener('change', save);
autoRenameCheckbox.addEventListener('change', save);
profileNameInput.addEventListener('input', save);
primerTextarea.addEventListener('input', save);