-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarks.html
333 lines (301 loc) · 9.62 KB
/
bookmarks.html
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!DOCTYPE html>
<html>
<head>
<!--
Personal Web Bookmarks, w/ client-side encryption
https://github.com/steve-vincent/web-bookmarks
-->
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script>
//========================================
var GUNSERVER = Gun('http://YOUR_GUN_SERVER:8765/gun');
//========================================
var gun = Gun(GUNSERVER);
console.log("Gun(" + GUNSERVER + ")");
</script>
<title>Bookmarks</title>
<meta charset=utf-8 />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="referrer" content="no-referrer">
<!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<script src="bootstrap-treeview.js"></script>
<script src="jsonQ.min.js"></script>
<!-- Stylesheets -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<style>
body {background: #003300; color: white;}
button {background-color: grey; color: black}
</style>
</head>
<body>
<!-- ------------------------------------------ HTML body ------------------------------------------ -->
<!-- Editor, header -->
<div style="padding-bottom:8px;">
<div style="display:flex; width:100%;">
<div style="flex-grow: 1;">
<span style="margin-left:5px; font-weight:bold;" id="localname"></span>:
<button type="button" onclick="addItem(prompt('URL to add:'));">add+</button>
</div>
<div style="flex-grow: 10; text-align:center;">
<span id="msg" style="width:100%; text-align:center; border:1px; color:#555555;">#url to add</span>
</div>
<div style="flex-grow: 1; text-align: right;">
<a href="javascript:toggleTextArea()">{...}</a>
Remember me?<input type="checkbox" id="remember">
</div>
</div>
<div id="editorDiv" style="display:none;">
<div style="text-align:center">
<a href="#" onclick="localStorage.clear(); window.location.reload();">reload</a> -
<button type="button" onclick="bkmkobj=JSON.parse($('#editor').val()); saveObj();">save</button>
</div>
<textarea id="editor" style="width:100%; height:400px; color:black;"></textarea>
</div>
</div>
<!-- Bootstrap TreeView Built Here -->
<div id="tree"></div>
<!-- Bootstrap TreeView Built Here -->
<div id="server" style="float:right;padding-right:10px;font-size:80%;"></div>
<script>
// GLOBALS
var rootids; //#tree root ids
var BKMKID;
var bkmkobj; //in-memory data
var pwd; //in-memory data
var expandedset = new Set(); //in-memory data
//Init UI
$("#localname").html(BKMKID);
$("#server").html("<a target='_new' href='" + GUNSERVER + "'>" + GUNSERVER + "</a>");
if (localStorage.getItem("pwd-"+BKMKID)) document.getElementById('remember').checked=true;
$("#remember").change(function() {
if(this.checked) {
console.log("remember me");
localStorage.setItem("pwd-"+BKMKID, pwd);
}
else{
console.log("forget me");
localStorage.removeItem("pwd-"+BKMKID);
}
});
function toggleTextArea() {
if ($('#editorDiv').css("display")=='block'){
console.log('*close editor');
$('#editorDiv').css({'display': 'none'});
}
else{
console.log('*open editor');
$('#editorDiv').css({'display': 'block'});
var prettify = JSON.stringify(bkmkobj).replace(/},/g,"},\n").replace(/],/g,"\n],").replace(/\[/g,"[\n"); //prettify
$('#editor').val(prettify); //edit
}
}
//==========================================
// Common data object & methods
//==========================================
//GUN: Encrypt & Put Data
function saveObj() {
console.log("saveObj() - gun.put: " + BKMKID);
var encdata = this.encrypt(JSON.stringify(bkmkobj));
gun.get('bkmk-'+BKMKID).get('data').put( encdata, drawMenuTree );
return true;
};
//================ password, decrypt, encrypt ====================
function getPassword() {
if (pwd) return pwd;
pwd = localStorage.getItem("pwd-"+BKMKID);
if (!pwd){
pwd = prompt('Your Password:');
if (document.getElementById('remember').checked) localStorage.setItem("pwd-"+BKMKID, pwd);
}
return pwd;
}
function encrypt(str) {
return CryptoJS.AES.encrypt(str, this.getPassword()).toString();
}
function decrypt(str) {
try{
return CryptoJS.AES.decrypt(str, this.getPassword()).toString(CryptoJS.enc.Utf8);
}
catch(err){
console.log("BAD PASSWORD?");
pwd = null; //forget
localStorage.removeItem("pwd-"+BKMKID);
return null;
}
}
//================ add, cut, paste ====================
//add
function addItem(url){
if (url){
if (!bkmkobj) bkmkobj = [];
bkmkobj.push({'href':url, 'text':url});
saveObj();
$("#msg").html('added.');
return true;
}
}
//delete, cut, paste
function cutItem(name, moveto){
var cutobj = jsonQ(bkmkobj);
var cutpath = cutobj.find("text", function (){return this==name}).path();
//rename
if (moveto.charAt(0)=='#'){
cutobj.setPathValue(cutpath, moveto.substring(1));
console.log(cutobj);
}
else {
//cut
cutpath.pop();
var elx = cutobj.pathValue(cutpath); //cut for paste
//delete
var cutidx = cutpath.pop();
var cuttree = cutobj.pathValue(cutpath);
cuttree.splice(cutidx,1);
cutobj.setPathValue(cutpath, cuttree);
//paste
if (moveto){
var pobj = jsonQ(bkmkobj);
//to root
if (moveto=='-'){
bkmkobj.push(elx);
}
//to folder
else{
try {
var pastepath = pobj.find("text", function (){return this==moveto}).path();
//error, else existing folder...
pastepath.pop();
var elp = pobj.pathValue(pastepath); //folder
elp.nodes.push(elx); //append
pobj.setPathValue(pastepath, elp);
}
catch(err){ //new folder
bkmkobj.push({"text": moveto, "nodes": [elx]});
}
}
}
}
saveObj();
return true;
}
//==========================================
//==========================================
// Update Bootstrap-Treeview
//https://github.com/jonmiles/bootstrap-treeview
//==========================================
//==========================================
function drawMenuTree() {
console.log('drawMenuTree()...');
if (!bkmkobj) {
return;
}
rootids = []; //root folders
jQuery(bkmkobj).each(function (i){
rootids.push(bkmkobj[i].text);
});
//BUG: loading issue?
if ($('#tree').treeview){
$('#tree').treeview({
data: bkmkobj,
enableLinks: true,
backColor: '#000000',
onhoverColor: '#333366',
borderColor: '#333333',
expandIcon: 'glyphicon glyphicon-chevron-right',
collapseIcon: 'glyphicon glyphicon-chevron-down',
showCheckbox: true,
checkedIcon: "glyphicon glyphicon-option-horizontal",
uncheckedIcon: "glyphicon glyphicon-option-horizontal"
});
}
else{
setTimeout(drawTreeMenu, 1000);
return true;
}
//on menu-button
$('#tree').on('nodeChecked', function(event, data) { //delete, move
var moveto = prompt(data.text + "\n\nTo DELETE, Press 'ok'.\n\nTo MOVE, type <folder name>\n\nTo RENAME, type #<newname>\n\nTo move TOP, enter '-'");
if (moveto!=null) {cutItem(data.text, moveto);};
$('#tree').treeview('uncheckAll', { silent: true });
});
//on select
$('#tree').on('nodeSelected', function(event, data) {//select->toggle
if (!data.href){
$('#tree').treeview('toggleNodeExpanded', data.nodeId);
}
else{
window.open(data.href);
}
$('#tree').treeview('toggleNodeSelected', data.nodeId); //de-select
});
//on expand
$('#tree').on('nodeExpanded', function(event, data) {
if (rootids.includes(data.text)) { //if is root
var opened = $('#tree').treeview('getExpanded');
jQuery(opened).each(function (i){ //close others
if (opened[i].text != data.text){
$('#tree').treeview('collapseNode', opened[i]);
}
});
}
expandedset.add(data.nodeId);
saveExpanded();
});
//on collapse
$('#tree').on('nodeCollapsed', function(event, data) {
expandedset.delete(data.nodeId);
saveExpanded();
});
//re-expand
$('#tree').treeview('collapseAll', { silent: true });
var expandedArr = Array.from(expandedset);
for (var nid in expandedArr){
try{
$('#tree').treeview('expandNode', expandedArr[nid]);
}
catch{ //forget not found
expandedset.delete(expandedArr[nid]);
saveExpanded();
}
}
}
function saveExpanded(){
localStorage.setItem(BKMKID+"-expanded", JSON.stringify(Array.from(expandedset)));
}
//============== end treeview =====================
//==========================================
//==========================================
//page ready
//==========================================
//==========================================
window.onload = function() {
console.log('document.ready...');
//bookmarks?id=
var qs = new URLSearchParams(window.location.search);
BKMKID = qs.get('id');
if (!BKMKID) {
//prompt for id
BKMKID=prompt("To continue, enter bookmarks name:");
if (BKMKID) location.href = location.href + '?id=' + BKMKID;
}
else {
// GUN: Decrypt & Get Data
console.log('wait for gun.get.on: bkmk-'+BKMKID + ".data");
gun.get('bkmk-'+BKMKID).get('data').on(function(data, key){
console.log("got: " + key);
var decdata = decrypt(data);
bkmkobj = JSON.parse(decdata);
drawMenuTree();
//add from bookmarklet: bookmarks?id=<id>#<new url>
if (window.location.hash){
var newurl = window.location.hash.substr(1);
$("#msg").html("<a href='#' onclick='addItem(\"" + newurl + "\")'>Add " + newurl + "</a>");
}
});
}
};
</script>
</body>
</html>