-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
117 lines (107 loc) · 4.94 KB
/
content-script.js
File metadata and controls
117 lines (107 loc) · 4.94 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
let keysPressed = {
Alt : false,
Coloring : false
};
function StoreInLocal(SelectedDOM, EntireDOM){
let alrdyPrsntJSONFrmStrg;
var alrdySelctdTxt = [];
//store it in localstorage
// console.log("SelectText ClassName ", SelectedDOM.commonAncestorContainer.className);
// console.log("SelectText ID ", SelectedDOM.commonAncestorContainer.id);
// console.log("SelectedHTML ", SelectedDOM.commonAncestorContainer.innerHTML);
// console.log("SelectedText ", SelectedDOM.commonAncestorContainer.innerText);
// console.log("ParentElem ClassName ", SelectedDOM.commonAncestorContainer.parentElement.className);
// console.log("ParentElem ID ", SelectedDOM.commonAncestorContainer.parentElement.id);
// console.log("Entire DOM is ", EntireDOM);
let CurrentContent = {
SelectTextClassName : SelectedDOM.commonAncestorContainer.className,
SelectTextID : SelectedDOM.commonAncestorContainer.id,
SelectedHTML : SelectedDOM.commonAncestorContainer.innerHTML,
SelectedText : EntireDOM.toString(),
ParentElemClassName : SelectedDOM.commonAncestorContainer.parentElement.className,
ParentElemID : SelectedDOM.commonAncestorContainer.parentElement.id,
DOM: EntireDOM,
};
alrdyPrsntJSONFrmStrg = localStorage.getItem("HightlightInfo");
if(alrdyPrsntJSONFrmStrg !== "" && alrdyPrsntJSONFrmStrg !== null){
alrdySelctdTxt = JSON.parse(alrdyPrsntJSONFrmStrg);
}
alrdySelctdTxt.push( CurrentContent);
localStorage.setItem("HightlightInfo", JSON.stringify(alrdySelctdTxt));
};
function StoreAndColor(selectedText){
console.log("In StoreAndColor", selectedText);
if (selectedText.isCollapsed == false){ //if selected text isnt empty,
if (selectedText.rangeCount && selectedText.getRangeAt) {
range = selectedText.getRangeAt(0); //reading first one- in firefox there is an option to select multiple times.
//https://javascript.info/selection-range#:~:text=using%20the%20method%3A-,getRangeAt(i),-%E2%80%93%20get%20i%2Dth
console.log("range val", range );
}
if (range) {
// Set design mode to on
document.designMode = "on";
//adding to range
selectedText.addRange(range);
// Colorize text
document.execCommand("ForeColor", false, "white");
document.execCommand("BackColor", false, "black");
// Set design mode to off
document.designMode = "off";
//storing in local storage
StoreInLocal(range,selectedText);
//remove selected default selection
selectedText.removeAllRanges();
}
}
}
document.addEventListener('keydown', (event) => {
// keysPressed[event.key] = true;
if(event.key == 'Alt'){
keysPressed.Alt = true;
console.log("pressed ", event.key);
}
else if((event.key == 'c' || event.key == 'C') && keysPressed.Alt == true && keysPressed.Coloring == false){
let Content = document.getSelection();
// console.log("Window.Getselection value ", window.getSelection());
// console.log("in event ", Content.toString());
// console.log( "ID ", document.getSelection().valueOf("id"));
StoreAndColor(document.getSelection());
console.log("coloriing");
keysPressed.Coloring = true
}
else{
keysPressed.Coloring = false
}
},);
document.addEventListener('mouseup', (event) => {
console.log("Mouseup fired.");
console.log("Found text" , document.getSelection().toString());
});
window.addEventListener("load", (event) => {
console.log("loaaaaded");
var FullTxtFrmStrg;
var eachSelctdTxt;
var rtrndJSONFrmStrg = localStorage.getItem("HightlightInfo");
if (rtrndJSONFrmStrg !== "" && rtrndJSONFrmStrg !== null){
FullTxtFrmStrg = JSON.parse(rtrndJSONFrmStrg);
console.log(FullTxtFrmStrg);
//create ranges.
FullTxtFrmStrg.forEach((eachVal) => {
eachSelctdTxt = eachVal.SelectedText
console.log("TEXT : ", eachSelctdTxt);
var FoundIndex = (document.documentElement.textContent || document.documentElement.innerText).indexOf(eachSelctdTxt);
if ((document.documentElement.textContent || document.documentElement.innerText).indexOf(eachSelctdTxt) > -1) {
console.log(eachSelctdTxt, " - Value Found!");
// let node = document.getElementById(eachVal.ParentElemID);
//console.log("Found Text ", node);
//console.log("Entire DOM ", eachVal.DOM);
const range = document.createRange();
const element = document.body.textContent;
const node = element.childNode[0];
range.setStart(node, FoundIndex);
}else{console.log(eachSelctdTxt, "- Value NOT Found!");}
});
}else{
console.log("No Previously Selected Values Found!");
}
});