-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
194 lines (175 loc) · 6.44 KB
/
popup.js
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
// Maximum number of profiles
const MAX_PROFILES = 10;
// Function to show toast notifications
function showToast(message) {
const toast = document.getElementById("toast");
toast.textContent = message;
toast.className = "show";
setTimeout(() => {
toast.className = toast.className.replace("show", "");
}, 3000);
}
// Load profiles into the select field
function loadProfiles() {
chrome.storage.sync.get(["profiles"], (data) => {
const profiles = data.profiles || {};
const profileSelect = document.getElementById("profiles");
profileSelect.innerHTML =
'<option value="" disabled selected>Select a profile</option>';
Object.keys(profiles).forEach((name) => {
const option = document.createElement("option");
option.value = name;
option.textContent = name;
profileSelect.appendChild(option);
});
});
}
// Save current settings as a backup before loading a new profile
function backupCurrentSettings(callback) {
const currentSettings = {
width: document.getElementById("width").value,
height: document.getElementById("height").value,
quality: document.getElementById("quality").value,
fit: document.getElementById("fit").value,
crop: document.getElementById("crop").value,
ar: document.getElementById("ar").value,
customQuery: document.getElementById("custom-query").value,
};
chrome.storage.sync.set({ backupSettings: currentSettings }, () => {
if (callback) callback();
});
}
// Save current settings as a profile
function saveCurrentSettingsAsProfile(profileName, callback) {
const settings = {
width: document.getElementById("width").value,
height: document.getElementById("height").value,
quality: document.getElementById("quality").value,
fit: document.getElementById("fit").value,
crop: document.getElementById("crop").value,
ar: document.getElementById("ar").value,
customQuery: document.getElementById("custom-query").value,
};
chrome.storage.sync.get(["profiles"], (data) => {
const profiles = data.profiles || {};
profiles[profileName] = settings;
chrome.storage.sync.set({ profiles }, () => {
loadProfiles();
if (callback) callback();
});
});
}
// Save Profile Button Click
document.getElementById("save-profile").addEventListener("click", () => {
const profileName = document.getElementById("profile-name").value.trim();
if (!profileName) {
showToast("Please enter a profile name.");
return;
}
chrome.storage.sync.get(["profiles"], (data) => {
const profiles = data.profiles || {};
if (
Object.keys(profiles).length >= MAX_PROFILES &&
!profiles[profileName]
) {
showToast(`Maximum of ${MAX_PROFILES} profiles reached.`);
return;
}
saveCurrentSettingsAsProfile(profileName, () => {
document.getElementById("profile-name").value = "";
showToast("Profile saved successfully!");
});
});
});
// Load Profile Button Click
document.getElementById("load-profile").addEventListener("click", () => {
const selectedProfile = document.getElementById("profiles").value;
if (!selectedProfile) {
showToast("Please select a profile to load.");
return;
}
// First, backup current settings
backupCurrentSettings(() => {
// Then, load the selected profile
chrome.storage.sync.get(["profiles"], (data) => {
const profiles = data.profiles || {};
const settings = profiles[selectedProfile];
if (settings) {
document.getElementById("width").value = settings.width || "";
document.getElementById("height").value = settings.height || "";
document.getElementById("quality").value = settings.quality || "";
document.getElementById("fit").value = settings.fit || "";
document.getElementById("crop").value = settings.crop || "";
document.getElementById("ar").value = settings.ar || "";
document.getElementById("custom-query").value =
settings.customQuery || "";
// Save the loaded profile settings to chrome.storage.sync
chrome.storage.sync.set(settings, () => {
showToast("Profile loaded and settings saved!");
});
} else {
showToast("Selected profile does not exist.");
}
});
});
});
// Delete Profile Button Click (Optional)
document.getElementById("delete-profile").addEventListener("click", () => {
const selectedProfile = document.getElementById("profiles").value;
if (!selectedProfile) {
showToast("Please select a profile to delete.");
return;
}
chrome.storage.sync.get(["profiles"], (data) => {
const profiles = data.profiles || {};
if (profiles[selectedProfile]) {
delete profiles[selectedProfile];
chrome.storage.sync.set({ profiles }, () => {
loadProfiles();
showToast("Profile deleted successfully!");
});
}
});
});
// Save Settings Button Click
document.getElementById("save-button").addEventListener("click", () => {
const settings = {
width: document.getElementById("width").value,
height: document.getElementById("height").value,
quality: document.getElementById("quality").value,
fit: document.getElementById("fit").value,
crop: document.getElementById("crop").value,
ar: document.getElementById("ar").value,
customQuery: document.getElementById("custom-query").value,
};
chrome.storage.sync.set(settings, () => {
showToast("Settings saved!");
});
});
// Reset Settings Button Click
document.getElementById("reset-button").addEventListener("click", () => {
document.getElementById("settings-form").reset();
chrome.storage.sync.clear(() => {
loadProfiles();
showToast("Settings reset!");
});
});
// Initialize profiles on DOM load
document.addEventListener("DOMContentLoaded", () => {
// Load existing settings
chrome.storage.sync.get(
["width", "height", "quality", "fit", "crop", "ar", "customQuery"],
(data) => {
if (data.width) document.getElementById("width").value = data.width;
if (data.height) document.getElementById("height").value = data.height;
if (data.quality) document.getElementById("quality").value = data.quality;
if (data.fit) document.getElementById("fit").value = data.fit;
if (data.crop) document.getElementById("crop").value = data.crop;
if (data.ar) document.getElementById("ar").value = data.ar;
if (data.customQuery)
document.getElementById("custom-query").value = data.customQuery;
}
);
// Load profiles into the dropdown
loadProfiles();
});