forked from xi/bookmark-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.js
42 lines (35 loc) · 989 Bytes
/
stack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use struct';
var render = function(element, items) {
element.innerHTML = '';
items.forEach(function(item) {
var icon = document.createElement('img');
icon.className = 'icon';
if (chrome && typeof browser === 'undefined') {
icon.src = 'chrome://favicon/' + item.url;
} else {
var rootUrl = item.url.split('/').slice(0, 3).join('/');
icon.src = rootUrl + '/favicon.ico';
}
var a = document.createElement('a');
a.id = item.id;
a.href = item.url;
a.title = item.url;
a.textContent = item.title || item.url;
a.prepend(icon);
var li = document.createElement('li');
li.appendChild(a);
// reverse
element.prepend(li);
});
};
getBookmarks(function(bookmarks) {
render(document.querySelector('#bookmarks'), bookmarks);
});
document.addEventListener('click', function(event) {
if (event.target.tagName === 'A') {
event.preventDefault();
popBookmark(event.target.id, function(bookmark) {
chrome.tabs.create({url: bookmark.url});
});
}
});