-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkify_ico.user.js
79 lines (63 loc) · 2.59 KB
/
linkify_ico.user.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Separate script for identity numbers
// ==UserScript==
// @name Linkify Identity Numbers
// @namespace https://github.com/kofaysi/general-userscripts
// @version 1.0
// @description Parses the page content for identity numbers and linkifies them.
// @author https://github.com/kofaysi/
// @match *://*/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
const identityRegex = /^(?:IČ|IČO|ID|DIČ)\s?:?\s?(CZ)?(?:\d\s*){7,8}$/gi;
function formatIdentity(text) {
return text.replace(/\D/g, '').padStart(8, '0');
}
function createIdentityLink(identityNumber) {
const formattedIdentity = formatIdentity(identityNumber);
const a = document.createElement('a');
a.href = `https://or.justice.cz/ias/ui/rejstrik-$firma?ico=${formattedIdentity}`;
a.textContent = identityNumber;
a.style.color = '#FF5733';
return a;
}
// Function to linkify the detected texts
function linkifyText(node, regex, createLinkFn) {
const walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false);
const textNodes = [];
let currentNode;
while (currentNode = walker.nextNode()) {
textNodes.push(currentNode);
}
textNodes.forEach(textNode => {
const parent = textNode.parentNode;
if (parent.tagName.toLowerCase() === 'a') {
return;
}
const matches = textNode.nodeValue.match(regex);
if (matches) {
const fragment = document.createDocumentFragment();
let lastIndex = 0;
matches.forEach(match => {
const matchIndex = textNode.nodeValue.indexOf(match, lastIndex);
if (matchIndex !== lastIndex) {
fragment.appendChild(document.createTextNode(textNode.nodeValue.slice(lastIndex, matchIndex)));
}
const link = createLinkFn(match);
fragment.appendChild(link);
lastIndex = matchIndex + match.length;
});
if (lastIndex < textNode.nodeValue.length) {
fragment.appendChild(document.createTextNode(textNode.nodeValue.slice(lastIndex)));
}
parent.replaceChild(fragment, textNode);
}
});
}
function processDocument() {
const body = document.body;
linkifyText(body, identityRegex, createIdentityLink);
}
window.addEventListener('load', processDocument);
})();