0

I'm building a website with Glitch. When you press F12 on the webpage and enter the console, I want to be able to respond to a specific input to produce an outcome. Like, if the user types 'hello world' it should log 'lorem ipsum' in the console in response. I've tried readline() but that doesn't seem to work.

//in script.js:

const readline = require('readline');
var y = readline();

if (y == "hello world"){
    console.log("lorem ipsum");
  }

What am I doing wrong? Or does readline() not work?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • You want the webpage to read input from the console? Why not provide an input box or textarea? Even using `prompt` would be better UX than making someone open the browser console, I'd imagine. – ggorlen Mar 22 '21 at 21:41
  • @ggorlen Well, I should've clarified this; I want it to be discreet, which is why I want to use console. I could probably create a new hidden file with a textbox, but I'd prefer console. – threesodas Mar 22 '21 at 22:36
  • Could you explain this rather unusual use case a bit further? What are you really trying to achieve here? See [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Thanks. – ggorlen Mar 22 '21 at 22:39
  • Great, thanks. That helps me to explain it better. I'm making a web application in Glitch, and here's what I want to happen: the user inputs anything. In this case, let's say apple. When the console detects that the user has input 'apple', it should put 'banana'. `if (x == "apple") {` `console.log("banana");` `}` – threesodas Mar 23 '21 at 16:24
  • Also, it's supposed to be an "easter egg", I suppose, so that's why I don't want a textbox. If Google doesn't allow you to have custom actions in console, then that would be the easiest way to answer this. However if possible, I'd like to know how to do this. I'll update this Q with the full code. – threesodas Mar 23 '21 at 16:31
  • That makes more sense. Thanks for the explanation. You could try something in [this thread](https://stackoverflow.com/questions/3120761/how-do-i-get-console-input-in-javascript) but `readline` seems legacy/spidermonkey. Maybe provide an secret function for the user to call? – ggorlen Mar 23 '21 at 18:25

0 Answers0