-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_script.js
125 lines (106 loc) · 3.91 KB
/
content_script.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
// initMakeNoteHandler handles all the calls from background
// All the handlers needs to be registered here
function initMakeNoteHandler() {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "make_note") {
handleMakeNote()
sendResponse(true)
} else {
console.log("No handlers registered for this action")
sendResponse(false)
}
});
}
function persistNoteInStorage(tag, note, callback) {
chrome.storage.local.get(['easy-note-data'], function (results) {
notesData = {};
if(results.hasOwnProperty('easy-note-data')){
notesData = results['easy-note-data'];
}
if(notesData[tag] === undefined){
notesData[tag] = [note]
}else{
notesData[tag].push(note)
}
chrome.storage.local.set({'easy-note-data': notesData}, function () {
if(callback !== undefined) {
callback();
}
});
});
}
// TODO: Save tags under another key in storage to optimise computation each time new note needs to be saved
function getTags(callback) {
// TODO: Change local -> sync for accross devies sync
chrome.storage.local.get(['easy-note-data'], function (result) {
if(!result.hasOwnProperty('easy-note-data')){
callback([]);
return;
}
callback(Object.keys(result['easy-note-data']));
});
}
function displayExistingTags(tags){
var datalist = document.createElement('datalist');
datalist.setAttribute("id", "en-tags-list");
tags.forEach(tag => {
var optionT = document.createElement('option');
optionT.setAttribute("value", tag);
datalist.appendChild(optionT);
});
container = document.getElementById("en-input-tag-container");
container.appendChild(datalist);
}
function handleMakeNote() {
const selection = window.getSelection();
if (selection.toString() !== "") {
var dateTimeNow = new Date();
note = {
dataString: selection.toString(),
url: window.location.href,
title: document.title,
timeStamp: dateTimeNow.getTime()
}
//Get a the selected content, in a range object
var range = selection.getRangeAt(0);
//If the range spans some text, and inside a tag, set its css class.
if (range && !selection.isCollapsed) {
var input = document.createElement('input');
input.setAttribute("id", "en-input-tag");
input.setAttribute("list", "en-tags-list");
var container = document.createElement('div');
container.setAttribute("id", "en-input-tag-container");
container.setAttribute("style", "position: absolute;margin-left: -30px;margin-top: -30px;");
container.appendChild(input);
range.insertNode(container);
getTags(displayExistingTags);
input.focus();
input.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
// Cancel the default action, if needed
event.preventDefault();
persistNoteInStorage(input.value, note);
container.remove();
}
if (event.key === "Alt") {
event.preventDefault();
container.remove();
}
});
}
}
}
// TODO:: Give option to take note on text selection
function textSelectEvent() {
const p = document.body
p.addEventListener('mouseup', (e) => {
const selection = window.getSelection().toString();
if (selection === '') {
console.log('click');
} else {
console.log('selection', selection);
}
});
}
// textSelectEvent()
initMakeNoteHandler()