I want to know if there is a way to check if a javascript function is being called from console of the browser or from source code.
I defined a function that can check if it's from console or from the page but it works only in google chrome, it doesn't work in firefox, I didn't test other browsers
function fromConsole()
{
var Caller = arguments.callee.caller;
while(Caller.caller != null)
Caller = Caller.caller;
return (Caller.toString().indexOf("function (expression, objectGroup,"))!=-1;
}
How this function works
this function looks for the top function that called our function. in google chrome the definition of the top function if its being called from console contains this string function (expression, objectGroup,
in firefox, there is no function
Let me explain to you in details
let's say we have this example
function a()
{
b();
}
function b()
{
return c();
}
function c()
{
console.log(fromConsole());
}
If we call the function a() from the page , it shows in the console false (because the top function is a() ) however, if we call it from console it shows true, because the top function is this "function (expression, objectGroup,...
"
In firefox, the top function is always a() wether you call your function from console or from your page
My question is : is there a way we can know if the function is called from console or not ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…