Set multiple values to variable and use that variable in DOM

Viewed 41

I'm trying to replace some words in DOM with chrome extension. While the hard coded word works and is replaced, I don't know how to do it dynamically, e.g. to loop through several words and if any of them is on web page, it should be used as a variable and replaced on page. Since I'm a n00b (obviously), I hope I'm making sense.

Here's the code:

const Key = ('covid-19', 'coronavirus', 'covid'); // TODO: use this as a variable in innerhtml below

const words = document.querySelectorAll('h1, h2, h3, h4, h5, h6, p, li, td, caption, span');

let div = document.createElement('div');
div.id = 'tag';
let text = document.createTextNode('TAG');

for(let i = 0; i < words.length; i++){
    if(words[i].innerHTML.includes('COVID-19')|| words[i].innerHTML.includes('COVID-19')){
       words[i].innerHTML = words[i].innerHTML.replace('COVID-19', '<a href="https://www.cdc.gov/">ALERT</a>');
       div.appendChild(text);
}

Thanks!

0 Answers
Related