-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathessentials.js
More file actions
208 lines (173 loc) · 4.74 KB
/
essentials.js
File metadata and controls
208 lines (173 loc) · 4.74 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// ================================
// URL Updater
// ================================
function updateURL() {
if (!config.get("updateURL")) return;
if (!keys || keys.length === 0) {
history.replaceState(null, "", globalThis.location.origin + globalThis.location.pathname);
return;
}
const keysForURL = keys.map(k => prepareKey(k, { shortImport: true }));
const data = JSON.stringify(keysForURL);
const base64Data = base64EncodeUnicode(data);
const newURL = globalThis.location.origin + globalThis.location.pathname + "#keys=" + base64Data;
history.replaceState(null, "", newURL);
}
function getKeysFromURL() {
if (globalThis.location.hash.startsWith("#keys=")) {
try {
return JSON.parse(base64DecodeUnicode(globalThis.location.hash.substring(6)));
} catch (e) {
console.error("Error reading keys from URL:", e);
}
}
return null;
}
// ================================
// Notifications
// ================================
function showNotification(message) {
const n = document.getElementById("notification");
n.querySelector("p").textContent = String(message);
n.style.display = "block";
n.style.animation = "fadeIn 0.5s ease-in-out";
setTimeout(() => {
n.style.animation = "fadeOut 0.5s ease-in-out";
setTimeout(() => {
n.style.display = "none";
}, 500);
}, 2300);
}
globalThis.alert = function (message) {
showNotification(message);
};
// ================================
// Custom Prompt
// ================================
function customPrompt(text, isPassword = 0, noEscape = 0) {
return new Promise((resolve) => {
const promptDialog = document.getElementById("customPromptDialog");
const promptText = document.getElementById("customPromptText");
const promptInput = document.getElementById("customPromptInput");
const okBtn = document.getElementById("customPromptOk");
const cancelBtn = document.getElementById("customPromptCancel");
promptText.innerText = text;
promptInput.value = "";
if (config.get('passwordHideOn') === false) {
promptInput.type = "text";
} else {
promptInput.type = isPassword ? "password" : "text";
}
okBtn.disabled = true;
promptDialog.showModal();
function blockEscape(event) {
if (event.key === "Escape") {
event.preventDefault();
}
}
if (noEscape === 1) {
promptDialog.addEventListener("cancel", preventCancel);
document.addEventListener("keydown", blockEscape);
}
function preventCancel(e) {
e.preventDefault();
}
function cleanup() {
okBtn.removeEventListener("click", onOk);
cancelBtn.removeEventListener("click", onCancel);
if (noEscape === 1) {
document.removeEventListener("keydown", blockEscape);
promptDialog.removeEventListener("cancel", preventCancel);
}
promptInput.type = "text";
promptDialog.close();
}
function onOk(e) {
e.preventDefault();
resolve(promptInput.value);
cleanup();
}
function onCancel() {
resolve(null);
cleanup();
}
promptInput.addEventListener('input', function() {
okBtn.disabled = !this.value.trim();
});
okBtn.addEventListener("click", onOk);
cancelBtn.addEventListener("click", onCancel);
});
}
globalThis.prompt = customPrompt;
document.getElementById('customPromptInput').addEventListener('input', function() {
document.getElementById('customPromptOk').disabled = !this.value.trim();
});
// ================================
// Config
// ================================
const config = {
key: 'config',
values: {},
defaults: {},
load() {
try {
const stored = localStorage.getItem(this.key);
if (stored) {
this.values = JSON.parse(stored);
}
} catch (e) {
console.error("Error loading config:", e);
this.values = {};
}
for (let key in this.defaults) {
if (!(key in this.values)) {
this.values[key] = this.defaults[key];
}
}
},
save() {
try {
localStorage.setItem(this.key, JSON.stringify(this.values));
} catch (e) {
console.error("Error saving config:", e);
}
},
set(key, value) {
this.values[key] = value;
this.save();
},
get(key) {
return this.values[key];
},
reset(key) {
if (key in this.defaults) {
this.values[key] = this.defaults[key];
this.save();
}
},
exportstr() {
return JSON.stringify(this.values);
},
importstr(str) {
try {
const obj = JSON.parse(str);
if (typeof obj === 'object' && obj !== null) {
this.values = obj;
this.save();
}
} catch (e) {
console.error("[Config] Invalid import string", e);
}
}
};
// ================================
// Serviceworker
// ================================
if (location.origin !== "null" && location.protocol !== "file:") {
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("sw.js")
.then(() => console.log("sw registered"))
.catch((error) => console.error("sw error:", error));
}
}