-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
65 lines (54 loc) · 2.24 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
// Merges lines with only a grade as content with the previous line so the whole context gets colour coded
function mergeNodesWithParenthesisContent(arr) {
for (let i = 0, merged = false; i < arr.length; i++) {
if (merged) {
merged = false;
continue;
}
const current = arr[i];
const next = arr[i + 1];
if (next && /^\(\s*\d+\.\d+\s*betyg\s*\)$/.test(next)) {
arr[i] = current + next;
arr.splice(i + 1, 1);
merged = true;
}
}
return arr;
}
(function () {
// Get all the paragraphs within the entry-excerpt div
const paragraphs = document.querySelectorAll('.entry-excerpt');
// Loop through each paragraph
paragraphs.forEach(function (paragraph) {
// Get the innerHTML of the paragraph and split it by line break
let lines = paragraph.innerHTML.split('\n');
lines = mergeNodesWithParenthesisContent(lines);
// Create a new array to hold the new lines with span tags
const newLines = [];
// Loop through each line
lines.forEach(function (line) {
const match = line.match(/\(\s*(\d+\.\d+)\s*betyg\s*\)/);
// If the line has content
if (line.trim().length > 0 && match) {
// Create a new span element
const span = document.createElement('span');
// Check the line value and set the appropriate style
const value = match ? parseFloat(match[1]) : NaN;
if (value >= 1 && value <= 2.99) {
span.style.color = 'red';
} else if (value >= 3 && value <= 3.99) {
span.style.color = 'darkgoldenrod';
}
// Set the innerHTML of the span to the line
span.innerHTML = line;
// Replace the original line with the styled span in the new lines array
newLines.push(span.outerHTML);
} else {
// If the line is empty, add it to the new lines array
newLines.push(line);
}
});
// Join the new lines and set the paragraph innerHTML
paragraph.innerHTML = newLines.join('');
});
})();