-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (155 loc) · 5.57 KB
/
index.js
File metadata and controls
193 lines (155 loc) · 5.57 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
const fs = require("fs").promises;
const path = require("path");
const os = require("os");
async function main() {
try {
const data = await readJson();
const html = convertJsonToHtml(data);
await writeHtml(html);
console.log("Done!");
} catch (error) {
console.error("An error occurred:", error);
}
}
async function readJson() {
console.log("Reading JSON...");
const filename = "StorableSidebar.json";
let libraryPath;
if (process.platform === "win32") {
const arcRootParentPath = path.join(os.homedir(), "AppData", "Local", "Packages");
const arcRootPaths = (await fs.readdir(arcRootParentPath))
.filter((f) => f.startsWith("TheBrowserCompany.Arc"))
.map((f) => path.join(arcRootParentPath, f));
if (arcRootPaths.length !== 1) {
throw new Error("Arc installation directory not found");
}
libraryPath = path.join(arcRootPaths[0], "LocalCache", "Local", "Arc", filename);
} else {
libraryPath = path.join(os.homedir(), "Library", "Application Support", "Arc", filename);
}
let data = {};
try {
data = JSON.parse(await fs.readFile(filename, "utf-8"));
console.log(`> Found ${filename} in current directory.`);
} catch (error) {
try {
data = JSON.parse(await fs.readFile(libraryPath, "utf-8"));
console.log(`> Found ${filename} in Library directory.`);
} catch (error) {
console.error('> File not found. Look for the "StorableSidebar.json" file within the "~/Library/Application Support/Arc/" folder.');
throw new Error("File not found");
}
}
return data;
}
function convertJsonToHtml(jsonData) {
console.log("convertJsonToHtml", jsonData);
const containers = jsonData.sidebar.containers;
console.log("containers", containers);
const topAppsContainerIDs = containers.findIndex((i) => "topAppsContainerIDs" in i);
// console.log("containers[topAppsContainerIDs]", containers[topAppsContainerIDs]);
// console.log("containers[topAppsContainerIDs].spaces", containers[topAppsContainerIDs].spaces);
// console.log("containers[topAppsContainerIDs].items", containers[topAppsContainerIDs].items);
const spaces = getSpaces(containers[topAppsContainerIDs].spaces);
const items = containers[topAppsContainerIDs].items;
const bookmarks = convertToBookmarks(spaces, items);
const htmlContent = convertBookmarksToHtml(bookmarks);
return htmlContent;
}
function getSpaces(spaces) {
console.log("Getting spaces...");
console.log(spaces);
const spacesNames = { pinned: {}, unpinned: {} };
let spacesCount = 0;
let n = 1;
for (const space of spaces) {
let title = space.title || `Space ${n++}`;
if (typeof space === "object") {
const containers = space.newContainerIDs;
for (let i = 0; i < containers.length; i++) {
if (typeof containers[i] === "object") {
if ("pinned" in containers[i]) {
spacesNames.pinned[containers[i + 1]] = title;
} else if ("unpinned" in containers[i]) {
spacesNames.unpinned[containers[i + 1]] = title;
}
}
}
spacesCount++;
}
}
console.log(`> Found ${spacesCount} spaces.`);
return spacesNames;
}
function convertToBookmarks(spaces, items) {
console.log("Converting to bookmarks...");
const bookmarks = { bookmarks: [] };
let bookmarksCount = 0;
const itemDict = Object.fromEntries(items.filter((item) => typeof item === "object").map((item) => [item.id, item]));
function recurseIntoChildren(parentId) {
const children = [];
for (const [itemId, item] of Object.entries(itemDict)) {
if (item.parentID === parentId) {
if (item.data && item.data.tab) {
children.push({
title: item.title || item.data.tab.savedTitle || "",
type: "bookmark",
url: item.data.tab.savedURL || "",
});
bookmarksCount++;
} else if (item.title) {
const childFolder = {
title: item.title,
type: "folder",
children: recurseIntoChildren(itemId),
};
children.push(childFolder);
}
}
}
return children;
}
for (const [spaceId, spaceName] of Object.entries(spaces.pinned)) {
const spaceFolder = {
title: spaceName,
type: "folder",
children: recurseIntoChildren(spaceId),
};
bookmarks.bookmarks.push(spaceFolder);
}
console.log(`> Found ${bookmarksCount} bookmarks.`);
return bookmarks;
}
function convertBookmarksToHtml(bookmarks) {
console.log("Converting bookmarks to HTML...");
let htmlStr = `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>`;
function traverseDict(d, level = 1) {
const indent = "\t".repeat(level);
for (const item of d) {
if (item.type === "folder") {
htmlStr += `\n${indent}<DT><H3>${item.title}</H3>`;
htmlStr += `\n${indent}<DL><p>`;
traverseDict(item.children, level + 1);
htmlStr += `\n${indent}</DL><p>`;
} else if (item.type === "bookmark") {
htmlStr += `\n${indent}<DT><A HREF="${item.url}">${item.title}</A>`;
}
}
}
traverseDict(bookmarks.bookmarks);
htmlStr += "\n</DL><p>";
console.log("> HTML converted.");
return htmlStr;
}
async function writeHtml(htmlContent) {
console.log("Writing HTML...");
const currentDate = new Date().toISOString().split("T")[0].replace(/-/g, "_");
const outputFile = `arc_bookmarks_${currentDate}.html`;
await fs.writeFile(outputFile, htmlContent, "utf-8");
console.log(`> HTML written to ${outputFile}.`);
}
main();