Skip to content

Instantly share code, notes, and snippets.

@ruucm
Forked from keikoro/FF_HTMLbookmarks_toCSV.js
Created October 16, 2020 16:29
Show Gist options
  • Save ruucm/83fdd36c862a49ba35475c75aa360417 to your computer and use it in GitHub Desktop.
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
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);
})();
@ruucm
Copy link
Author

ruucm commented Oct 16, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment