Skip to content

Instantly share code, notes, and snippets.

@magasine
Last active February 19, 2023 13:21
Show Gist options
  • Save magasine/2e336f5198969a0a95eda917516818cd to your computer and use it in GitHub Desktop.
Save magasine/2e336f5198969a0a95eda917516818cd to your computer and use it in GitHub Desktop.
Destaca Ag-Contrato v20230219 (bookmarklet)
javascript: (function () {
var pattern = /(?<=\s|^)(?<gSR>\d{2})([\.\/\s-])?(?<gAG>\d{1,4})([\.\/\s-])?(?<gOP>\d{3})([\.\/\s-])?(?<gCONTRATO>\d{1,7})([\.\/\s-])?(?<gDV>\d{2})(?=\s|$)/g;
var textNodes = [];
function recurse(node) {
if (node.nodeType === Node.TEXT_NODE) {
textNodes.push(node);
} else {
for (var i = 0; i < node.childNodes.length; i++) {
recurse(node.childNodes[i]);
}
}
}
recurse(document.body);
for (var i = 0; i < textNodes.length; i++) {
var node = textNodes[i];
var matches = Array.from(node.nodeValue.matchAll(pattern));
if (matches.length === 0) {
continue;
}
var div = document.createElement("div");
div.setAttribute("id", "div_contrato");
// div.style.border = "1px dotted blue";
div.style.display = "inline-block";
var lastIndex = 0;
for (let j = 0; j < matches.length; j++) {
const match = matches[j];
var index = match.index;
var before = node.nodeValue.substring(lastIndex, index);
if (j == 0 && before.trim() !== '') {
var textNode = document.createTextNode(before);
div.appendChild(textNode);
} else if (before.trim() !== '') {
var textNode = document.createTextNode(before);
div.appendChild(textNode);
}
var parts = {
gSR: match.groups.gSR,
gAG: match.groups.gAG.padStart(4, "0"),
gOP: match.groups.gOP,
gCONTRATO: match.groups.gCONTRATO.padStart(7, "0"),
gDV: match.groups.gDV,
};
var color = j % 2 === 0 ? "yellow" : "lightgreen";
for (const [key, value] of Object.entries(parts)) {
var partSpan = document.createElement("span");
partSpan.classList.add(key);
partSpan.style.backgroundColor = color;
partSpan.style.padding = "2px";
partSpan.style.border = "1px solid gray";
partSpan.style.borderRadius = "10px";
partSpan.appendChild(document.createTextNode(value));
div.appendChild(partSpan);
color = color === "yellow" ? "lightgreen" : "yellow";
}
lastIndex = index + match[0].length;
}
if (lastIndex < node.nodeValue.length) {
var lastText = document.createTextNode(node.nodeValue.substring(lastIndex));
div.appendChild(lastText);
}
if (div.childNodes.length > 0) {
node.parentNode.replaceChild(div, node);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment