Electron 引入 jQuery(11)

到目前为止,我使用了浏览器API来操作前端。这不是一个编写前端代码好的方法。因此在接下来的几篇文章,我将使用不同的前端框架开发。

从备受喜爱的经典jQuery开始。

安装jQuery

复制代码
npm install jquery

我们可以从以前的项目中复制除了app.js脚本之外的所有代码。

加载jQuery

xml 复制代码
<script src="./node_modules/jquery/dist/jquery.js"></script>

app.js

现在我们可以使用jQuery改写DOM操作。

js 复制代码
function appendInput(command) {
  let e = $(
    `<div class='input-line'>
      <span class='prompt'>$</span>
      <span class='input'></span>
    </div>`)
  e.find('.input').text(command)
  $("#history").append(e)
}

function appendOutput(output) {
  let e = $(`<div class='output'></div>`)
  e.text(output)
  $("#history").append(e)
}

$("form").on("submit", function(e) {
  e.preventDefault()
  let command = $("input").val()
  let output = window.api.runCommand(command)
  appendInput(command)
  appendOutput(output)
  $("input").val("")
  $("input")[0].scrollIntoView()
})

安全

现在,使用字符串 <span class='input'>${command}</span> 是很有吸引力的,但这是不安全的。如果命令包含像<或>特殊字符,应用程序将表现不正确。

还有其他方法可以更有表现力,更安全。例如模板字符串和各种模板库,如 handlebars。

代码

地址:github.com/fengjutian/...

原文:dev.to/taw/electro...

相关推荐
JavaGuide1 小时前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
zhanlang02171 小时前
HTML 零基础入门到企业实战全套教程【第九章 9.3】移动端 HTML 基础适配入门
前端·html
用户69371750013841 小时前
AI 帮我写了大半年的代码,我却越来越不会写代码了
前端·后端·github
skywalk81632 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
彭亚川Allen2 小时前
从零手搓 RAG:用 Elasticsearch 存储与检索向量
前端·后端·面试
jerrywus2 小时前
15 个可以直接安装的 AI Agent Skills:按场景挑
前端·agent·claude
涛涛ing2 小时前
用了三年微前端,我们终于把它拆成了单体应用
前端
去伪存真3 小时前
《最终版》《打死不改版》全传一遍?RAG 系统文件去重实战
前端
anyup3 小时前
【全端支持】 uni-app x 专用请求库:axios 写法,多实例、拦截器、可中断、上传下载
前端·uni-app·unix
费曼学习法3 小时前
6 个 MCP Server 传输层重写实录:超时率 12% → 0.3%
前端·后端