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

442
Vistas
Change content of document.scripts[0].firstChild

Is it possible to change the content of document.scripts[0].firstChild with a textual replacement? And in such a way that the changes affect the code.

e.g.

function hamjam() {
    console.log("__hamjam__");
}
document.scripts[0].firstChild.textContent = document.scripts[0].firstChild.textContent.replaceAll("__hamjam__", "XXX")

Calling hamjam now should log XXX instead of __hamjam__ (which it does not).

Important, what I try to do is highly experimental and more a proof of concept then something I try to do in production. The goal can be achieved in a really ugly matter, and if it only works with a certain browser is also fine.

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

0

And in such a way that the changes affect the code.

No. You can change the text that's in the script tag, but that text is obsolete by the time you do it. It's been parsed into actual program code in memory. Changing it has no effect on that program code in memory. (In fact, you could remove the script tag entirely and it would have no effect — unlike removing CSS resources, which does.)

Here's an example:

const script = document.getElementById("target-script");
script.textContent = script.textContent.replace("Hi there", "I've updated the message!");
console.log("The new script content is:");
console.log(script.textContent);
console.log("But click the button and see what happens.");
<input type="button" id="the-button" value="Click me">
<script id="target-script">
document.getElementById("the-button").addEventListener("click", () => {
    const message = "Hi there";
    console.log(message);
});
</script>

You've said your example is just a small example of a larger thing, but FWIW, you could replace that function. In very limited situations, that might do what you want (and so probably isn't applicable to your larger pattern).

Here's an example:

console.log("Calling it before the change:");
hamjam();
console.log("Changing it");
let source = Function.prototype.toString.call(hamjam);
source = source.replace(/__hamjam__/g, "XXX");
hamjam = (0, eval("(" + source + ")"));
console.log("Calling it after the change:");
hamjam();
<script id="target-script">
function hamjam() {
    console.log("__hamjam__");
}
</script>

But that only works in very limited situations.

  • Function.prototype.toString has to return a valid representation of the function (which it should do in the normal case, but...)
  • The function has to be assignable in the scope where you're making the change
  • It has to not close over things that it relies on (because your new version won't)

...and probably others.


Side note: That (0, eval("...")) may look odd if you haven't seen it before. As you may know, eval has this...special...ability that it works in local scope. But most of the time (in my experience, YMMV), you don't want the code you're evaluating to have access to local scope. The (0, eval("...")) pattern (0 can be anything) breaks the link with local scope, doing the eval at global scope instead. It's called "indirect eval".

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