-
-
Save ruucm/83fdd36c862a49ba35475c75aa360417 to your computer and use it in GitHub Desktop.
JavaScript bookmarklet for converting HTML-formatted Firefox bookmarks into a downloadable CSV file
This file contains 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
javascript:(function(){ | |
/* escape quotes and commas in contents to be comma-separated */ | |
function wrapCsvContents(content) { | |
if (typeof(content) === 'string') { | |
if (content.replace(/ /g, '').match(/[\s,"]/)) { | |
return '"' + content.replace(/"/g, '""') + '"'; | |
} | |
} | |
return content; | |
} | |
/* filter bookmarks */ | |
let nodeList = window.document.querySelectorAll('dt a, dt h3'); | |
let contents = Array.from(nodeList); | |
let bookmarks = contents.filter(function(a) { | |
let h = a.getAttribute('href'); | |
let t = a.getAttribute('add_date'); | |
if ((h && !h.startsWith('place')) || t === "0") { | |
return true; | |
} | |
}); | |
/* extract basic info from FF bookmarks - name, url, date added - and sort them by name ASC */ | |
let category = ''; | |
let output = contents.map(function(a) { | |
let obj = {}; | |
obj.name = a.innerHTML; | |
obj.url = a.getAttribute('href'); | |
if (!a.getAttribute('href')) { | |
category = a.innerHTML; | |
} | |
obj.category = category; | |
obj.tags = a.getAttribute('tags'); | |
let date = new Date(parseInt(a.getAttribute('add_date')) * 1000); | |
obj.added = date.toISOString(); | |
return obj; | |
}).filter(obj => obj.url).sort((a, b) => a.name.localeCompare(b.name)); | |
/* pour the bookmark contents into CSV format */ | |
let csvContent = 'name,url,category,tags,date added\r\n'; | |
output.forEach(function(obj) { | |
let row = [wrapCsvContents(obj.name), wrapCsvContents(obj.url), wrapCsvContents(obj.category), '"' + obj.tags + '"', obj.added].join(','); | |
csvContent += row + '\r\n'; | |
}); | |
/* put CSV contents into a CSV file reusing the original HTML file's date */ | |
let file = new Blob([csvContent], {type: 'text/csv;charset=utf-8;'}); | |
let getFileDate = new Date(window.document.lastModified).toISOString().slice(0, 10); | |
let downloadLink = window.document.createElement('a'); | |
downloadLink.setAttribute('href', window.URL.createObjectURL(file)); | |
downloadLink.setAttribute('download', 'bookmarks-' + getFileDate + '.csv'); | |
downloadLink.innerHTML = 'Download CSV'; | |
/* make resulting .csv downloadable via link in the HTML file */ | |
let firstElem = window.document.body.firstChild; | |
window.document.body.insertBefore(downloadLink, firstElem); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used to merge personal bookmarks (safari/chrome/raindrop) to Notion.
after uploaded to Notion, run the below scripts to remove duplicates
awk -F, '++seen[$2]==1' input.csv > output.csv