-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
210 lines (165 loc) · 5.61 KB
/
app.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* I don't care if you moved on */
import {
copyToClipboard,
decodeVariable,
downloadAsTextFile,
dialog,
autoResizeTextarea,
} from "./helper.js";
import { chromeGetData, chromeSetData } from "./chrome.js";
const maxNotesChars = 999;
(function () {
console.info("I'm not laying in bed with a fucked up head");
restoreSettings();
loadEventListeners();
})();
function loadEventListeners() {
const $copyToClipboard = document.querySelector(".app-copy-to-clipboard");
const $notes = document.querySelector("#app-notes");
const $openSettingsBtn = document.querySelector(".app-open-settings");
const $downloadTextFile = document.querySelector(
".app-download-as-text-file"
);
const $useTemplateBtn = document.querySelector(".app-trigger-use-template");
const $notesCurrentChars = document.querySelector(
".app-notes-current-chars-count"
);
const saveTimeout = 500;
let typingTimer;
$copyToClipboard.addEventListener("click", function () {
if ($notes.value.length < 1) {
dialog({
content:
"There's nothing to copy at the moment. Please write something at least.",
type: "alert",
});
return;
}
copyToClipboard($notes, function () {
dialog({
content:
"Copied to clipboard, you're all set to send it anywhere you want.",
type: "alert",
});
console.log("Copied!");
});
});
$notes.addEventListener("input", () => {
clearInterval(typingTimer);
typingTimer = setTimeout(saveNotes, saveTimeout);
$notesCurrentChars.innerHTML = maxNotesChars - $notes.value.length;
});
$openSettingsBtn.addEventListener("click", () => {
chrome.runtime.openOptionsPage
? chrome.runtime.openOptionsPage()
: window.open(chrome.runtime.getURL("options.html"));
});
$useTemplateBtn.addEventListener("click", (e) => {
e.preventDefault();
dialog({
content: `
Are you sure you want to use your Notes template now? This will override your current notes.
`,
confirmCallback: () => {
loadNotesTemplate();
console.log("Template notes loaded.");
},
});
});
$downloadTextFile.addEventListener("click", () => {
if ($notes.value.length < 1) {
dialog({
content: `
Your notes are empty. Nothing to download.
`,
type: "alert",
});
return;
}
downloadAsTextFile($notes.value);
});
}
async function loadNotesTemplate() {
const $notes = document.querySelector("#app-notes");
const notesTemplate = await chromeGetData("notesTemplate");
if (notesTemplate) {
$notes.value = decodeVariable(notesTemplate);
autoResizeTextarea($notes);
saveNotes();
console.log("I won't feel a thing");
}
}
async function saveNotes() {
const $notes = document.querySelector("#app-notes");
// Do not save if notes length is greater than maxNotesChars
if ($notes.value.length > maxNotesChars) {
console.log("I'm not laying in bed with a fucked up");
return;
}
let now = new Date();
const notesSaved = await chromeSetData("notes", $notes.value);
const lastUpdated = await chromeSetData("lastUpdated", now);
if (notesSaved && lastUpdated) {
console.log("Notes saved.");
}
}
function toggleElementDisplay(selector = null, display) {
if (typeof selector != "string" || typeof display != "string") {
return;
}
const $selector = document.querySelector(selector);
if (display == "show") {
if ($selector.classList.contains("display-none")) {
$selector.classList.remove("display-none");
}
} else if (display == "hide") {
$selector.classList.add("display-none");
}
}
async function restoreSettings() {
const $notes = document.querySelector("#app-notes");
const $html = document.querySelector("html");
const $notesCurrentChars = document.querySelector(
".app-notes-current-chars-count"
);
let today = new Date();
const resetNotes = await chromeGetData("resetNotes", false);
const lastUpdated = await chromeGetData("lastUpdated", today);
let notesTemplate = await chromeGetData("notesTemplate", "");
const notes = await chromeGetData("notes", "");
const enableDarkMode = await chromeGetData("enableDarkMode", false);
// If dark mode is enabled
$html.dataset.theme = enableDarkMode ? "dark" : "light";
const lastUpdatedPresise = new Date(lastUpdated);
today.setHours(0, 0, 0, 0); // Set today's time to 0
const withinToday = lastUpdatedPresise > today;
notesTemplate = decodeVariable(notesTemplate);
// Check if notes template is set to toggle trigger button
if (notesTemplate.length > 0) {
toggleElementDisplay(".app-trigger-use-template", "show");
} else {
toggleElementDisplay(".app-trigger-use-template", "hide");
}
// Check if automatic reset notes is enabled
if (resetNotes) {
console.log("Reset notes is enabled.");
// If last updated is not within today -> load template
if (!withinToday) {
$notes.value = notesTemplate;
$notesCurrentChars.innerHTML = maxNotesChars - $notes.value.length;
autoResizeTextarea($notes);
saveNotes(); // Save currently loaded template as notes
return;
}
// Else if last updated is within today -> load recently saved notes
$notes.value = notes;
$notesCurrentChars.innerHTML = maxNotesChars - $notes.value.length;
autoResizeTextarea($notes);
return;
}
console.log("Reset notes is disabled. Displaying recently saved notes.");
// If automatic reset of notes is disabled -> load recently saved notes
$notes.value = notes;
$notesCurrentChars.innerHTML = maxNotesChars - $notes.value.length;
autoResizeTextarea($notes);
}