forked from moklick/kontext-browser-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
38 lines (32 loc) · 1.3 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
const getNameRegex = name => new RegExp(`\\b${name}\\b`, 'g');
const getNewString = (item, isEndOfSentence) => `${item.name}, ${item.text[Math.floor(item.text.length * Math.random())]}${isEndOfSentence ? '' : ','}`;
// via http://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page#answer-10730777
function findTextNodes(el) {
const textNodes = [];
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false);
while (walker.nextNode()) {
textNodes.push(walker.currentNode);
}
return textNodes;
}
function checkNames() {
data.forEach(item => item.isPresent = document.body.innerHTML.includes(`${item.forename} ${item.name}`));
}
// extend text content with extension sentences
function extendText() {
findTextNodes(document.body).forEach(node => {
node.textContent = node.textContent.replace(/Björn Höcke/g, 'Bernd Höcke');
data.forEach(item => {
if (item.isPresent) {
const parts = node.textContent.split(getNameRegex(item.name));
for (let i = parts.length - 1; i > 0; i--) {
const isEndOfSentence = /^\s*[^a-z 0-9]/i.test(parts[i]) || parts[i] === '';
parts.splice(i, 0, getNewString(item, isEndOfSentence));
}
node.textContent = parts.join('');
}
});
});
}
checkNames();
extendText();