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

后端 未结 3 832
青春惊慌失措
青春惊慌失措 2020-12-16 05:37

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 ch

相关标签:
3条回答
  • 2020-12-16 06:01

    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.

    0 讨论(0)
  • 2020-12-16 06:03

    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("\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.

    0 讨论(0)
  • 2020-12-16 06:04

    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' && keys.toString().indexOf('Command Line API') !== -1
      if (fromConsole) {
        alert('From console')
      } else {
        alert('Not from console')
      }
    }
    
    0 讨论(0)
提交回复
热议问题