-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventHandlers.js
112 lines (97 loc) · 4.03 KB
/
eventHandlers.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
import { sendBookmarksToServer } from "./serverCommunication.js";
import { simplifyBookmarkData } from "./bookmarkUtils.js";
const debugMode = true; // Toggle for enabling/disabling debug logs
function logDebug(message) {
if (debugMode) {
console.log(message);
}
}
function safelyGetElementById(id) {
const element = document.getElementById(id);
if (!element) throw new Error(`Element with id '${id}' not found.`);
return element;
}
async function removeBookmarksRecursively(bookmarkNodes) {
for (const node of bookmarkNodes) {
if (node.children) {
await removeBookmarksRecursively(node.children); // Recursively remove children first
}
if (node.url) { // Check if it's a bookmark (and not a folder)
await browser.bookmarks.remove(node.id);
} else if (node.id !== "0" && node.id !== "1" && node.id !== "2" && !["toolbar_____", "menu________", "unfiled_____", "mobile______"].includes(node.id)) {
// Remove folders that are not root folders
await browser.bookmarks.removeTree(node.id);
}
}
}
async function removeAllBookmarksExceptRoots() {
try {
const roots = await browser.bookmarks.getTree();
for (const root of roots) {
await removeBookmarksRecursively(root.children);
}
logDebug("All non-root bookmarks and folders removed.");
} catch (error) {
console.error("Error removing all bookmarks except roots:", error);
throw error; // Ensure the error is caught in the calling context
}
}
async function setBookmarksInBrowser(sortedBookmarks) {
try {
await removeAllBookmarksExceptRoots(); // Clear existing bookmarks while preserving root folders
// Here, directly use the ID or search for a specific default folder if needed
const toolbarFolderId = "toolbar_____"; // Assuming "toolbar_____" is the ID for the Bookmarks Toolbar
let creationPromises = [];
for (const [category, bookmarks] of Object.entries(sortedBookmarks)) {
const newFolder = await browser.bookmarks.create({
parentId: toolbarFolderId,
title: category
});
for (const bookmark of bookmarks) {
const creationPromise = browser.bookmarks.create({
parentId: newFolder.id,
title: bookmark.name,
url: bookmark.url
});
creationPromises.push(creationPromise);
}
}
await Promise.all(creationPromises);
logDebug("New bookmarks added to the browser.");
} catch (error) {
console.error("Error setting bookmarks in the browser:", error);
throw error; // Handle errors appropriately in the calling context
}
}
async function initializeUI() {
const sortButton = safelyGetElementById("saveBookmarksButton");
const indicator = safelyGetElementById("waitingIndicator");
const errorContainer = safelyGetElementById("errorContainer");
sortButton.addEventListener("click", async () => {
indicator.style.display = "block";
errorContainer.style.display = "none";
try {
const bookmarkItems = await browser.bookmarks.getTree();
const simplifiedBookmarks = bookmarkItems.flatMap(item =>
item.children ? item.children.flatMap(simplifyBookmarkData) : simplifyBookmarkData(item))
.map(simplifiedBookmark => ({
...simplifiedBookmark,
tags: ["Bookmarks Menu"],
}));
const sortedBookmarks = await sendBookmarksToServer(simplifiedBookmarks);
if (!sortedBookmarks || typeof sortedBookmarks !== "object") {
throw new Error("Received invalid response from the server.");
}
logDebug("Bookmarks successfully sent to the server and received response.");
await setBookmarksInBrowser(sortedBookmarks);
logDebug("Bookmarks added to the browser successfully.");
} catch (error) {
console.error("Error processing bookmarks:", error);
errorContainer.textContent = error.message || "An unexpected error occurred. Please try again later.";
errorContainer.style.display = "block";
} finally {
indicator.style.display = "none";
}
});
}
document.addEventListener("DOMContentLoaded", initializeUI);