最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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 ?

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 ?

Share Improve this question edited Jul 28, 2014 at 7:33 Wladimir Palant 57.7k12 gold badges99 silver badges127 bronze badges asked Jul 26, 2014 at 1:53 KhalidKhalid 4,8085 gold badges28 silver badges50 bronze badges 4
  • I think "console.error('hello world')" maybe can help you. it will backtrace on browser like this: screenshot – tureki Commented Jul 26, 2014 at 2:13
  • In firefox it doesn't backtrace. and I need a function not something I can do manually – Khalid Commented Jul 26, 2014 at 3:20
  • 5 What problem are you trying to solve? – Pointy Commented Jul 26, 2014 at 4:17
  • If you are trying to do some kind of of anti-cheating, Chrome allows the user to edit the JavaScript in place and thus could add a function call inside the code. There are proxies servers that can alter the script in transit as well. – Jeremy J Starcher Commented Jul 26, 2014 at 4:41
Add a ment  | 

3 Answers 3

Reset to default 15 +50

In Chrome the console always calls intermediate JavaScript functions, in Firefox the call es 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("\n");
    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.

This is a cross-browser way of seeing if it was called from a public (global, called by js console), or a private (your code) context:

(function() { 
    window.f = function() {
        console.log('public')
    } ;
    //f will be this function in the rest of the code in this outer function:
    var f = function() {
        console.log('private'); 
    }
    f();
    //more code here...

}) ()

The code within the outer function will use the private function, while running f() from console will run the public function.

For Chrome, you could just check to see if the keys function is available. It's part of chrome's Command Line API and only available when the code was executed from the console

function myFunction() {
  var fromConsole = typeof keys === 'function'
  if (fromConsole) {
    alert('From console')
  } else {
    alert('Not from console')
  }
}
发布评论

评论列表(0)

  1. 暂无评论