Created
February 19, 2023 19:47
-
-
Save magasine/89efe34fbcb72140e4d668c80a1164ed to your computer and use it in GitHub Desktop.
! Datas, Semáforo de (v20230219) - bookmarklet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var datePattern = /(\d{2})\/(\d{2})\/(\d{4})/; | |
var today = new Date(); | |
var dateNodes = []; | |
function isToday(date) { | |
var now = new Date(); | |
return date.getDate() === now.getDate() && | |
date.getMonth() === now.getMonth() && | |
date.getFullYear() === now.getFullYear(); | |
} | |
function recurse(node) { | |
if (node.nodeType === Node.TEXT_NODE) { | |
var matches = node.nodeValue.match(datePattern); | |
if (matches) { | |
var date = new Date(matches[3], matches[2] - 1, matches[1]); | |
if (date <= new Date(today.getTime() - 15 * 24 * 60 * 60 * 1000)) { | |
dateNodes.push({ node: node, style: "2px solid orange" }); | |
} else if (date < new Date(today.getTime() - 24 * 60 * 60 * 1000)) { | |
dateNodes.push({ node: node, style: "2px solid green" }); | |
} else if (isToday(date)) { | |
dateNodes.push({ node: node, style: "2px solid red" }); | |
} else { | |
dateNodes.push({ node: node, style: "2px solid red" }); | |
} | |
} | |
} else { | |
for (var i = 0; i < node.childNodes.length; i++) { | |
recurse(node.childNodes[i]); | |
} | |
} | |
} | |
recurse(document.body); | |
for (var i = 0; i < dateNodes.length; i++) { | |
var node = dateNodes[i].node; | |
var style = dateNodes[i].style; | |
var matches = node.nodeValue.match(datePattern); | |
var index = node.nodeValue.indexOf(matches[0]); | |
var before = node.nodeValue.substring(0, index); | |
var after = node.nodeValue.substring(index + matches[0].length); | |
var dateSpan = document.createElement("span"); | |
var textNode = document.createTextNode(matches[0]); | |
dateSpan.style.border = style; | |
dateSpan.style.display = "inline-block"; | |
dateSpan.appendChild(textNode); | |
var wrapperDiv = document.createElement("div"); | |
wrapperDiv.appendChild(document.createTextNode(before)); | |
wrapperDiv.appendChild(dateSpan); | |
wrapperDiv.appendChild(document.createTextNode(after)); | |
node.parentNode.replaceChild(wrapperDiv, node); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment