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

182
Vistas
Insert script into HTML string

Im running on a node server and returning the HTML for a given URL

server.get('*', async (request, response) => {
  const url = request.protocol + '://' + request.get('host') + request.originalUrl;

  const { html } = await renderPage(url, {
    manifest,
    preload: true,
    request,
    response
  });

  //TODO: Insert script into html string in head

  response.end(html);
});

However, what I would like is that I could manipulate the HTML before response.end(), eg inserting a script into the head section of the HTML string... Any ideas on how to do this?

I was thinking at first I somehow could use html.replace(), but that's not the right approach I think...

Any help would be greatly appreciated :)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Not knowing the types but if html is just a string, the .replace() method is an appropriate way to go. Depending on if you have control over the html templates which are rendered you could add a placeholder to add the script to a certain place in the html.

If you have no control I rather would just append it to the end of the body.

const script = `<script src="your/fancy/script.js" />`;

html.replace(/(<\s*\/\s*body)/, `${scripts}\n$1`);

should put the script like

  <div>Some content before</div>
<script src="your/fancy/script.js" />
</body>
</html>
about 4 years ago · Juan Pablo Isaza 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