-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotes.js
More file actions
91 lines (77 loc) · 2.25 KB
/
notes.js
File metadata and controls
91 lines (77 loc) · 2.25 KB
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
var noteCount = 0;
init_notes();
function clear_notes() {
document.querySelectorAll('.htn-sticky-note-class').forEach(e => e.remove());
noteCount = 0;
}
function add_notes(notes) {
var body = document.getElementsByTagName("body")[0];
for (let i = 0; i < notes.length; i++) {
noteCount += 1;
var noteNode = document.createElement("div");
noteNode.setAttribute("id", "htn-sticky-note-" + noteCount);
noteNode.setAttribute("class", "htn-sticky-note-class");
noteNode.innerText = notes[i];
noteNode.setAttribute("size", notes[i].length);
noteNode.style.left = (noteCount - 1) * 200 + "px";
noteNode.style.top = 10 + "px";
body.prepend(noteNode);
draggable(document.getElementById("htn-sticky-note-" + noteCount));
}
}
function draggable(DOMelement) {
var x1 = 0,
y1 = 0,
x2 = 0,
y2 = 0;
if (document.getElementById("sticky-notes-tut")) {
document.getElementById("sticky-notes-tut").onmousedown = dragMouseDown;
} else {
DOMelement.onmousedown = dragMouseDown;
}
function dragMouseDown(event) {
event = event || window.event;
event.preventDefault();
x2 = event.clientX;
y2 = event.clientY;
document.onmouseup = function () {
document.onmouseup = null;
document.onmousemove = null;
};
document.onmousemove = elementDrag;
}
function elementDrag(event) {
event = event || window.event;
event.preventDefault();
x1 = x2 - event.clientX;
y1 = y2 - event.clientY;
x2 = event.clientX;
y2 = event.clientY;
DOMelement.style.top = DOMelement.offsetTop - y1 + "px";
DOMelement.style.left = DOMelement.offsetLeft - x1 + "px";
}
}
///// EDIT CODE BELOW THIS LINE
function init_notes() {
let url = document.URL;
chrome.storage.local.get(document.URL, notes => {
if (notes[url]) {
add_notes(notes[url]);
}
});
}
chrome.runtime.onMessage.addListener(
(request, _, sendResponse) => {
if (request.action == "clear") { // Delete All Notes on the Page
clear_notes();
sendResponse({status: "complete"});
}
else if (request.action == "add") { // Add New Notes to Page
add_notes(request.notes);
sendResponse({status: "complete"});
}
else {
sendResponse({status: "error"});
}
}
);