-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
95 lines (87 loc) · 2.5 KB
/
content_script.js
File metadata and controls
95 lines (87 loc) · 2.5 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
86
87
88
89
90
91
92
93
94
95
var url = window.location.href.toString();
var DOM = document.body;
var highlights, note;
function searchForHighlights() {
chrome.storage.local.get("highlights", results => {
if (objDoesNotExist(results)) {
createHighlightObj();
} else {
if (doHighlightsForThisURLExist(results)) {
return;
} else {
applyHighlights(results.highlights[url]);
addPromptToTargets();
}
}
});
}
function objDoesNotExist(results) {
if (
results === "undefined" ||
(Object.entries(results).length === 0 && results.constructor === Object)
) {
return true;
}
}
function createHighlightObj() {
highlights = {};
// active = {
// active: true
// };
chrome.storage.local.set({ highlights }, () => {});
// chrome.storage.local.get("active", results => {});
return;
}
function doHighlightsForThisURLExist(results) {
if (!results.highlights[url]) {
return true;
}
}
function applyHighlights(pageHighlights) {
console.log("Highlights Found For This URL");
for (key in pageHighlights) {
if (!(pageHighlights[key].toString().charAt(0) === "#")) {
var nodeList = document.body.querySelectorAll(pageHighlights[key][0]); // NodeList(4) [queryselector, ...]
for (let i = 0; i < nodeList.length; i++) {
if (
pageHighlights[key][1] === nodeList[i].innerText.indexOf(key) ||
pageHighlights[key][2] === nodeList[i].innerText.indexOf(key)
) {
grabNoteIfExists(pageHighlights);
nodeList[i].innerHTML = nodeList[i].innerHTML.replace(
key,
`<span style="background-color: ${pageHighlights["color"] ||
"#CFFFDF"};" class="el" title="${note}">` +
key +
"</span>"
);
}
}
}
}
}
function grabNoteIfExists(phls) {
if (phls[key][3] != undefined) {
note = phls[key][3].toString();
} else {
note = "";
}
}
searchForHighlights();
function addPromptToTargets() {
var nodes = document.getElementsByClassName("el");
for (let i = 0; i < nodes.length; i++) {
nodes[i].ondblclick = () => {
note = prompt("Add a comment for this highlight: ", nodes[i].title);
if (note != null) {
chrome.storage.local.get("highlights", results => {
highlights = results.highlights;
highlight = highlights[url][nodes[i].innerHTML];
highlight[3] = note;
nodes[i].title = note;
chrome.storage.local.set({ highlights }, () => {});
});
}
};
}
}