Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

javascript - How can we know if a function is called from console or from source code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Chrome the console always calls intermediate JavaScript functions, in Firefox the call comes directly from native code. As a consequence, you can inspect arguments.callee.caller in Chrome but in Firefox it will always be null. Safari behaves the same as Firefox here, so inspecting the caller is really a trick that only works in Chrome.

What you can check nevertheless is Error.stack property. The following function works in Firefox, Chrome and even Safari:

function fromConsole()
{
    var stack;
    try
    {
       // Throwing the error for Safari's sake, in Chrome and Firefox
       // var stack = new Error().stack; is sufficient.
       throw new Error();
    }
    catch (e)
    {
        stack = e.stack;
    }
    if (!stack)
        return false;

    var lines = stack.split("
");
    for (var i = 0; i < lines.length; i++)
    {
        if (lines[i].indexOf("at Object.InjectedScript.") >= 0)
            return true;   // Chrome console
        if (lines[i].indexOf("@debugger eval code") == 0)
            return true;   // Firefox console
        if (lines[i].indexOf("_evaluateOn") == 0)
            return true;   // Safari console
    }
    return false;
}

This will walk up the stack until it finds an entry corresponding to the console. This means that fromConsole() doesn't need to be called directly, there can be any number of other function calls in between. Still, it can easily be tricked, e.g. by using setTimeout():

setTimeout(fromConsole, 0);

Here the caller will be the native timeout handler, nothing pointing to the console any more.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...