【问题标题】:How to run a console command with a button in HTML?如何使用 HTML 中的按钮运行控制台命令?
【发布时间】:2022-01-14 15:09:48
【问题描述】:

我想在 HTML 中制作一个按钮,相当于在 Chrome 控制台中输入特定命令。你会怎么做呢?

【问题讨论】:

    标签: html google-chrome button console


    【解决方案1】:

    在阅读本文之前要知道:使用这些方法,特别是 eval() 不建议使用,因为包括重大问题在内的大量巨大问题安全漏洞和性能问题(请同时阅读everything wrong with eval和eval docs)。因此请考虑其他方法或分享更多细节,以便我们提出更好的解决方案。

    对于您的情况,大多数代码应该可以通过下面的示例执行,并且输出将显示在控制台中。 (document.querySelector('botton') 可以替换为您想要的代码)

    function exec(code){
       console.log(Function(`return (${code})`)())
    }
    <button onclick="exec(`document.querySelector('button')`)">Test</button>

    据我所知,上面的代码非常容易受到攻击

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多