Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
creating chrome extension to change colour of each word

So as the title suggests I've been working on a chrome extension to change the colour of each word in a website (to help with my ADHD reading)

The code I have runs without errors but doesn't seem to do anything. The parts I've commented out is when i was trying to split each paragraph word to word but i gave up on that a bit to temporarily simplify

var paragraph = document.getElementsByTagName("p");
var words = paragraph; //.innerHTML.split(" ");
var colours = ["red", "purple", "blue"];
var spans = [];

for(var i = 0; i<words; i++){
    var colour = colours[Math.floor(Math.random()*colours.length)]
    var span = "<span style='color:" + colour + ";'>" + words[i] + "</span>"
    
    spans.push(span);
}

//paragraph.innerHTML = spans.join(" ");
words.innerHTML = spans.join(" ");
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

There are several problems in your code.

  • getElementsByTagName() returns collection of elements (you have to iterate over it)
  • without .split() you will get whole content instead of words
  • Whole innerHtml() and split() solution probably won't work with complex HTML

I would like to suggest you different approach. I would find every word with space with regular expression in whole document (or element) and wrap it in span with specific class and style it with CSS. Something like this:

const root = document.querySelector('p');
root.innerHTML = root.innerHTML.replace(/(^|<\/?[^>]+>|\s+)([^\s<]+)/g, '$1<span class="word">$2</span>');
.word:nth-child(1n-1) {
  color: green;
}
.word:nth-child(2n-1) {
  color: red;
}
.word:nth-child(3n-1) {
  color: blue;
}
<p>
 some words some words
 some words some words
 some words some words
</p>


If you want random colors, you can change second parameter of replace with function which is being called every iteration. Something like this:

const root = document.querySelector('p');
const colors = ["red", "purple", "blue"];
root.innerHTML = root.innerHTML.replace(
  /(^|<\/?[^>]+>|\s+)([^\s<]+)/g, 
  (match) => {
    const color = colors[Math.floor(Math.random()*colors.length)];
    return ` <span style="color: ${color}">${match}</span>`
  }
);
<p>
 some words some words
 some words some words
 some words some words
</p>

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda