Is it possible to have a global reference only be available from a JS console, but not available to other JS running on a page?
For example, could I set a variable m
to 10
, so that...
<script>
console.log(m);
</script>
Is a ReferenceError
, but from the console...
console.log(m);
Prints 10
?
Edit:
The responses so far only touch on the basics of scope in JavaScript, but if this is possible, it's going to be way more hacky than adding a variable to a scope that only the console can see (yes, I know this doesn't exist). Here's an almost-working example of what I'm looking for:
Object.defineProperty(
window,
"m",
{
get : function window_m_getter() {
if (CONSOLE_OPEN) {
return 10;
} else {
throw new ReferenceError('m can only be used in a console');
}
},
}
);
This example would work if it were possible to detect whether the console was currently open, which it really isn't, but it's pretty close.
The answer to this question is almost certainly no, but it isn't entirely because of scope.