forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
196 lines (182 loc) · 5.7 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
// Copyright 2021 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Search the bookmarks when entering the search keyword.
$('#search').change(function () {
$('#bookmarks').empty();
dumpBookmarks($('#search').val());
});
// Traverse the bookmark tree, and print the folder and nodes.
function dumpBookmarks(query) {
const bookmarkTreeNodes = chrome.bookmarks.getTree(function (
bookmarkTreeNodes
) {
$('#bookmarks').append(dumpTreeNodes(bookmarkTreeNodes, query));
});
}
function dumpTreeNodes(bookmarkNodes, query) {
const list = $('<ul>');
for (let i = 0; i < bookmarkNodes.length; i++) {
list.append(dumpNode(bookmarkNodes[i], query));
}
return list;
}
function dumpNode(bookmarkNode, query) {
let span = '';
if (bookmarkNode.title) {
if (query && !bookmarkNode.children) {
if (
String(bookmarkNode.title.toLowerCase()).indexOf(query.toLowerCase()) ==
-1
) {
return $('<span></span>');
}
}
const anchor = $('<a>');
anchor.attr('href', bookmarkNode.url);
anchor.text(bookmarkNode.title);
/*
* When clicking on a bookmark in the extension, a new tab is fired with
* the bookmark url.
*/
anchor.click(function () {
chrome.tabs.create({ url: bookmarkNode.url });
});
span = $('<span>');
const options = bookmarkNode.children
? $('<span>[<a href="#" id="addlink">Add</a>]</span>')
: $(
'<span>[<a id="editlink" href="#">Edit</a> <a id="deletelink" ' +
'href="#">Delete</a>]</span>'
);
const edit = bookmarkNode.children
? $(
'<table><tr><td>Name</td><td>' +
'<input id="title"></td></tr><tr><td>URL</td><td><input id="url">' +
'</td></tr></table>'
)
: $('<input>');
// Show add and edit links when hover over.
span
.hover(
function () {
span.append(options);
$('#deletelink').click(function (event) {
console.log(event);
$('#deletedialog')
.empty()
.dialog({
autoOpen: false,
closeOnEscape: true,
title: 'Confirm Deletion',
modal: true,
show: 'slide',
position: {
my: 'left',
at: 'center',
of: event.target.parentElement.parentElement
},
buttons: {
'Yes, Delete It!': function () {
chrome.bookmarks.remove(String(bookmarkNode.id));
span.parent().remove();
$(this).dialog('destroy');
},
Cancel: function () {
$(this).dialog('destroy');
}
}
})
.dialog('open');
});
$('#addlink').click(function (event) {
edit.show();
$('#adddialog')
.empty()
.append(edit)
.dialog({
autoOpen: false,
closeOnEscape: true,
title: 'Add New Bookmark',
modal: true,
show: 'slide',
position: {
my: 'left',
at: 'center',
of: event.target.parentElement.parentElement
},
buttons: {
Add: function () {
edit.hide();
chrome.bookmarks.create({
parentId: bookmarkNode.id,
title: $('#title').val(),
url: $('#url').val()
});
$('#bookmarks').empty();
$(this).dialog('destroy');
window.dumpBookmarks();
},
Cancel: function () {
edit.hide();
$(this).dialog('destroy');
}
}
})
.dialog('open');
});
$('#editlink').click(function (event) {
edit.show();
edit.val(anchor.text());
$('#editdialog')
.empty()
.append(edit)
.dialog({
autoOpen: false,
closeOnEscape: true,
title: 'Edit Title',
modal: true,
show: 'fade',
position: {
my: 'left',
at: 'center',
of: event.target.parentElement.parentElement
},
buttons: {
Save: function () {
edit.hide();
chrome.bookmarks.update(String(bookmarkNode.id), {
title: edit.val()
});
anchor.text(edit.val());
options.show();
$(this).dialog('destroy');
},
Cancel: function () {
edit.hide();
$(this).dialog('destroy');
}
}
})
.dialog('open');
});
options.fadeIn();
},
// unhover
function () {
options.remove();
}
)
.append(anchor);
}
const li = $(bookmarkNode.title ? '<li>' : '<div>').append(span);
if (bookmarkNode.children && bookmarkNode.children.length > 0) {
li.append(dumpTreeNodes(bookmarkNode.children, query));
}
return li;
}
document.addEventListener('DOMContentLoaded', function () {
dumpBookmarks();
});