James Patterson James Patterson - 1 year ago 72
Javascript Question

Javascript add text to the head of document

I am writing a simple Atom.io package and would like to add a generic string to the head of the document using JavaScript :

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


I can not understand why the text is being inserted at the position of the cursor and not at the head of the document.

Answer Source

Reposition the cursor to the head of the page using:

  editor.selectToTop();

Then add the desired text using:

  editor.insertText("Sample Text");