Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda