-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
133 lines (120 loc) · 3.46 KB
/
content.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
var clickedEl = null;
document.addEventListener("mousedown", function(event) {
//right click
if (event.button === 2) {
clickedEl = event.target;
}
}, true);
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request === "getClickedEl") {
sendResponse({
value: clickedEl.value
});
console.log("Now Rendering");
renderPara(clickedEl);
}
});
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function removeHTML(htmlText){
var text = "";
for(var i = 0; i < htmlText.length; i++){
if(htmlText[i] === "<"){
while(htmlText[i] !== ">")
i++;
continue;
}
text += htmlText[i];
}
return text;
}
/**
* This function takes the clicked HTML object
* And renders text according to configuration
**/
async function renderPara(clickedEl){
if(!clickedEl){
return;
}
//
var span = 11;
// min time to stop on a word. Will add time to this based on word
var defaultTime = 150;
// get the text and clean it
var originalText = clickedEl.innerHTML;
var text = getFormattedText(clickedEl);
console.log(text);
// pre is the words that has been rendered
// middle is the word that will be renedered now
// post is the word that will be rendered in future
var pre = "";
var middle = "";
var post = text;
// iterate till there is no word left to iterate
while(post){
// remove html elements
if(post[0] === "<"){
while(post[0] !== ">")
post = post.substring(1);
post = post.substring(1);
}
// render numbers in one go
if(post[0] >= '0' && post[0] <= "9"){
while(post && post[0] !== " "){
middle += post[0];
post = post.substring(1);
}
}
if(post)
middle += post[0];
// console.log(post);
// its time to display new text
if(post.length <= 1 || pauseTime[post[0]] || (post[0] === " " && middle.length > span)){
var emboldedText = emboldText(pre, middle, post.substring(1));
clickedEl.innerHTML = emboldedText;
//sleep here
var waitTime = defaultTime + middle.length * 20;
if(pauseTime[post[0]]){
waitTime += pauseTime[post[0]];
}
await sleep(waitTime);
pre += middle;
middle = "";
}
post = post.substring(1);
}
// make the page as it was before
clickedEl.innerHTML = originalText;
currentEl = clickedEl.nextElementSibling;
console.log(currentEl.nodeName);
console.log(clickedEl.nodeName);
while(currentEl.nodeName !== clickedEl.nodeName){
currentEl = currentEl.nextElementSibling;
console.log(clickedEl.nodeName);
}
return renderPara(currentEl);
}
function getFormattedText(para){
// https://stackoverflow.com/questions/3738490/finding-line-wraps
var current = para;
var text = removeHTML(current.innerHTML);
var words = text.split(' ');
current.innerHTML = words[0];
var height = current.offsetHeight;
var finalText = words[0];
for(var i = 1; i < words.length; i++){
current.innerHTML = current.innerHTML + ' ' + words[i];
if(current.offsetHeight > height){
finalText += "\n" + words[i];
height = current.offsetHeight;
}else{
finalText += ' ' + words[i];
}
}
return finalText;
}
function emboldText(pre, boldTxt, post){
var emboldedText = "<span style='color: #D3D3D3'>" + pre +"</span>" + "<span style='color: #000000'>" + boldTxt +"</span>" + "<span style='color: #D3D3D3'>" + post +"</span>";
return emboldedText;
}