-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpopup.js
288 lines (237 loc) · 9.48 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// File responsible for the extension popup
document.addEventListener("DOMContentLoaded", function () {
initLanguage();
populateReplacedWords();
initForm();
initAddButton(); // Initialize the add-button functionality
});
function initAddButton() {
const addButton = document.getElementById("edit-button");
if (addButton) {
addButton.addEventListener("click", function () {
openDialog(); // Navigate to the word-replacement section
});
}
}
function initForm() {
const form = document.getElementById("input-form");
const wordInput = document.getElementById("word-input");
const replacementInput = document.getElementById("replacement-input");
const errorMessage = document.getElementById("error-message");
const errorText = document.getElementById("error-text");
const loadingIndicator = document.getElementById("loading-indicator");
const submitButton = document.getElementById("dialog-submit");
// Initially disable the Save button
submitButton.disabled = true;
const inputs = [wordInput, replacementInput];
// Add event listeners for input fields
inputs.forEach((input) => {
input.addEventListener("input", validateInputs);
input.addEventListener("focus", handleFocus);
input.addEventListener("blur", handleBlur);
});
form.addEventListener("submit", async (event) => {
event.preventDefault(); // Prevent default form submission
const wordValue = wordInput.value.trim();
const replacementValue = replacementInput.value.trim();
// Check if the words are the same
if (wordValue === replacementValue) {
// Show error message but allow Save button to be clicked
showErrorMessage("The word and its replacement cannot be the same.");
wordInput.classList.add("input-error");
replacementInput.classList.add("input-error");
return;
}
// Check if either field is empty
if (!wordValue || !replacementValue) {
showErrorMessage("Both fields are required.");
return;
}
// Show loading indicator and hide error message
loadingIndicator.style.display = "block";
clearErrorStyles();
try {
const response = await fetch(
"https://script.google.com/macros/s/AKfycbwqV-kCvRonl9MXdSOP7l7LsMh4ZA-Ro0eLsDvrruF228OI4UT1-AW5JFuijnNqsg5V/exec",
{
method: "POST",
body: new FormData(form),
}
);
if (response.ok) {
addWord(wordValue, replacementValue);
closeDialog();
} else {
showErrorMessage("Submission failed. Please try again.");
}
} catch (error) {
console.error("Error:", error);
showErrorMessage("Submission failed. Please try again.");
} finally {
// Hide loading indicator
loadingIndicator.style.display = "none";
}
});
document
.querySelector(".dialog-close")
.addEventListener("click", closeDialog);
// Validation logic for enabling/disabling Save button
function validateInputs() {
const wordValue = wordInput.value.trim();
const replacementValue = replacementInput.value.trim();
if (!wordValue || !replacementValue) {
// Disable Save button if either input is empty
submitButton.disabled = true;
clearErrorStyles();
} else {
// Enable Save button if inputs are valid
submitButton.disabled = false;
clearErrorStyles();
}
}
function showErrorMessage(message) {
errorMessage.style.display = "flex";
errorText.textContent = message;
}
function clearErrorStyles() {
wordInput.classList.remove("input-error");
replacementInput.classList.remove("input-error");
errorMessage.style.display = "none";
errorText.textContent = "";
}
// Handle focus and blur events to animate placeholder as a label
function handleFocus(event) {
const label = event.target.previousElementSibling;
if (label) label.classList.add("label-focused");
}
function handleBlur(event) {
const label = event.target.previousElementSibling;
if (label && !event.target.value.trim()) label.classList.remove("label-focused");
}
}
function initLanguage() {
const languageSelect = document.getElementById("language-select");
languageSelect.addEventListener("change", changeLanguage);
chrome.storage.sync.get(["selectedLanguage"], function (result) {
const selectedLanguage = result.selectedLanguage || "en"; // Default to English if no language is saved
languageSelect.value = selectedLanguage;
setLanguage(selectedLanguage);
});
}
function changeLanguage() {
const languageSelect = document.getElementById("language-select");
const selectedLanguage = languageSelect.value;
setLanguage(selectedLanguage);
// Save the selected language
chrome.storage.sync.set({ selectedLanguage: selectedLanguage });
}
function setLanguage(language) {
if (language === "en") {
loadLanguage(en);
document.body.setAttribute("dir", "ltr");
document.querySelector(".header").setAttribute("dir", "ltr");
document.body.style.fontFamily = "'Montserrat', sans-serif";
} else if (language === "ar") {
loadLanguage(ar);
document.body.setAttribute("dir", "rtl");
document.querySelector(".header").setAttribute("dir", "rtl");
document.body.style.fontFamily = "'Beiruti', sans-serif";
}
}
function loadLanguage(lang) {
// Update titles and headers
document.getElementById("replaced-words-title").textContent = lang.replacedWords;
document.getElementById("word-header").textContent = lang.word;
document.getElementById("replacement-header").textContent = lang.replacement;
// Update buttons
document.getElementById("dialog-submit").textContent = lang.submitButton;
document.getElementById("dialog-close").textContent = lang.cancelButton;
// Update form labels
document.getElementById("word-label").textContent = lang.wordLabel;
document.getElementById("replacement-label").textContent = lang.replacementLabel;
// Update error messages
document.getElementById("error-message").textContent = lang.errorSubmissionFailed;
// Update input placeholders
document.getElementById("word-input").placeholder = lang.wordInput;
document.getElementById("replacement-input").placeholder = lang.replacementInput;
}
function getLocalizedString(key) {
const language = document.getElementById("language-select").value;
const lang = language === "en" ? en : ar;
return lang[key];
}
function populateReplacedWords() {
chrome.storage.local.get("replacedWords", function (data) {
if (data.replacedWords && data.replacedWords.length > 0) {
data.replacedWords.forEach((word) => {
const tableBody = document.getElementById("table-body");
const row = document.createElement("tr");
const wordCell = document.createElement("td");
const wordLink = document.createElement("a");
wordLink.href = `https://www.palestineremembered.com/Search.html#gsc.tab=0&gsc.sort=&gsc.q=${encodeURIComponent(
word.original
)}`;
wordLink.target = "_blank"; // Open link in new tab
wordLink.textContent = word.original;
wordLink.style.color = "#97700B";
wordLink.style.textDecoration = "none";
wordCell.appendChild(wordLink);
const replacementCell = document.createElement("td");
const replacementLink = document.createElement("a");
replacementLink.href = `https://www.palestineremembered.com/Search.html#gsc.tab=0&gsc.sort=&gsc.q=${encodeURIComponent(
word.replacement
)}`;
replacementLink.target = "_blank";
replacementLink.textContent = word.replacement;
replacementLink.style.color = "#000000";
replacementLink.style.textDecoration = "none";
replacementCell.appendChild(replacementLink);
row.appendChild(wordCell);
row.appendChild(replacementCell);
tableBody.appendChild(row);
});
}
});
}
function addWord(word, replacement) {
if (word && replacement) {
const tableBody = document.getElementById("table-body");
const row = document.createElement("tr");
const wordCell = document.createElement("td");
const wordLink = document.createElement("a");
wordLink.href = `https://www.palestineremembered.com/Search.html#gsc.tab=0&gsc.sort=&gsc.q=${encodeURIComponent(
word
)}`;
wordLink.target = "_blank"; // Open link in new tab
wordLink.textContent = word;
wordCell.appendChild(wordLink);
const replacementCell = document.createElement("td");
const replacementLink = document.createElement("a");
replacementLink.href = `https://www.palestineremembered.com/Search.html#gsc.tab=0&gsc.sort=&gsc.q=${encodeURIComponent(
replacement
)}`;
replacementLink.target = "_blank";
replacementLink.textContent = replacement;
replacementCell.appendChild(replacementLink);
row.appendChild(wordCell);
row.appendChild(replacementCell);
tableBody.appendChild(row);
}
}
function openDialog() {
const inputDialog = document.getElementById("input-dialog");
const content = document.getElementById("content");
if (inputDialog && content) {
inputDialog.style.display = "block"; // Show the input dialog
content.style.display = "none"; // Hide the main content
}
}
function closeDialog() {
const inputDialog = document.getElementById("input-dialog");
const content = document.getElementById("content");
inputDialog.style.display = "none";
content.style.display = "block";
const form = document.getElementById("input-form");
form.reset(); // Reset the form inputs
document.getElementById("error-message").textContent = ""; // Clear error message
}