forked from coreymwamba/old-opera-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.js
137 lines (120 loc) · 3.85 KB
/
notes.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
// variables
var blocksq = document.getElementsByTagName("blockquote");
var bq = blocksq[0];
var newNote = document.getElementById("newnote");
var delNote = document.getElementById("delnote");
var noteList = document.getElementById("notelist");
bq.addEventListener("keyup", saveNote, true);
bq.addEventListener("focus", createNote, true);
newNote.addEventListener("click", createNoteByClick, true);
delNote.addEventListener("click", deleteNoteByClick, true);
noteList.addEventListener("keydown", deleteNote, true);
noteList.addEventListener("change", displayNote, true);
//display note
function displayNote() {
var selectedListItem = noteList.options[noteList.selectedIndex];
var j = selectedListItem.id;
bq.setAttribute("id", "note-" + j);
bq.innerText = localStorage.getItem(j);
}
//delete notes with delete key
function deleteNote(d) {
var selectedListItem = noteList.options[noteList.selectedIndex];
if (d.keyCode == 46) {
var j = selectedListItem.id;
var deletedNode = document.getElementById(j);
localStorage.removeItem(j);
var sbq = document.getElementById("note-" + j);
sbq.innerText = "";
sbq.removeAttribute("id");
while (noteList.lastChild) {
noteList.removeChild(noteList.lastChild);
}
displayListNotes();
}
}
function deleteNoteByClick() {
var selectedListItem = noteList.options[noteList.selectedIndex];
var j = selectedListItem.id;
var deletedNode = document.getElementById(j);
var sbq = document.getElementById("note-" + j);
sbq.innerText = "";
localStorage.removeItem(j);
sbq.removeAttribute("id");
while (noteList.lastChild) {
noteList.removeChild(noteList.lastChild);
}
displayListNotes();
}
// create a new note, inserting
// a blockquote with an id
// FUTURE plan: if the copied text
// is from a web page, add the
// URL into the cite attribute
// [how will this work with localStorage??]
function createNoteByClick() {
var newDate = new Date();
var noteId = newDate.getTime();
bq.setAttribute("id", "note-" + noteId);
bq.innerText = null;
localStorage.setItem(noteId, "");
bq.focus();
}
function createNote() {
if (bq.hasAttribute("id") == false) {
var newDate = new Date();
var noteId = newDate.getTime();
bq.setAttribute("id", "note-" + noteId);
localStorage.setItem(noteId, "");
}
}
function saveNote() {
var noteList = document.getElementById("notelist");
bqId = bq.id;
prevNoteId = bqId.replace("note-", "");
var alreadyThere = document.getElementById(prevNoteId);
prevNoteText = localStorage.getItem(prevNoteId);
noteListItem = document.createElement("option");
summaryText = bq.innerText;
if (summaryText.length > 25) {
noteListItem.innerText = summaryText.substring(0, 25) + "...";
} else {
noteListItem.innerText = summaryText;
}
noteListItem.setAttribute("id", prevNoteId);
if (alreadyThere) {
noteList.replaceChild(noteListItem, alreadyThere);
} else {
noteList.appendChild(noteListItem);
}
localStorage.setItem(prevNoteId, bq.innerText);
}
//display list of notes
function displayListNotes() {
var i = 0;
var noteItems = localStorage.length - 1;
for (i = 0; i <= noteItems; i++) {
var noteKey = localStorage.key(i);
var noteValue = localStorage.getItem(noteKey);
var noteTitle;
if (noteValue.length > 25) {
noteTitle = noteValue.substring(0, 25) + "...";
} else {
noteTitle = noteValue;
}
noteListItem = document.createElement("option");
noteListItem.innerText = noteTitle;
noteListItem.setAttribute("id", noteKey);
prevNoteId = noteListItem.id;
var alreadyThere = document.getElementById(prevNoteId);
if (!alreadyThere) {
noteList.appendChild(noteListItem);
}
}
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.actionItem == "update")
displayListNotes();
});
window.onload = displayListNotes();