I am trying to inject some JS into a webpage using my Chrome extension's content script. The injected script declares a variable, which prints okay when printed from the same injected script. However, if I try to access the same variable later in the page or even in the console, it is not found. Please note that I am not declaring the variable in my content script, I am injecting JS in the actual webpage.
Here is the minimal code from content script:
var script = `<script>
var zzxx = "random stuff";
console.log(zzxx);
window.xyz = "1234";
console.log(window.xyz);
</script>`;
$('body').append(script);
Output:
random stuff
1234
But cannot access zzxx or window.xyz from console or another injected script. What's the reason?