mirror of
https://github.com/XFox111/TabsAsideExtension.git
synced 2026-04-22 07:58:01 +03:00
feat: add netscape bookmark import/export #203
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { getCollections, saveCollections } from "@/features/collectionStorage";
|
||||
import { sendMessage } from "@/utils/messaging";
|
||||
import parse from "node-bookmarks-parser";
|
||||
import { Bookmark } from "node-bookmarks-parser/build/interfaces/bookmark";
|
||||
import convertBookmarks from "./convertBookmarks";
|
||||
|
||||
export default async function importBookmarks(): Promise<number | null>
|
||||
{
|
||||
const element: HTMLInputElement = document.createElement("input");
|
||||
element.style.display = "none";
|
||||
element.hidden = true;
|
||||
element.type = "file";
|
||||
element.accept = ".html";
|
||||
|
||||
document.body.appendChild(element);
|
||||
element.click();
|
||||
|
||||
await new Promise(resolve =>
|
||||
{
|
||||
const listener = () =>
|
||||
{
|
||||
element.removeEventListener("input", listener);
|
||||
resolve(null);
|
||||
};
|
||||
element.addEventListener("input", listener);
|
||||
});
|
||||
|
||||
if (!element.files || element.files.length < 1)
|
||||
return null;
|
||||
|
||||
const file: File = element.files[0];
|
||||
const content: string = await file.text();
|
||||
|
||||
document.body.removeChild(element);
|
||||
|
||||
try
|
||||
{
|
||||
const bookmarks: Bookmark[] = parse(content);
|
||||
const [data, graphics, tabCount] = convertBookmarks(bookmarks);
|
||||
const [collections, cloudIssues] = await getCollections();
|
||||
|
||||
await saveCollections([...data, ...collections], cloudIssues === null, graphics);
|
||||
sendMessage("refreshCollections", undefined);
|
||||
|
||||
return tabCount;
|
||||
}
|
||||
catch (error)
|
||||
{
|
||||
console.error("Failed to parse bookmarks file", error);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user