-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
114 lines (97 loc) · 3.83 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
function collectNonEmptyTextNodes(element) {
let textElements = [];
function recurseThroughNodes(node) {
// Define the parent tags where text should not be modified
const excludedTags = ["SCRIPT", "STYLE"];
// If the node is a text node and its not empty and its parent is not excluded
if (
node.nodeType === Node.TEXT_NODE &&
node.textContent.trim() !== "" &&
!excludedTags.includes(node.parentNode.tagName)
) {
textElements.push(node);
}
// If the node is an element node, check all of its children nodes for text
else if (node.nodeType === Node.ELEMENT_NODE) {
Array.from(node.childNodes).forEach(recurseThroughNodes);
}
}
// Recursively go through every element
recurseThroughNodes(element);
// Return the array of non-empty text nodes
return textElements;
}
function boldEveryXWord(textNode, wordPercentage, boldXWords, boldWeight) {
const fullText = textNode.textContent;
const words = fullText.split(/\s+/);
let updatedParts = [];
let actualWordCount = 0;
words.forEach((word, index) => {
if (word.trim() !== '') {
if (actualWordCount % boldXWords === 0) {
const boldLength = Math.ceil(word.length * (wordPercentage / 100));
const boldPart = word.substring(0, boldLength);
const restPart = word.substring(boldLength);
updatedParts.push(`<strong class="bionic-enhanced" style="font-weight:${boldWeight};">${boldPart}</strong>${restPart}`);
} else {
updatedParts.push(word);
}
actualWordCount++;
}
// Add space if it's not the last word
if (index < words.length - 1) {
updatedParts.push(' ');
}
});
const updatedText = updatedParts.join('');
const newContent = document.createRange().createContextualFragment(updatedText);
if (textNode.parentNode) {
textNode.parentNode.replaceChild(newContent, textNode);
}
}
function enableBionicReading(wordPercentage, boldXWords, boldWeight) {
console.log("started to enable bionic");
// Get all non-empty text nodes in the document
let elements = collectNonEmptyTextNodes(document.body);
// Apply the bold transformation to each text node
elements.forEach((el) => {
console.log("Bolding an element");
boldEveryXWord(el, wordPercentage, boldXWords, boldWeight);
});
}
function removeBionicEnhancements() {
const enhancedElements = document.querySelectorAll('.bionic-enhanced');
enhancedElements.forEach(enhancedElement => {
// Remove the <strong> tag but preserve the text
const parent = enhancedElement.parentNode;
while (enhancedElement.firstChild) {
parent.insertBefore(enhancedElement.firstChild, enhancedElement);
}
parent.removeChild(enhancedElement);
});
document.body.normalize();
}
let isEnabled = false;
// Listen for messages from the popup
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
// Check the command and action to toggle Bionic Reading
if (request.command === "toggleBionic") {
if (request.action === "enable") {
isEnabled = true;
// Enable Bionic Reading with the specified settings
enableBionicReading(request.argument.percentOfWord, request.argument.everyXWord, request.argument.boldWeight);
} else if (request.action === "disable") {
isEnabled = false;
// Disable Bionic Reading
removeBionicEnhancements();
}
}
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.command === "updateSettings" && isEnabled) {
// First, remove any existing Bionic enhancements
removeBionicEnhancements();
// Then, apply the new settings
enableBionicReading(request.settings.percentOfWord, request.settings.everyXWord, request.settings.boldWeight);
}
});