-
Notifications
You must be signed in to change notification settings - Fork 0
/
addDaysToPageWithDates
35 lines (32 loc) · 1.28 KB
/
addDaysToPageWithDates
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
/* [https://stackoverflow.com/questions/5558613/replace-words-in-the-body-text] */
/* TEXT HTML
<!DOCTYPE html><head><meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<p>3. 10.</p>
<p>16.10.2023 13:00 (Délka semináře)</p>
<p>11.10.2023 13:00 (Délka semináře)</p>
</html>
*/
function addDay(str){
/* edit date - add days to czech formatted date */
var elems = str.split(".");
if (elems.length == 4)
var mydate = new Date( Date.parse(elems[1] + "/" + elems[0] + "/" + elems[2] ) );
else
var mydate = new Date( Date.parse(elems[1] + "/" + elems[0] + "/" + new Date().getFullYear() ) );
days = ["ne","po","út","st","čt","pá","so","ne"];
return days[mydate.getDay()];
}
function walkText(node) {
/* go through all text of page; better then innterHTML replace - it causes damage of css */
if (node.nodeType == 3) {
/* [https://stackoverflow.com/questions/2447915/javascript-string-regex-backreferences] */
node.data = node.data.replace(/[0-9]{1,2}\. ?[0-9]{1,2}\. ?[0-9]{0,4}/g, function(match, $1, $2, offset, original) { return "(" + addDay($2) + ") " + match});
}
if (node.nodeType == 1 && node.nodeName != "SCRIPT") {
for (var i = 0; i < node.childNodes.length; i++) {
walkText(node.childNodes[i]);
}
}
}
/* call main function */
walkText(document.body);