使用 JavaScript 模拟 Linux 终端

通过 JavaScript 仿Linux终端控制台,假定目录结构由 json格式数据描述

root: {
   
  pathA: {
   
    game: {
   },
    toy: {
   },
  },
  pathB: {
   
    project1: {
   },
    project2: {
   },
  },
  pathC: {
   
    image: {
   },
    data: {
   },
  },
}

文件代码

两个文件,一个是 html,一个是 js

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Rosetta Interview: VirtualConsole</title>

  <style>
    html,
    body {
     
      height: 100vh;
      width: 100vw;
      margin: 0;
      padding: 0;
      overflow: hidden
    }

    #console {
     
      color: #d3d3d3;
      background-color: #000;
      border: 1px solid #fff;
      border-radius: 10px;
      flex-direction: column;
      align-items: stretch;
      padding: 7px 13px;
      display: flex;
      position: absolute;
      inset: 3em;
      overflow-y: scroll
    }

    #console>div:last-child {
     
      display: flex
    }

    #console>div:last-child:before {
     
      content: "$root > "
    }

    #console>div:last-child input {
     
      color: #fff;
      background-color: #0000;
      border: none;
      outline: none;
      flex: 1
    }

    #console #history .cmd:before {
     
      content: "$root > ";
      font-weight: 700;
    }
  </style>

  <script type="module">
    class n {
      }

    class e extends n {
     
      async read() {
     
        return ''
      }
    }

    class t extends n {
     
      async write(n) {
     
        this.format && (n = this.format(n)), o(n)
      }
    }

    // n:command/first_arg, e:{ className }/undefined
    function o(n, e) {
     
      const t = document.createElement("div");
      for (const [n, o] of Object.entries(e || {
     })) t[n] = o;
      t.innerText = n, c("history").append(t), c("input").scrollIntoView()
    }

    function c(n) {
     
      return document.getElementById(n)
    }

    window.addEventListener("load", (async function n() {
     
      window.removeEventListener("load", n);

      const r = await async function () {
     
        return new Promise((function (n, e) {
     
          const t = document.createElement("script");
          t.src = "./sh.js", t.onload = function () {
     
            t.remove()
          },
            document.head.append(t), document.addEventListener("sh.js", (function e(t) {
     
              document.removeEventListener("sh.js", e);
              const o = t.detail;
              n(o)
            }))
        }))
      }(), s = new e, i = new t, d = new t;

      c("console")?.addEventListener("click", (function () {
     
        const n = document.getSelection();
        n && n.toString() || c("input")?.focus()
      }));

      c("input")?.addEventListener("keyup",
        (function (n) {
     
          if ("Enter" !== n.code) return;
          if (!(n.currentTarget instanceof HTMLInputElement)) return;
          const e = n.currentTarget.value.trim();
          if (!e) return;
          n.currentTarget.value = "";
          o(e, {
     
            className: "cmd"
          });
          
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值