I am trying to make a chrome extension that finds certain words on pages and turns them into hyperlinks. So for example, if I visit a website that has the word "search" written somewhere, then that word will turn into a link that I can click on (it will still appear as the word but maybe in a different colour or something) and be redirected to "www.google.com".
I have a code that finds words and changes them to other words but I don't know how to change them to hyperlinks. Here is the JavaScript I have:
walk(document.body);
function walk(node)
{
var child, next;
switch ( node.nodeType )
{
case 1:
case 9:
case 11:
child = node.firstChild;
while ( child )
{
next = child.nextSibling;
walk(child);
child = next;
}
break;
case 3:
handleText(node);
break;
}
}
function handleText(textNode)
{
var v = textNode.nodeValue;
v = v.replace(/\bsearch\b/g, (str.link("https://www.google.com")));
v = v.replace(/\bsearch\b/g, <a href="http://www.google.com">asdf</a>);
textNode.nodeValue = v;
}
Can somebody helpful help me out please?