Well, I've managed to come up with this, although it spams the alert for every instance of the word. How could I make it only alert once?
js code:
// ==UserScript==
// @name Alert
// @namespace --
// @description Alert
// @include *
// ==/UserScript==
(function() {
const WORD = {
"test": {"color": "red"},
};
function highlightText() {
var allTextNodes = document.evaluate('//text()', document, null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i = 0; i < allTextNodes.snapshotLength; i++) {
var ele = allTextNodes.snapshotItem(i);
for (var key in WORD) {
if (ele.nodeValue.toLowerCase().indexOf(key) != -1) {
var span = document.createElement("span");
ele.parentNode.replaceChild(span, ele);
span.appendChild(ele);
for (var css in WORD[key]) {
span.style[css] = WORD[key][css];
alert("test");
}
}
}
}
}
highlightText();
})();
Also, what if the word wasnt actually hardcoded into the page source, but retrieved from a session, or something, how could I detect the word then?