使用 crosh 从 Chrome 扩展名 [terminalPrivate.sendInput] 创建文件

Creating file using crosh from Chrome extension [terminalPrivate.sendInput]

我正在尝试通过 Chrome 扩展程序与 Chrome OS "crosh" 终端进行交互。我正在使用 Secure Shell 的开发者 ID 来访问 chrome.terminalPrivate。从我最初的尝试开始,我能够启动 crosh 进程和 bash shell。但是,我试图在 ~/Downloads 目录中创建一个文件,但它似乎不起作用。据我所知,该文件从未创建过。

这是我到目前为止整理的代码(我使用 Chromium 开发人员的 code 作为起点):

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var shellCommand = 'shell\n';
var croshName = 'crosh';

window.onload = function() {
  Crosh(null);
  commandTest();
}

function Crosh(argv) {
  this.argv_ = argv;
  this.io = null;
  this.keyboard_ = false;
  this.pid_ = -1;
}

function commandTest() {
  chrome.terminalPrivate.onProcessOutput.addListener(processListener);

  chrome.terminalPrivate.openTerminalProcess(croshName, (pid) => {
    if (pid < 0) {
      window.alert("error!");
    }

    this.pid_ = pid;

    var cmd1 = 'shell\n';
    var cmd2 = 'touch ~/Downloads/test.txt\n';

    chrome.terminalPrivate.sendInput(pid, cmd1,
      function (r1) {
        window.alert(r1);
      }
    );

    chrome.terminalPrivate.sendInput(pid, cmd2,
      function (r2) {
        window.alert(r2);
      }
    );

    chrome.terminalPrivate.closeTerminalProcess(
      this.pid_,
      function(result) {
        window.alert(result);
      }
    );
  });
}

function processListener(pid, type, text){
  window.alert(text);
}

感谢您的帮助!

我在 chromium-hterm group 中回答了你也发布了这个问题

您似乎正试图将一个缓慢的事件驱动进程硬塞到一个快速的同步代码路径中。您需要更改模型以对事件做出反应,而不是在另一端尚未准备好时将输入推入管道。

即从 openTerminalProcess 中删除所有 sendInput 调用并将所有逻辑移至 processListener。需要检查 "text" 中的输出,然后决定下一步发送什么。

基本上你需要实现一个类似于 expect.

的临时解析器