Skip to content

Instantly share code, notes, and snippets.

@brito
Last active February 1, 2017 21:24
Show Gist options
  • Save brito/6da08cf82905301c7710a134f100d5cb to your computer and use it in GitHub Desktop.
Save brito/6da08cf82905301c7710a134f100d5cb to your computer and use it in GitHub Desktop.
Bookmarks chrome extension
<input name=query placeholder="search">
<menu></menu>
<script>
console.clear();
document.addEventListener('DOMContentLoaded', function(){
chrome.bookmarks.getTree(function(results){
// 1. index browser bookmarks
var bookmarks = results[0].children.reduce(index, {});
console.info(bookmarks);
});
});
/* index */
function index(Bookmarks, bookmark){
if (bookmark.url) {
var url = document.createElement('a');
url.href = bookmark.url;
'protocol host port pathname search hash'
.split(/\W/)
.forEach(function(key){
bookmark[key] = url[key];
});
bookmark.domain =
(bookmark.host.match(/[^\.]+\.\w+$/) || [''])[0];
}
for (var key in bookmark){
var value = bookmark[key];
if (value instanceof Array)
// flatten
Bookmarks = value.reduce(index, Bookmarks);
else
Bookmarks[key] = archive(Bookmarks[key], value, bookmark);
}
return Bookmarks;
}
/* archive */
function archive(context, value, item){
context = context || {};
context[value] = value in context ?
[item].concat(context[value])
: item;
return context;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment