-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
76 lines (67 loc) · 2.37 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
let zenReadEnabled = false;
const originalTextMap = new Map();
const observer = new MutationObserver(mutations => {
if (zenReadEnabled) {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.nodeType === 1) {
applyBionicReadingToNode(node);
}
});
});
}
});
function applyBionicReading(text) {
return text.split(' ').map(word => {
const mid = Math.floor(word.length / 2);
return `<b>${word.substring(0, mid)}</b>${word.substring(mid)}`;
}).join(' ');
}
function applyBionicReadingToNode(node) {
if (['SCRIPT', 'STYLE', 'TEXTAREA', 'INPUT', 'CODE', 'PRE'].includes(node.nodeName)) {
return;
}
Array.from(node.childNodes).forEach(child => {
if (child.nodeType === 3 && child.nodeValue.trim() !== '') {
const span = document.createElement('span');
originalTextMap.set(span, child.nodeValue); // Save original text
span.innerHTML = applyBionicReading(child.nodeValue);
child.replaceWith(span);
} else if (child.nodeType === 1) {
applyBionicReadingToNode(child);
}
});
}
function revertBionicReading(node) {
if (node.nodeType === 1) {
Array.from(node.childNodes).forEach(child => {
if (child.nodeType === 1 && originalTextMap.has(child)) {
const originalText = originalTextMap.get(child);
child.innerHTML = originalText; // Restore the original HTML
} else if (child.nodeType === 1) {
revertBionicReading(child);
}
});
}
}
function toggleBionicReading() {
zenReadEnabled = !zenReadEnabled;
if (zenReadEnabled) {
applyBionicReadingToNode(document.body);
observer.observe(document.body, { childList: true, subtree: true });
} else {
observer.disconnect();
revertBionicReading(document.body);
originalTextMap.clear();
}
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'toggleZenRead') {
toggleBionicReading();
sendResponse({zenReadEnabled: zenReadEnabled});
} else if (message.action === 'getState') {
sendResponse({zenReadEnabled: zenReadEnabled});
}
});
// Apply Bionic Reading on page load
// toggleBionicReading();