Javascript 将文本添加到文档的头部

Javascript add text to the head of document

我正在编写一个简单的 Atom.io 程序包,并希望使用 JavaScript 将通用字符串添加到文档的头部:

var editor = atom.workspace.getActiveTextEditor();
    if (editor) {
      console.log("Package Active");
      var selection = editor.getSelectedText();
      editor.insertText("Sample Text" + selection);
    }
  }

我不明白为什么文本被插入到光标所在的位置而不是文档的开头。

使用以下方法将光标重新定位到页首:

  editor.selectToTop();

然后使用以下方式添加所需的文本:

  editor.insertText("Sample Text");