Taranveer (Taran) Bains

Taranveer (Taran) Bains

// Name: Chrome bookmarks (with folder support)
// Author: Taran "tearing it up" Bains
// Description: Navigate your chrome bookmarks (even if they are in folders)!
// GitHub: @tearingitup786
import "@johnlindquist/kit";
// Reading the Chrome bookmarks file from the user's system
// Maybe allow for a choice of different browsers?
let bookmarks = await readFile(
home("Library/Application Support/Google/Chrome/Default/Bookmarks"),
);
bookmarks = JSON.parse(bookmarks);
bookmarks = bookmarks.roots.bookmark_bar.children;
// Initializing an array to keep track of the navigation history
let historyStack = [];
const CUSTOMSEPARATOR = "-CUSTOMSEPARATOR-";
// Loop to handle user interaction and navigation within bookmarks
while (true) {
const getBookmark = ({ name, url, type }) => {
if (type === "folder") {
return {
name: `🗂️ ${name}`,
description: "⤵️ Select directory",
value: `folder${CUSTOMSEPARATOR}${name}`, // Encoding type and name for folders
};
}
return {
name: `⛓️ ${name}`,
description: url,
type,
value: `link${CUSTOMSEPARATOR}${url}`, // Encoding type and URL for links
};
};
// Generating options based on current level of bookmarks
let options = bookmarks.map(getBookmark);
// Adding a "go back" option if there is history in the stack
if (historyStack.length > 0) {
options = [
{ name: "..", description: "Go back", value: "go-back" },
...options,
];
}
const lastSelection = await arg("Select A Bookmark!", options);
if (lastSelection === "go-back") {
bookmarks = historyStack.pop();
continue;
}
// Splitting the value to determine the type and actual value (name or URL)
const [type, value] = lastSelection.split(CUSTOMSEPARATOR);
if (type === "folder") {
// push the old bookmarks into the stack
historyStack.push(bookmarks);
bookmarks = bookmarks.find((bookmark) => bookmark.name === value).children;
continue;
}
if (type === "link") {
exec(`open "${value}"`);
break;
}
console.log("Unknown type", type);
}
sh(bookmarks);
bookmarks = bookmarks.find((bookmark) => bookmark.name === value).children;
continue;
}
if (type === "link") {
exec(`open "${value}"`);
break;
}
console.log("Unknown type", type);
}