Ich habe den folgende Java-userscript:Hervorhebungen Exact Text Spiel
function highlightWord(word) {
var xpath = "//text()[contains(., '" + word + "')]";
var texts = document.evaluate(xpath, document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (n = 0; n < texts.snapshotLength; n++) {
var textNode = texts.snapshotItem(n);
var p = textNode.parentNode;
var a = [];
var frag = document.createDocumentFragment();
textNode.nodeValue.split(word).forEach(function(text, i) {
var node;
if (i) {
node = document.createElement('span');
node.style.backgroundColor = 'lightgreen';
node.appendChild(document.createTextNode(word));
frag.appendChild(node);
}
if (text.length) {
frag.appendChild(document.createTextNode(text));
}
return a;
});
p.replaceChild(frag, textNode);
}
}
highlightWord('office');
Würde mir jemand in der Lage sein, zu helfen mit, eine Lösung zu finden, nur den genauen Text des highlightWord zusammenzubringen, dass ich hinzufügen? Zum Beispiel möchte ich, dass es nur "Büro" und nicht "Offizier" zusammenbringt.
Danke!