1

What I want to do

First of all, I would like to state that I am not looking for how to run a function by name (that's explained in this question).

What I want to do is something like this:

function myFunc(){
    // some code here
}

myFunc; // this should run the function

// I know it doesn't, but read below

Why I want to do this

There's a Google Easter egg, that if you type in text adventure into the search bar and open the console, you can play a text adventure game.

It asks,

Would you like to play a game? (yes/no)

And if you type in yes (just the word yes), then it will start the real game. I am trying to figure out how it does that (you didn't type in the () after it, and I already learned that you can't see what the user types in the console), but can't figure out what they are doing.

My guess was that they are able to run a function when you type in yes (maybe a function called yes?), so that's why I asked this question to understand what they really are doing.

To prevent an XY question: what I really want to do is be able to run some code when the user types something ('yes' maybe) into the console. It's just I think that they are running functions when that happens, so that's why I asked this specific question.

Question

So how is it possible to use the console as an input like Google's text adventure?

Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34
  • 2
    There are ways but it's just the wrong direction anyway. You should read the user input into a variable and then act on it - compare it to known values. – CherryDT Apr 22 '21 at 20:12
  • @CherryDT: OK, well maybe read it into variables - but how? They entered something in the *console*, not a real ``. – Lakshya Raj Apr 22 '21 at 20:13
  • 1
    And I appreciate that you explained what you _really_ wanted to do, because your question is a perfect example for an XY question indeed! :) – CherryDT Apr 22 '21 at 20:14
  • Oooh I missed this part that it's just something in the console. Sorry about that. I cannot reproduce this easter egg though, for me no "do you want to play a game" prompt appears. Anyway, I guess you could create getters on the `window` object with your code. For example: `Object.defineProperty(window, 'yes', { get: () => console.log('OH YEAH') })` – CherryDT Apr 22 '21 at 20:15
  • 1
    `Object.defineProperty(window, 'yes', { get() { console.log("let's play") } })` – deceze Apr 22 '21 at 20:15
  • @CherryDT @ deceze: That's exactly what I wanted - although I'm not sure why my question was closed for something that has nothing to do with the console. Thanks! – Lakshya Raj Apr 22 '21 at 20:17
  • @CherryDT, `Object.defineProperty` is part of the first duplicate. – Nina Scholz Apr 22 '21 at 20:20
  • @NinaScholz: Oh, you have to scroll down the solutions to see the solution. OK, that makes more sense. Weird that I didn't see that question while searching. Must have been that I used words that didn't match the one of that question. – Lakshya Raj Apr 22 '21 at 20:23

0 Answers0