我们如何知道是从控制台还是从源代码调用函数

时间:2014-07-26 01:53:21

标签: javascript google-chrome firefox google-chrome-devtools firefox-developer-tools

我想知道是否有办法检查是否从浏览器的控制台或源代码调用了javascript函数。

我定义了一个功能,可以检查它是来自控制台还是来自页面,但它仅适用于谷歌浏览器,它不能在Firefox中运行,我没有测试其他浏览器< / p>

function fromConsole()
{
    var Caller = arguments.callee.caller;
    while(Caller.caller != null)
        Caller = Caller.caller;
    return (Caller.toString().indexOf("function (expression, objectGroup,"))!=-1;
}

此功能的工作原理

此函数查找调用我们函数的top函数。在google chrome中,如果从控制台调用top函数的定义在firefox中包含此字符串function (expression, objectGroup,,则没有函数

让我详细解释一下

让我们说我们有这个例子

function a()
{
    b();
}
function b()
{
    return c();
}
function c()
{
    console.log(fromConsole());
}

如果我们从页面调用函数a(),它会在控制台中显示false(因为top函数是a())但是,如果我们从控制台调用它,则显示为true,因为top函数是这个& #34; function (expression, objectGroup,...&#34;

在firefox中,top函数始终是a(),你可以从控制台或页面调用你的函数

我的问题是:我们是否可以知道是否从控制台调用该函数?

3 个答案:

答案 0 :(得分:10)

在Chrome中,控制台始终调用中间JavaScript函数,在Firefox中调用直接来自本机代码。因此,您可以在Chrome中检查arguments.callee.caller,但在Firefox中,它将始终为null。 Safari在这里的行为与Firefox相同,因此检查调用者实际上是一种仅适用于Chrome的技巧。

你可以检查的是Error.stack property。以下功能适用于Firefox,Chrome甚至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;
}

这会向上走,直到找到与控制台对应的条目。这意味着fromConsole()不需要直接调用,中间可以有任意数量的其他函数调用。不过,它很容易被欺骗,例如,使用setTimeout()

setTimeout(fromConsole, 0);

这里调用者将是本机超时处理程序,不再指向控制台。

答案 1 :(得分:4)

这是一种跨浏览器的方式,可以查看它是从公共(全局,由js控制台调用)还是私有(您的代码)上下文调用的:

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

}) ()

外部函数中的代码将使用私有函数,而从控制台运行f()将运行公共函数。

答案 2 :(得分:1)

对于Chrome,您只需检查keys功能是否可用。它是chrome Command Line API的一部分,仅在从控制台执行代码时才可用

function myFunction() {
  var fromConsole = typeof keys === 'function' && keys.toString().indexOf('Command Line API') !== -1
  if (fromConsole) {
    alert('From console')
  } else {
    alert('Not from console')
  }
}