-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
75 lines (66 loc) · 2.9 KB
/
Copy pathpopup.js
File metadata and controls
75 lines (66 loc) · 2.9 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
function formatMatrixWithNewlines(array, groupSize = 9) {
const result = [];
for (let i = 0; i < array.length; i += groupSize) {
const group = array.slice(i, i + groupSize);
result.push(group.join(','));
}
return result.join(',\n');
}
document.addEventListener("DOMContentLoaded", function () {
const homeSection = document.getElementById("home");
const editSection = document.getElementById("edit");
const matrixDisplay = document.getElementById("matrix");
const matrixInput = document.getElementById("matrixInput");
const saveStatus = document.getElementById("saveStatus");
(async () => {
try {
const result = await browser.storage.local.get('matrix');
if (result.matrix && Array.isArray(result.matrix)) {
matrixDisplay.textContent = formatMatrixWithNewlines(result.matrix);
} else {
matrixDisplay.textContent = "No matrix found.";
}
} catch (err) {
console.error("Error retrieving matrix:", err);
}
})();
document.getElementById("btn-edit").addEventListener("click", () => {
homeSection.style.display = "none";
editSection.style.display = "block";
});
document.getElementById("btn-back-home-edit").addEventListener("click", () => {
homeSection.style.display = "block";
editSection.style.display = "none";
});
document.getElementById("btn-save").addEventListener("click", async () => {
console.log("Save button clicked");
const inputValue = matrixInput.value.trim();
if (inputValue) {
try {
let matrix = JSON.parse(inputValue);
matrix = matrix.map(row =>
row.map(el => String(el)
.replace(/[\s"\n]/g, ''))
);
matrixDisplay.textContent = formatMatrixWithNewlines(matrix);
saveStatus.textContent = "Matrix saved successfully!";
saveStatus.style.color = "green";
browser.storage.local.set({ matrix: matrix });
} catch (e) {
let commaSeparated = inputValue.split(',').map(item => item.trim().replace(/[\s"\[\]\n]/g, ''));
if (commaSeparated.length > 0) {
matrixDisplay.textContent = formatMatrixWithNewlines(commaSeparated);
saveStatus.textContent = "Matrix saved successfully!";
saveStatus.style.color = "green";
browser.storage.local.set({ matrix: commaSeparated });
} else {
saveStatus.textContent = "Invalid input. Must contain exactly 81 elements.";
saveStatus.style.color = "red";
}
}
} else {
saveStatus.textContent = "Input cannot be empty.";
saveStatus.style.color = "red";
}
});
});