-
-
Save manustays/d54503ad75484c598b41ebd9c4bc3bec to your computer and use it in GitHub Desktop.
Export browser bookmarks, import them in Notion as a database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function downloadCsv() { | |
| const anchorPath = ":scope > a"; | |
| const defPath = [":scope > dl", ":scope > dt"].join(","); | |
| const headingPath = [ | |
| ":scope > h1", | |
| ":scope > h2", | |
| ":scope > h3", | |
| ":scope > h4", | |
| ":scope > h5", | |
| ":scope > h6", | |
| ].join(","); | |
| const dateF = new Intl.DateTimeFormat("en-US", { dateStyle: "medium" }); | |
| const hourF = new Intl.DateTimeFormat("en-US", { timeStyle: "short" }); | |
| function getDate(anchor) { | |
| const stringDate = anchor.getAttribute("add_date"); | |
| const date = stringDate ? new Date(Number(stringDate) * 1000) : new Date(); | |
| return `${dateF.format(date)} ${hourF.format(date)}`; | |
| } | |
| function getNewTags(tags, def) { | |
| const heading = def.querySelector(headingPath); | |
| if (!heading) return tags; | |
| return [...new Set([...tags, heading.innerText])]; | |
| } | |
| const createBookmark = (anchor, tags) => ({ | |
| Name: anchor.innerText, | |
| Created: getDate(anchor), | |
| Tags: tags.join(", "), | |
| Url: encodeURI(anchor.href), | |
| }); | |
| function getBookmarks(def, tags = []) { | |
| return [...def.querySelectorAll(defPath)].flatMap((child) => { | |
| const anchor = child.querySelector(anchorPath); | |
| return anchor | |
| ? createBookmark(anchor, tags) | |
| : getBookmarks(child, getNewTags(tags, child)); | |
| }); | |
| } | |
| function arrayToCsv(array) { | |
| const titles = ["Name", "Created", "Tags", "Url"].map((t) => | |
| JSON.stringify(t) | |
| ); | |
| const entries = array.map((e) => | |
| [e.Name, e.Created, e.Tags, e.Url] | |
| .map((f) => `"${f.replace(/"/g, `""`)}"`) | |
| .join(",") | |
| ); | |
| return `${titles.join(",")}\n${entries.join("\n")}`; | |
| } | |
| function downloadString(text) { | |
| const a = document.createElement("a"); | |
| a.href = window.URL.createObjectURL(new Blob([text], { type: "text/csv" })); | |
| a.download = "Bookmarks.csv"; | |
| a.click(); | |
| window.setTimeout(() => window.URL.revokeObjectURL(a.href)); | |
| } | |
| const bookmarks = getBookmarks(document.body); | |
| const csvString = arrayToCsv(bookmarks); | |
| downloadString(csvString); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment