-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontentScript.js
129 lines (112 loc) · 3.66 KB
/
contentScript.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
let isEnabled = false;
let focusLength = 2;
let isDarkMode = false;
let isDarkMode2 = false;
chrome.storage.sync.get(["isEnabled", "focusLength", "isDarkMode", "isDarkMode2"], ({ isEnabled: savedIsEnabled, focusLength: savedFocusLength, isDarkMode: savedIsDarkMode, isDarkMode2: savedIsDarkMode2 }) => {
isEnabled = savedIsEnabled;
focusLength = savedFocusLength || 2;
isDarkMode = savedIsDarkMode || false;
isDarkMode2 = savedIsDarkMode2 || false;
if (isEnabled) {
activateBionicReading();
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "activateBionicReading") {
isEnabled = true;
activateBionicReading();
} else if (request.action === "deactivateBionicReading") {
isEnabled = false;
deactivateBionicReading();
} else if (request.action === "updateFocusLength") {
focusLength = parseInt(request.focusLength, 10);
if (isEnabled) {
updateBionicReading();
}
} else if (request.action === "toggleDarkMode") {
isDarkMode = request.isDarkMode;
updateBionicReading();
} else if (request.action === "toggleDarkMode2") {
isDarkMode2 = request.isDarkMode2;
updateBionicReading();
}
});
function injectCSS() {
const primaryColor = isDarkMode ? '#B0C4DE' : 'inherit';
const secondaryColor = isDarkMode ? '#A0D6B4' : 'grey';
const primaryColor2 = isDarkMode2 ? '#FFA07A' : 'inherit'; // New alternate color
const secondaryColor2 = isDarkMode2 ? '#FFB6C1' : 'grey'; // New alternate color
const styles = `
.bionic-primary {
font-weight: bold;
color: ${isDarkMode2 ? primaryColor2 : primaryColor};
}
.bionic-secondary {
font-weight: bold;
color: ${isDarkMode2 ? secondaryColor2 : secondaryColor};
}
`;
const styleElement = document.createElement('style');
styleElement.innerHTML = styles;
document.head.appendChild(styleElement);
}
function activateBionicReading() {
injectCSS();
let textNodes = getTextNodes(document.body);
textNodes.forEach((node) => {
let bionicText = applyBionicReading(node);
let newNode = document.createElement("span");
newNode.innerHTML = bionicText;
node.parentNode.replaceChild(newNode, node);
});
}
function deactivateBionicReading() {
let bionicSpans = document.querySelectorAll(".bionic-primary, .bionic-secondary");
bionicSpans.forEach((span) => {
span.outerHTML = span.textContent;
});
}
function updateBionicReading() {
deactivateBionicReading();
activateBionicReading();
}
function getTextNodes(element) {
let textNodes = [];
let walk = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, {
acceptNode: function (node) {
if (
!/^(script|style|noscript)$/i.test(node.parentNode.nodeName) &&
node.textContent.trim().length > 0
) {
return NodeFilter.FILTER_ACCEPT;
}
},
}, false);
while (node = walk.nextNode()) {
textNodes.push(node);
}
return textNodes;
}
function applyBionicReading(textNode) {
const text = textNode.nodeValue;
const words = text.split(/(\s+)/);
const bionicWords = words.map((word) => {
if (/^\s+$/.test(word)) {
return word;
} else {
let bionicWord = "";
const characters = word.split('');
characters.forEach((char, index) => {
if (index === 0 || (index < focusLength && word.length > focusLength)) {
bionicWord += `<span class="bionic-primary">${char}</span>`;
} else if (index === Math.floor(word.length / 2)) {
bionicWord += `<span class="bionic-secondary">${char}</span>`;
} else {
bionicWord += char;
}
});
return bionicWord;
}
});
return bionicWords.join('');
}