I want to make a button in HTML that would do the equivalent of entering a specific command into the Chrome console. How would you go about doing this?
CodePudding user response:
Before you read this know that: Using such methods and specially eval()
ARE NOT recommended at all due to a massive range of huge problems including major security holes and performance issues (please read everything wrong with eval and eval docs as well). therefore please consider other methods or share more details so we may suggest better solutions.
For your case most of the codes should be executable with the example below and the output will be shown in console. (document.querySelector('botton')
can be replaced with your desired code)
function exec(code){
console.log(Function(`return (${code})`)())
}
<button onclick="exec(`document.querySelector('button')`)">Test</button>
As far as I know the code above is extremely vulnerable