0

I want to trigger the web console after a click on button with pure JavaScript. I think I can trigger with Ctrl + Shift + K combination but I don't know how to simulate it. I found some solutions but they're only for single keys.

Related for single keys: Trigger a keypress/keydown/keyup event in JS/jQuery?

How can I do this?

rch
  • 127
  • 1
  • 7
  • Easy, you don't it's an inappropriate use of Javascript and a user's machine. Many browsers will block this behavior. To sum it's keylogging territory and though we're given control inside the browser to an extent this doesn't touch the window API. E.g. you cannot take such ability outside the page without hacks. – BGPHiJACK Feb 04 '21 at 12:04
  • Actually I wanted to direct user to see the logs from console only with a click, no safe way is there? – rch Feb 04 '21 at 12:09
  • That'd be a better question, and as for what the better approach would be, can't say but hopefully an idea can spark with the demonstration below. :) – BGPHiJACK Feb 04 '21 at 12:23
  • Thank you but it does not seem like what I want or it doesn't look like a solution or spark which I can use for this problem. – rch Feb 05 '21 at 15:03

1 Answers1

0

This could be one way, without needing to go all fancy with macro; I'd change it up a bit for my use but you could load that to an element with ease all sorts.

let Logger = { normal: [], warn: [], error: [] };
const NewConsoleLog = console.log;

console.log = (e) => Logger.normal.push(e);
console.warn = (e) => Logger.warn.push(e);
console.error = (e) => Logger.error.push(e);
console.log("test");
console.log("test");
console.log("test");
console.warn("warrrninggggggsss");
console.error("error alert!!!!");
console.error("error alert!!");

NewConsoleLog(Logger);
BGPHiJACK
  • 1,277
  • 1
  • 8
  • 16
  • Sometimes a practice not commonly used is re-defining a function to behave how you want it to, so instead of sending console.log to console or its fellow warn/error we store it and call it when we want to; for reference to the console a constant of the original function had been made called NewConsoleLog. – BGPHiJACK Feb 04 '21 at 12:30