-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
newtab.js
487 lines (438 loc) · 17.5 KB
/
newtab.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
477
478
479
480
481
482
483
484
485
486
487
; (() => {
// FIXME: Should use uuid instead of index as key for storage
/** DB
* {
* version: <number>,
* mode: <string>
* list: [{
* content: <string>
* time: <number>
* }]
* }
**/
const newtab_script = () => {
const THEMES = {
night: "night",
day: "day"
}
const $body = document.querySelector('body')
const $textarea = document.querySelector('#note-content')
const $list = document.querySelector('#list')
const $mode_switcher = document.querySelector('#mode-switcher')
const $setting_gear = document.querySelector('#setting-icon')
const $credits_button = document.querySelector("#addon-author")
const $status = document.querySelector('#status')
const $create_entry = document.querySelector('#create_entry')
const $undo_deletion = document.querySelector("#undo_deletion")
let currentNoteId = 0
let data = null
let undostack = []
let hide_timeout = null
//paste text in plaintext, still allow pasting images
$textarea.addEventListener("paste", function (event) {
if (event.clipboardData.getData("text/plain") !== "") {
event.preventDefault();
document.execCommand("inserttext", false, event.clipboardData.getData("text/plain"));
}
})
//click links with ctrl+click
$textarea.addEventListener("click", function (event) {
if (data.clickablelinks && event.ctrlKey) {
var link = clickedword()
try {
url = new URL(link);
window.open(url)
} catch (_) {
console.log(link + " is an invalid link.")
}
}
})
function clickedword() {
var sel = document.getSelection();
sel.modify("extend", "backward", "paragraphboundary");
var pos = sel.toString().length;
if (sel.anchorNode != undefined) sel.collapseToEnd();
var fullstr = sel.focusNode.textContent
return fullstr.substring(fullstr.lastIndexOf(' ', pos) + 1, fullstr.indexOf(' ', pos) >= pos ? fullstr.indexOf(' ', pos) : fullstr.length)
}
const _emptyNote = () => {
const emptyNote = Object.create(null)
emptyNote.content = ''
emptyNote.time = (new Date()).getTime()
return emptyNote
}
const _render = (rendernote) => {
$body.style.backgroundColor = "#ffffff"
const _renderList = list => {
const _makeTitleString = content => content.substr(0, 50).replace(/<.*?>/g, '').replace(/[^A-Za-z0-9 ]/g, '').substr(0, 10) || '<span class="empty-string">(EMPTY)</span>'
const $ul = document.querySelector('ul')
$ul.innerHTML = list.sort((a, b) => b.time - a.time).map((item, index) => {
let title = _makeTitleString(item.content)
let className = index === currentNoteId ? 'current' : ''
return `<li class="${className}" data-id="${index}"><span>${title}<div class='del'>+</div><div class='dnld'><img class="dnldimg" src="./dnld.png" alt="Download"></div></span></li>`
}).join('')
if (undostack.length > 0) {
$undo_deletion.style.display = "block"
} else {
$undo_deletion.style.display = "none"
}
$ul.querySelectorAll('li').forEach(function ($li, index) {
$li.addEventListener('click', function (event) {
//console.log($textarea.innerText || $textarea.textContent)
if (event.target.classList.contains('del')) {
const currentNote = list[index]
if (currentNote.content !== '') {
const noteTitle = _makeTitleString(currentNote.content)
const deleteConfirmString = `Do you want to delete note: ${noteTitle}?`
if (!confirm(deleteConfirmString)) { return }
}
//this code is responsible for deleting notes
//if after deleting this note, the list of notes will be empty:
if (index === 0 && data.list.length === 1) {
undostack.push(data.list[index])
data.list = [_emptyNote()]
}
//if after deleting this note, the list of notes is not empty:
else {
undostack.push(data.list[index])
data.list = data.list.filter((note, i) => i !== index)
if (currentNoteId >= data.list.length) {
currentNoteId = data.list.length - 1
}
}
browser.storage.local.set({ list: data.list })
_render(true)
return
}
else if (event.target.classList.contains('dnld') || event.target.classList.contains('dnldimg')) {
currentNoteId = index
_render(true)
const noteTitle = _makeTitleString(list[index].content)
//const dbutton = $li.querySelector('.dnld');
//console.log(dbutton);
var userInput = $textarea.innerText;
var blob = new Blob([userInput], { type: "text/plain;charset=utf-8" });
let newLink = document.createElement("a");
newLink.download = `${noteTitle}.txt`;
if (window.webkitURL != null) {
newLink.href = window.webkitURL.createObjectURL(blob);
}
else {
newLink.href = window.URL.createObjectURL(blob);
newLink.style.display = "none";
document.body.appendChild(newLink);
}
newLink.click();
return
}
if (currentNoteId === index) { return }
currentNoteId = index
_render(true)
})
})
}
const _renderNote = note => {
$textarea.innerHTML = note.content || ''
$textarea.focus()
}
_renderList(data.list)
if (rendernote) {
_renderNote(data.list[currentNoteId])
}
}
const _enableAnimation = () => {
setTimeout(() => {
document.querySelector('style').innerHTML += `
#create_entry, #note-content, #mode-switcher,
#addon-author, #list, #list li > span, #list .current:before {
transition-duration: .2s;
}
`
}, 300)
}
const _noteEventHandler = () => {
// auto saving and indicator, also a save timeout so you can't exceed GitHub's rate limits
let write_timeout, save_timeout;
let timed_out = false, buffered = false;
$textarea.addEventListener('keyup', () => {
//if note didnt change
if (data.list[currentNoteId].content === $textarea.innerHTML) { return }
$status.classList.remove('hide')
$status.textContent = 'Saving...'
clearTimeout(hide_timeout)
clearTimeout(write_timeout)
write_timeout = setTimeout(() => {
if (!timed_out) {
_saveNote()
timed_out = true
save_timeout = setTimeout(() => {
timed_out = false
if (buffered) {
buffered = false
_saveNote()
}
}, 5000)
} else {
buffered = true
}
}, 250)
const _saveNote = async () => {
console.log("saving")
//save to local storage
data.list[currentNoteId].content = $textarea.innerHTML
data.list[currentNoteId].time = (new Date()).getTime()
currentNoteId = 0
browser.storage.local.set({ list: data.list })
//save to gist
fetch(`https://api.github.com/gists/${data.gistid}`, {
method: "PATCH",
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${data.map}`,
},
body: JSON.stringify({
description: `Gist to sync your tab-notes data. Last updated at: ${new Date().toLocaleString()}`,
files: { "tab-notes.html": { content: data.list.map(note => `${note.content}\n\n<<${note.time}>>\n\n`).filter(c => c).join('') } }
}),
}).then(async response => {
if (!response.ok) {
return response.text().then(text => { throw new Error(text) })
}
return response.json()
}).then(out => {
$status.classList.remove('hide')
$status.textContent = 'Saved and synced.'
})
.catch(error => {
$status.classList.remove('hide')
$status.textContent = 'Saved locally.'
console.log(`An error occured trying to upload the note to a GitHub gist: ${error}`)
data.offline = true
browser.storage.local.set({ offline: data.offline })
_attemptConnection()
})
clearTimeout(hide_timeout)
_render(false);
hide_timeout = setTimeout(() => $status.classList.add('hide'), 3000)
}
})
$textarea.focus()
}
const _createButtonEventHandler = () => {
$create_entry.addEventListener('click', () => {
currentNoteId = 0
data.list = [_emptyNote(), ...data.list]
browser.storage.local.set({ list: data.list })
_render(true)
})
}
const _undoButtonEventHandler = () => {
$undo_deletion.addEventListener('click', () => {
if (undostack.length > 0) {
data.list = [...data.list, undostack.pop()]
browser.storage.local.set({ list: data.list })
_render(true)
} else {
alert("Nothing to undo.")
}
})
}
const _renderTheme = () => {
$textarea.style.fontFamily = data.font
$textarea.style.fontSize = data.fontsize + "px"
$textarea.style.lineHeight = data.fontsize * 1.5 + "px"
if (data.mode == THEMES.night) {
$body.classList.add('dark')
$textarea.classList.add('dark')
$list.classList.add('dark')
$setting_gear.classList.add('dark')
}
else {
$body.classList.remove('dark')
$textarea.classList.remove('dark')
$list.classList.remove('dark')
$setting_gear.classList.remove('dark')
}
$mode_switcher.hidden = !data.darktoggleonnotes
$credits_button.hidden = !data.showcredits
}
const _themeSwitchHandler = () => {
$mode_switcher.addEventListener('click', event => {
$body.classList.toggle('dark')
$textarea.classList.toggle('dark')
$list.classList.toggle('dark')
data.mode = data.mode === THEMES.day ? THEMES.night : THEMES.day
browser.storage.local.set({ mode: data.mode })
_renderTheme()
})
}
const _settingHandler = () => {
$setting_gear.addEventListener('click', event => {
window.open('settings.html')
})
}
const _multiTabHandler = () => {
const loadLatestData = async updateTabInfo => {
let currentTabInfo = await browser.tabs.getCurrent()
if (typeof updateTabInfo === 'object') { // tab switch
if (currentTabInfo.id !== updateTabInfo.tabId || currentTabInfo.windowId !== updateTabInfo.windowId) { return }
}
else { // window
if (currentTabInfo.windowId !== updateTabInfo) { return }
}
data = await window.utils.loadPreference()
_render(true)
_renderTheme()
}
browser.tabs.onActivated.addListener(loadLatestData)
// XXX: Window event causing double click issue, should temporarily comment it when default open sidebar...
browser.windows.onFocusChanged.addListener(loadLatestData)
}
const _initEventHandler = () => {
_noteEventHandler()
_createButtonEventHandler()
_undoButtonEventHandler()
_themeSwitchHandler()
_settingHandler()
_multiTabHandler()
}
const _renderAnnoucement = async () => {
const syncRes = await browser.storage.sync.get()
if (!syncRes.list && !syncRes.mode) { return } // not use the version using storage.sync
// annoucement
const $annoucement_container = document.querySelector('.annoucement-container')
$annoucement_container.innerHTML = `
<div id='annoucement'>
Due to <a href='https://blog.mozilla.org/addons/2020/07/09/changes-to-storage-sync-in-firefox-79/' target='_blank'>Firefox WebExtension Storage API update</a>,
the sync function between Firefox is not work anymore...
<div class='btn'>So sad, I get it.</div>
</div>
`
const $migrate_btn = $annoucement_container.querySelector('.btn')
const migrateBtnOnClick = async () => {
$migrate_btn.removeEventListener('click', migrateBtnOnClick)
const before = await browser.storage.sync.get()
const clearRes = await browser.storage.sync.clear()
const after = await browser.storage.sync.get()
$annoucement_container.remove()
}
$migrate_btn.addEventListener('click', migrateBtnOnClick)
}
const _attemptConnection = () => {
//attempt to connect to the github gist
setTimeout(() => {
fetch(`https://api.github.com/gists/${data.gistid}`, {
method: "GET",
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${data.map}`
}
}).then(async response => {
if (!response.ok) {
throw new Error()
}
console.log("Reconnected successfully")
_syncNotes()
}).catch(error => {
console.log(`An error occured while trying to reconnect: ${error}`)
_attemptConnection()
})
}, 5000)
}
const _syncNotes = () => {
//if it's synced with github, then load the data
if (!data.offline && data.gistid != undefined) {
fetch(`https://api.github.com/gists/${data.gistid}`, {
method: "GET",
headers: {
Accept: "application/vnd.github+json",
//Authorization: `Bearer ${data.map}`
}
}).then(async response => {
if (!response.ok) {
return response.text().then(text => { throw new Error(text) })
}
return response.json()
}).then(out => {
notecontent = out.files["tab-notes.html"].content
//replace notes content
var tmp = notecontent.split(/\n\n<<([0-9]+)>>\n\n/g).slice(0, -1)
var newnotes = []
for (var i = 0; i < tmp.length; i += 2) {
newnotes.push({ content: tmp[i], time: parseInt(tmp[i + 1]) })
}
data.list = newnotes
browser.storage.local.set({ list: data.list })
//add a "synced" text at the bottom
$status.classList.remove('hide')
$status.textContent = 'Synced.'
_render(false)
clearTimeout(hide_timeout)
hide_timeout = setTimeout(() => $status.classList.add('hide'), 3000)
}).catch(error => {
data.offline = true
browser.storage.local.set({ offline: data.offline })
console.log(`An error occured trying to load the note content: ${error}`)
_attemptConnection()
})
//if it was offline, push the offline edits to the gist
} else if (data.offline && data.gistid != undefined) {
//push the current notes to the gist
fetch(`https://api.github.com/gists/${data.gistid}`, {
method: "PATCH",
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${data.map}`,
},
body: JSON.stringify({
description: `Gist to sync your tab-notes data. Last updated at: ${new Date().toLocaleString()}`,
files: { "tab-notes.html": { content: data.list.map(note => `${note.content}\n\n<<${note.time}>>\n\n`).filter(c => c).join('') } }
})
}).then(async response => {
if (!response.ok) {
return response.text().then(text => { throw new Error(text) })
}
return response.json()
}).then(out => {
$status.classList.remove('hide')
$status.textContent = 'Synced.'
data.offline = false
browser.storage.local.set({ offline: data.offline })
clearTimeout(hide_timeout)
_render(false);
hide_timeout = setTimeout(() => $status.classList.add('hide'), 3000)
})
.catch(error => {
console.log(`An error occured trying to upload the note to a GitHub gist: ${error}`)
data.offline = true
browser.storage.local.set({ offline: data.offline })
_attemptConnection()
})
} else if (data.gistid != undefined) {
data.offline = true
browser.storage.local.set({ offline: data.offline })
console.log(`Couldn't load note content because you're offline`)
_attemptConnection()
} else {
console.log("Can't sync notes, because you haven't configured your settings yet.")
}
}
const init = async () => {
data = await window.utils.loadPreference()
//render instantly so screen doesn't stay white
_render(true)
_syncNotes()
_renderAnnoucement()
_render(true)
_renderTheme()
_initEventHandler()
_enableAnimation()
}
return {
init: init
}
}
window.addEventListener('load', () => {
newtab_script().init()
})
})()