-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.js
476 lines (380 loc) · 14.1 KB
/
options.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
var alldata;
var allKeys;
var alljson = [];
document.addEventListener("DOMContentLoaded", function(){
chrome.storage.local.get(null, function(items) {
allKeys = Object.keys(items);
alldata = items;
console.log(alldata);
for( var i in items){
alljson.push(items[i]);
const article_fragment = document.getElementById('articleItem');
const article_instance = document.importNode(article_fragment.content, true);
// Add relevant content to the template
var domain = extractHostname(items[i].url);
article_instance.querySelector('.title').innerHTML = items[i].title;
// article_instance.querySelector('.author').innerHTML = items[i].author;
article_instance.querySelector('.mini-favicon').src = "https://s2.googleusercontent.com/s2/favicons?domain_url="+domain+"";
article_instance.querySelector('.url').innerHTML = domain;
article_instance.querySelector('.article').setAttribute("data-id",items[i].url);
// Append the instance ot the DOM
document.getElementById('article-scrollContainer').appendChild(article_instance);
}
alljson = JSON.stringify(alljson);
alljson = JSON.parse(alljson);
var pagehash = $(location).attr('hash').replace("#","");
if($(location).attr('hash')){
if(allKeys.includes(pagehash)){ // error handling for some mishandled hash value
displayquotes(pagehash)
}else{
displayquotes(allKeys[0]);
};
}else{
displayquotes(allKeys[0]);
}
window.addEventListener('hashchange', function() {
var hashval = $(location).attr('hash').replace("#","");
if(allKeys.includes(hashval)){ // check hash is a valid entry in data
displayquotes(hashval)
};
}, false);
$( ".article" ).click(function() {
var url = $(this).attr('data-id');
displayquotes(url);
});
$('#rightpanel').on('click',"#copy", function() {
console.log("copying?");
var quote = $(this).closest('.quoteblock').find('.portal-content-519256').text();
copyToClipboard(quote);
var el = $(this);
el.html("Copied!");
setTimeout(function() {
el.html("Copy Text");
}, 1000);
});
$('#rightpanel').on('click',"#embedLink", function() {
var title = $(".selected").find(".title").text();
var url = $(".selected").attr("data-id");
var quote = $(this).closest('.quoteblock').find('.portal-content-519256').html();
var author = $(this).closest('.quoteblock').find('.portal-author-519256').text();
const embed_fragment = document.getElementById('embed');
const embed = document.importNode(embed_fragment.content, true);
// Add relevant content to the template
embed.querySelector('.portal-author-519256').innerHTML = author;
embed.querySelector('.title-wrapper-519256').innerHTML = title;
embed.querySelector('.portal-arrow-519256').setAttribute("href", url);
embed.querySelector('.portal-content-519256').innerHTML = quote;
embed.querySelector('.mini-favicon-519256').src = "https://s2.googleusercontent.com/s2/favicons?domain_url="+url+"";
let div=document.createElement("div");
div.appendChild(embed);
copyToClipboard(div.innerHTML);
var el = $(this);
el.html("Copied!");
setTimeout(function() {
el.html("<> Embed");
}, 1000);
});
// use html2screenshot to generate canvas, copy img to clipboard
$('#rightpanel').on('click',"#copyimg", function() {
var el = $(this);
var element = $(this).closest('.quoteblock').find('.portal-container-519256');
html2canvas(element[0], {
useCORS: true
}).then((canvas) => {
canvas.toBlob(function(blob) {
console.log("Writing to clipboard");
const item = new ClipboardItem({ "image/png": blob });
navigator.clipboard.write([item]).then(
function() {
console.log("Copied to clipboard successfully!");
el.html("Copied!");
setTimeout(function() {
el.html("Copy Image");
}, 1000);
},
function(error) {
console.error("unable to write to clipboard. Error:");
console.log(error);
}
);
});
}).catch(function (error) {
console.log('oops, something went wrong!', error);
});
});
// DELETE QUOTE
$('#rightpanel').on('click',"#delete", function() {
var r = confirm("Are you sure you want to delete this quote?");
if (r == true) {
var url = $(".selected").attr("data-id");
var quoteblock = $(this).closest('.quoteblock');
var index = $(".quoteblock").index(quoteblock);
var quotes = alldata[url]["quotes"];
var removed = quotes.splice(index,1);
alldata[url]["quotes"] = quotes;
if(quotes.length == 0){ //if no quotes left then delete whole item from alldata
chrome.storage.local.remove(url, function (){
console.log("deleted "+url);
$(".selected").hide();
displayquotes(url);
});
}else{
chrome.storage.local.set(alldata, function(){
console.log("saving data");
displayquotes(url);
});
}
} else {
}
});
// DELETE ARTICLE
$('#titlebar').on('click',"#titlebar-delete", function(){
var r = confirm("Are you sure you want to delete this entire article?")
if (r == true) {
var url = $(".selected").attr("data-id");
chrome.storage.local.remove(url, function(){ // delete entire item from alldata
console.log("deleted article "+url);
$(".selected").hide();
if(url == allKeys[0]){
displayquotes(allKeys[1]);
}else{
displayquotes(allKeys[0]);
};
});
}else{
}
});
// CLEAR STORAGE
document.getElementById("clearStorage").onclick = function(){
var r = confirm("Are you sure you want to delete all citations?!");
if (r == true) {
chrome.storage.local.clear()
} else {
}
};
// Export function
document.getElementById("exportQuotes").onclick = function(){
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(alldata, null, 2));
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var a = document.createElement("a");
a.setAttribute("href", dataStr );
a.setAttribute("download", curr_date + "-" + curr_month + "-" + curr_year +"-quoteback.json");
a.click();
};
// on title/author -> trigger autosave
$( "#rightpanel" ).on("focus" , "#titlebar-author", function() {
var el = $(this);
var url = $(".selected").attr("data-id");
var object = alldata[url];
AutoSaveTitle.start(object, el ,url);
$("#titlebar-author").keypress(function(e){ // prevent return in title
if(e.which == 13){
$("#titlebar-author").blur();
}
return e.which != 13;
});
});
$( "#rightpanel" ).on("focusout" , "#titlebar-author", function() {
AutoSaveTitle.stop();
});
$( "#rightpanel" ).on("focus" , "#titlebar-title", function() {
var el = $(this);
var url = $(".selected").attr("data-id");
var object = alldata[url];
AutoSaveTitle.start(object, el ,url);
$("#titlebar-title").keypress(function(e){ // prevent return in title
if(e.which == 13){
$("#titlebar-title").blur();
}
return e.which != 13;
});
});
$( "#rightpanel" ).on("focusout" , "#titlebar-title", function() {
AutoSaveTitle.stop();
});
// on focus comment box -> trigger autosave
$( "#rightpanel" ).on("focus" , ".comment", function() {
var url = $(".selected").attr("data-id");
var object = alldata[url];
console.log($(this).text());
if($(this).text() == "Add comment"){
$(this).text("");
};
$(this).css("color","#464A4D")
var el = $(this);
AutoSave.start(object, el , $(this).index(".comment"),url);
});
// on focusout comment box -> stop autosave
$( "#rightpanel" ).on("focusout" , ".comment", function() {
AutoSave.stop();
});
});
});
function displayquotes(url){
document.getElementById('panel-scrollContainer').innerHTML = "";
window.location.hash = url;
const fragment = document.getElementById('quote');
var offset = $(".article[data-id='"+url+"']").offset();
$("#article-scrollContainer").scrollTop(offset.top);
alldata[url].quotes.forEach(item => {
// Create an instance of the template content
const instance = document.importNode(fragment.content, true);
// Add relevant content to the template
instance.querySelector('.portal-content-519256').innerHTML = item.text;
instance.querySelector('.linkback a').href = url;
// try making the "view original" link use the text fragment spec. Janky af.
//instance.querySelector('.linkback a').href = url + "#:~:text=" + item.text.substring(0,20);
instance.querySelector('.portal-arrow-519256').href = url;
instance.querySelector('.title-wrapper-519256').innerHTML = alldata[url].title;
instance.querySelector('.portal-author-519256').innerHTML = alldata[url].author;
instance.querySelector('.mini-favicon-519256').src = "https://s2.googleusercontent.com/s2/favicons?domain_url="+url+"";
var date = new Date(item.date);
console.log(date); // date is a timestamp but we only display formatted
var dd = String(date.getDate()).padStart(2, '0');
var mm = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = date.getFullYear();
date = mm + '/' + dd + '/' + yyyy;
instance.querySelector('.date').innerHTML += date;
if(item.comment){
instance.querySelector('.comment').innerHTML = item.comment;
instance.querySelector('.comment').style.color = "#464A4D";
}else{
instance.querySelector('.comment').innerHTML = "Add comment";
};
// Append the instance ot the DOM
document.getElementById('panel-scrollContainer').appendChild(instance);
$( ".article" ).each(function() {
$( this ).removeClass( "selected" );
});
$(".article[data-id='"+url+"']").addClass( "selected" );
// Update the Title Bar
var titlebar = document.getElementById('titlebar');
titlebar.querySelector("#titlebar-author").innerHTML = alldata[url].author;
titlebar.querySelector("#titlebar-title").innerHTML = alldata[url].title;
});
};
// COPY TO CLIPBOARD
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
function extractHostname(url) {
var hostname;
//find & remove protocol (http, ftp, etc.) and get hostname
if (url.indexOf("//") > -1) {
hostname = url.split('/')[2];
}
else {
hostname = url.split('/')[0];
}
return hostname
};
// HOVER TO SHOW CONTROLS
$(document).on('mouseenter mouseleave', '.quoteblock', function(e) {
if (e.type == "mouseenter"){
$(this).find('.quote-controls').addClass('showcontrols');
}else{
$(this).find('.quote-controls').removeClass('showcontrols');
}
});
// AUTOSAVE FUNCTION
var AutoSave = (function(){
var timer = null;
function save(object, el, index, url){
console.log("running save");
console.log(el);
alldata[url]["quotes"][index]["comment"] = el.text();
chrome.storage.local.set(alldata, function(){
console.log("autosaved");
});
};
function restore(){ //don't think I actually need this restore function...?
var page = document.location.href;
var saved = "";
chrome.storage.local.get([page], function(result) {
saved = result[page]["quotes"][0]["comment"];
});
var editor = getEditor();
if (saved && editor){
editor.value = saved;
}
}
return {
start: function(object, el, index, url){
if (timer != null){
clearInterval(timer);
timer = null;
}
timer = setInterval(function(){
save(object, el, index, url)
}, 1000);
},
stop: function(){
if (timer){
clearInterval(timer);
timer = null;
}
}
}
}());
// AUTOSAVE Meta Title / Author FUNCTION
var AutoSaveTitle = (function(){
var timer = null;
function save(object, el, url){
console.log("running autosavetitle");
console.log(el);
var author = document.getElementById("titlebar-author").textContent;
var title = document.getElementById("titlebar-title").textContent;
alldata[url]["author"] = author;
alldata[url]["title"] = title;
chrome.storage.local.set(alldata, function(){
console.log("autosaved");
});
};
return {
start: function(object, el, url){
if (timer != null){
clearInterval(timer);
timer = null;
}
timer = setInterval(function(){
save(object, el, url)
}, 1000);
},
stop: function(){
if (timer){
clearInterval(timer);
timer = null;
}
}
}
}());
document.addEventListener("DOMContentLoaded", function(){
$(".comment").resizable();
});
// $("#leftnav").resizable({
// // only use the eastern handle
// handles: 'e',
// // restrict the width range
// minWidth: 120,
// maxWidth: 450,
// // resize handler updates the content panel width
// resize: function(event, ui){
// var currentWidth = ui.size.width;
// // this accounts for padding in the panels +
// // borders, you could calculate this using jQuery
// var padding = 12;
// // this accounts for some lag in the ui.size value, if you take this away
// // you'll get some instable behaviour
// $(this).width(currentWidth);
// // set the content panel width
// $("#rightpanel").width(containerWidth - currentWidth - padding);
// }
// });