-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
230 lines (193 loc) · 6.02 KB
/
popup.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const rootId = '0';
const bbarId = '1';
const defaultWidth = 180;
const maxWidth = 500;
var selected = null;
function itemClicked(ev) {
if (selected) {
clearMenu();
} else {
switch (ev.button) {
case 0:
if (ev.ctrlKey && ev.shiftKey)
openBookmark(this, createTabSelected);
else if (ev.ctrlKey)
openBookmark(this, createTab);
else
openBookmark(this, reuseTab);
break;
case 1:
openBookmark(this, createTab);
break;
case 2:
popupMenu(this, ev);
break;
}
}
}
function openBookmark(a, loadUrl) {
if (a.href) {
loadUrl(a.href);
} else {
var data = JSON.parse(a.data);
loadFolder(data.id);
}
clearMenu();
}
function createTab(url) {
chrome.tabs.create({url: url, selected: false});
}
function createTabSelected(url) {
chrome.tabs.create({url: url, selected: true});
window.close();
}
function reuseTab(url) {
chrome.tabs.getSelected(null, function(tab) {
if (isJsURL(url))
chrome.tabs.executeScript(tab.id, {code: getJsCode(url)});
else
chrome.tabs.update(tab.id, {url: url});
window.close();
});
}
function isJsURL(url) {
return url.substr(0, 11) == 'javascript:';
}
function getJsCode(url) {
return unescape(url.substr(11));
}
function popupMenu(a, ev) {
var blist = document.getElementById('blist');
var menu = document.getElementById('menu');
var menuOpenTab = document.getElementById('menuOpenTab');
var menuOpenAll = document.getElementById('menuOpenAll');
blist.className = 'obscured';
menu.className = a.href ? 'bookmark' : 'folder';
var x = Math.min(ev.clientX,
document.body.clientWidth - menu.clientWidth - 4);
var y = Math.min(ev.clientY,
document.body.clientHeight - menu.clientHeight - 3);
menu.style.left = x + 'px';
menu.style.top = y + 'px';
menu.style.visibility = 'visible';
selected = a;
a.className = 'selected';
}
function clearMenu() {
var blist = document.getElementById('blist');
var menu = document.getElementById('menu');
blist.className = 'selectable';
menu.style.top = '0';
menu.style.left = '0';
menu.style.visibility = 'hidden';
if (selected) {
selected.className = '';
selected = null;
}
}
function openSelected() {
openBookmark(selected, reuseTab);
}
function openSelectedNewTab() {
openBookmark(selected, createTab);
}
function openSelectedChildren() {
var data = JSON.parse(selected.data);
chrome.bookmarks.getChildren(data.id, function(children) {
children.forEach(function(bookmark) {
if (bookmark.url)
createTab(bookmark.url);
});
});
}
function renameSelected() {
var data = JSON.parse(selected.data);
var item = selected;
var input = document.createElement('input');
input.value = data.title;
item.replaceChild(input, item.lastChild);
item.onmouseup = undefined;
input.focus();
input.select();
input.addEventListener('keydown', function(event) {
if (event.keyCode === 13) {
data.title = input.value;
item.data = JSON.stringify(data);
chrome.bookmarks.update(data.id, {title: data.title});
item.replaceChild(document.createTextNode(data.title), input);
}
item.onmouseup = itemClicked;
});
clearMenu();
}
function deleteSelected() {
var blist = document.getElementById('blist');
var data = JSON.parse(selected.data);
blist.removeChild(selected.parentNode);
chrome.bookmarks.remove(data.id);
clearMenu();
}
function makeSelectedDefault() {
var data = JSON.parse(selected.data);
localStorage.startId = data.id;
}
function createBookmarkItem(b, parent) {
var icon, title = parent ? '..' : b.title;
var li = document.createElement('li');
var a = document.createElement('a');
a.data = JSON.stringify({id: b.id, title: title});
a.onmouseup = itemClicked;
if (b.url) {
a.href = b.url;
if (isJsURL(b.url))
icon = 'icons/script.png';
else if (localStorage[b.url])
icon = localStorage[b.url];
else
icon = 'http://getfavicon.appspot.com/' + b.url;
} else {
icon = parent ? 'icons/folder-open.png' : 'icons/folder.png';
}
a.innerHTML = '<img class="favicon" src="'+icon+'" />' + title;
li.appendChild(a);
return li;
}
function loadFolder(id) {
var blist = document.getElementById('blist');
var curLength = blist.children.length;
var append = function(b) {
blist.appendChild(createBookmarkItem(b));
// Unfortunately this is a guess based on a 13px font in the CSS.
var approxWidth = Math.floor(b.title.length * 7);
document.body.style.width = Math.min(maxWidth,
Math.max(document.body.clientWidth, approxWidth)) + 'px';
};
blist.innerHTML = '';
document.body.style.width = defaultWidth + 'px';
if (id === rootId || id === bbarId) {
chrome.bookmarks.getChildren(bbarId, function(children) {
children.forEach(append);
blist.appendChild(document.createElement('hr'));
chrome.bookmarks.getChildren(rootId, function(children) {
children.forEach(function(b) {
if (b.id !== bbarId)
append(b);
});
});
});
} else {
chrome.bookmarks.get(id, function(b) {
chrome.bookmarks.get(b[0].parentId, function(p) {
blist.appendChild(createBookmarkItem(p[0], true));
blist.appendChild(document.createElement('hr'));
chrome.bookmarks.getChildren(id, function(children) {
children.forEach(append);
});
});
});
}
blist.focus();
}
function init() {
loadFolder(localStorage.startId || rootId);
}