-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpopup.js
70 lines (64 loc) · 2.14 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
function getStorageData(keys) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(keys, function (result) {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(result);
}
});
});
}
const options = {
formatOutput: true,
showUploadButton: true,
showLanguageButton: true,
enableDirectPasting: true,
enableDragAndDrop: true,
useThirdPartyOCR: false,
theme: "default-mode",
showInitialLoadingMessage: true,
};
getStorageData(Object.keys(options))
.then((result) => {
// If options are not set in storage, use default values
Object.keys(options).forEach((optionKey) => {
options[optionKey] = result.hasOwnProperty(optionKey) ? result[optionKey] : options[optionKey];
if (optionKey === "theme") {
// Set the selected option in the dropdown
document.getElementById("theme").value = options[optionKey];
} else {
// Set checkbox state
document.getElementById(optionKey).checked = options[optionKey];
}
});
})
.catch((error) => {
console.error("Error getting data from storage: ", error);
});
document.getElementById("save-button").addEventListener("click", function () {
// Get current states
Object.keys(options).forEach((optionKey) => {
if (optionKey === "theme") {
// Get the selected option in the dropdown (for dropdown options)
let e = document.getElementById("theme");
options[optionKey] = e.options[e.selectedIndex].value;
} else {
// Get checkbox states (for boolean options)
options[optionKey] = document.getElementById(optionKey).checked;
}
});
// Save options to chrome's local storage
chrome.storage.local.set(options, function () {
console.log("Options saved");
// Notify user that options are saved
let notification = document.getElementById("notification");
notification.style.display = "block";
notification.innerHTML = "<p>Options saved! Please refresh the page to apply the settings.</p>";
// Remove the notification after some time
setTimeout(function () {
notification.style.display = "none";
notification.innerHTML = "";
}, 3000);
});
});