SparedWhisle
SparedWhisle

Reputation: 1440

programatically putting text at Chrome console prompt

I defined a function that I run using Chrome console.
So I would type my_function() then hit enter to call my function.

Sometimes I need to call my function many times (the exact number is unknown), so I'll repeat up enter to repeatedly call my function.

I was wondering if I can let my function write my_function() at the prompt after it's execution, so that I'll just need to keep hitting enter to call my function however many times I want?

I don't know much about JavaScript, so my approach could be totally wrong.

Upvotes: 1

Views: 674

Answers (2)

Novy
Novy

Reputation: 1520

First thing first I am ok with the first solution. But in case you really want to stay in the chrome devtool why not using snippets they are the perfect tool for what you what:

-Go to the snippet pane. Write a snippet of your function.

-Call it.

-Keep running it by clicking or use Control+Enter or Command+Enter.

Another solution can be stackoverflow site. Go to any question. your for example in the answer part click on javascript/html/css snippet and do your thing

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 371049

I'd insert a button into the document instead, then click that button as needed:

const button = document.body.appendChild(document.createElement('button'));
button.textContent = 'click me';
button.addEventListener('click', my_function);

If you want to be able to do this sort of thing automatically (have the button inserted on pageload without having to type the code into the console yourself every time), put it into a userscript.

Upvotes: 3

Related Questions