Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Insertar script en cadena HTML

Estoy ejecutando en un servidor de nodo y devolviendo el HTML para una URL determinada

 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); });

Sin embargo, lo que me gustaría es poder manipular el HTML antes de que responda.end(), por ejemplo, insertando un script en la sección principal de la cadena HTML... ¿Alguna idea sobre cómo hacer esto?

Estaba pensando al principio que de alguna manera podría usar html.replace(), pero ese no es el enfoque correcto, creo...

Cualquier ayuda sería muy apreciada :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sin conocer los tipos, pero si html es solo una cadena, el método .replace() es una forma adecuada de hacerlo. Dependiendo de si tiene control sobre las plantillas html que se representan, puede agregar un marcador de posición para agregar el script a un lugar determinado en el html.

Si no tiene control, prefiero agregarlo al final del cuerpo.

 const script = `<script src="your/fancy/script.js" />`; html.replace(/(<\s*\/\s*body)/, `${scripts}\n$1`);

debe poner el script como

 <div>Some content before</div> <script src="your/fancy/script.js" /> </body> </html>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!