-
Notifications
You must be signed in to change notification settings - Fork 5
/
bookmarks.js
112 lines (94 loc) · 3.03 KB
/
bookmarks.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
var addTable = function(storage) {
let bookmarks = storage.bookmarks;
let $tbody = $("#tabledata").find("tbody");
let record = $("#record").html();
var index = 1;
for(var i=0,len=bookmarks.length; i<len; i++){
var tr = record;
tr = tr.replace(/{index}/g, index++);
tr = tr.replace(/{valueIndex}/g, i);
tr = tr.replace(/{name}/g, bookmarks[i].name);
tr = tr.replace(/{url}/g, bookmarks[i].url);
tr = tr.replace(/{time}/g, bookmarks[i].updateTime);
$tbody.append(tr);
}
};
var deleteCheckedItems = function(bookmarks) {
// Get selected items
var deleteList = [];
$(".select:checked").toArray().forEach(function(item, index){
deleteList.push(item.value);
});
// Filter data
bookmarks = bookmarks.filter(function(item, index){
return !deleteList.includes(index.toString());
});
setStorage({bookmarks: bookmarks});
};
var saveModification = function(bookmarks, modifingIndex) {
bookmarks[modifingIndex] = $.editor.get();
setStorage({bookmarks: bookmarks});
};
var setStorage = function(data) {
chrome.storage.sync.set(data, function() {
if (chrome.runtime.error) { console.log("Runtime error."); }
else {
// Refresh
history.go(0);
}
});
};
var hideEditor = function() {
document.getElementById("jsoneditor1").style.display = "none";
};
var showEditor = function() {
document.getElementById("jsoneditor1").style.display = "block";
window.scrollTo(0, $("#jsoneditor1").offset().top);
$("#saveBtn").css("display", "inline-block");
};
var eventBinding = function(storage) {
var bookmarks = storage.bookmarks;
var modifingIndex;
// selectAll checkbox
$("#selectAll").on("change", function(e){
$(".select").prop("checked", e.target.checked);
});
// Save modification button
$("#saveBtn").on("click", function(e){
saveModification(bookmarks, modifingIndex);
});
// Delete button
$("#deleteBtn").on("click", function(e){
deleteCheckedItems(bookmarks);
});
// Modify button
$(".modify").on("click", function(e){
modifingIndex = this.value;
var bookmark = bookmarks[this.value];
$.editor.set(bookmark);
showEditor();
});
// Ctrl+S to save modification
$(window).on("keydown", function(e){
if(e.ctrlKey && e.keyCode == 'S'.charCodeAt(0) && modifingIndex){
e.preventDefault();
saveModification(bookmarks, modifingIndex);
}
});
};
document.addEventListener('DOMContentLoaded', function () {
// get chrome.storage
chrome.storage.sync.get(null, function(storage) {
if (chrome.runtime.error) {
console.log("Runtime error.");
} else {
// Render bookmarks
addTable(storage);
// Event binding
eventBinding(storage);
}
//json editor
$.editor = new JSONEditor(document.getElementById("jsoneditor1"), {});
hideEditor();
});
});