1

I want a particular JavaScript function to behave differently depending on if it's called within JavaScript code referenced from an HTML page or called from within a console. Is this possible? Something like the following:

function mySpecialFunc() {
  if (inConsole())
    console.log("You called me from the console!");
  else
    console.log("You called me from an HTML page or a JavaScript file linked from an HTML page, I think.");
}

Does something equivalent to the inConsole() function above exist?

Does this exist for at least just Chrome specifically, or Firefox specifically?

at.
  • 50,922
  • 104
  • 292
  • 461

2 Answers2

2

One way is to throw an error and check the stack trace for a string that is unique to the console's injection. Something like "InjectedScript"

Here is an example that works.

    var f = function(){
      var injected;
      try {
        throw new Error();
      } catch (e) {
        injected = e.stack.match('InjectedScript');
      }
      if (injected) {
        console.log("Called from console");
      } else {
        console.log("Called from code");
      }
    }
    // Add it to window so we can call it from the console.
    window.f = f;
    f();
david
  • 17,925
  • 4
  • 43
  • 57
  • The range of error messages possible is likely much greater than in your answer, does **every** implementation with a console include the exact phrase "InjectedScript" in the error? Is that specified somewhere? – RobG Oct 12 '15 at 00:44
  • I answered before marking as a duplicate. It was only when someone else marked it that I realised it had been asked before. – david Oct 12 '15 at 00:45
  • Fair enough. The general strategy seems to provide a possible answer, however the difference in the error message for code invoked from the console seems to be different in different environments, therefore more extensive testing is required. The above does not work in IE. Also `widnow.f = f` is redundant, `var f` has already created a global variable *f* before any code is executed. – RobG Oct 12 '15 at 00:56
  • Yeah I was only testing in chrome so that's the error message I wound up with. Should probably have mentioned that in the answer but eh, the marked duplicate is way better anyway. – david Oct 12 '15 at 02:10
0

Unfortunately there is no way to tell via system input, but you can do it "Manually" in a sense using overflow/overload functions. See here for a excellent tutorial on how to use overflow/overload in js.

So in your code that calls the function in javascript add an additional argument to the call that will tell the function it is not called from the console.

mySpecialFunc() <---- From console
mySpecialFunc(value) <---- From code
Community
  • 1
  • 1
Jean de Toit
  • 127
  • 9
  • I don't think the linked answer has anything to do with the question. – RobG Oct 11 '15 at 23:39
  • The link is not an answer, it is an tutorial to do overload functions. The answer I am proposing is using overload functions to solve his problem. If he adds an arguement to the code that calls his function within the program and a different set of arguements (none in this case) for the code that calls it from the console then it is maybe possible to differentiate the code. – Jean de Toit Oct 11 '15 at 23:43