I wrote a small script that displays the name of a connected user (and the user who switched a device) instead of only showing the IP address in the Domoticz log.
Because we have many users on our local network who use the default local account, it was sometimes difficult to identify who actually connected to Domoticz or operated a device.
To use it, simply add the script to your custom.js file. After configuring the static IP addresses and their corresponding names, Domoticz will display the assigned name together with the IP address in the log.
It might be useful for others as well.
Code: Select all
(function () {
"use strict";
const clientNames = {
"127.0.0.1": "localhost",
"192.168.1.3": "localdocker",
"192.168.1.39": "BarryMuziekPC",
"10.8.0.2": "myvpn",
"10.8.0.3": "mygirlfriendvpn",
"10.8.0.4": "othervpn",
"192.168.1.90": "user1",
"192.168.1.91": "otheruser2",
"192.168.1.92": "otheruser3",
"192.168.1.93": "otheruser4"
};
const sortedClients = Object.entries(clientNames)
.sort((a, b) => b[0].length - a[0].length);
function escapeRegex(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function replaceClientIPs(root) {
if (!root) return;
const walker = document.createTreeWalker(
root,
NodeFilter.SHOW_TEXT
);
const textNodes = [];
while (walker.nextNode()) {
textNodes.push(walker.currentNode);
}
for (const node of textNodes) {
let text = node.nodeValue;
for (const [ip, name] of sortedClients) {
const escapedIp = escapeRegex(ip);
const replacement = `${name} (${ip})`;
const mappedRegex = new RegExp(
`::ffff:${escapedIp}(?!\\d)`,
"g"
);
const ipv4Regex = new RegExp(
`(?<![\\d.])${escapedIp}(?![\\d.])`,
"g"
);
text = text.replace(mappedRegex, replacement);
text = text.replace(ipv4Regex, replacement);
}
if (text !== node.nodeValue) {
node.nodeValue = text;
}
}
}
function start() {
replaceClientIPs(document.body);
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
if (addedNode.nodeType === Node.TEXT_NODE) {
replaceClientIPs(addedNode.parentNode);
} else if (addedNode.nodeType === Node.ELEMENT_NODE) {
replaceClientIPs(addedNode);
}
}
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", start);
} else {
start();
}
})();