javascripthtmlevernote

How to use innerHTML or innerText to insert into the Evernote editor which is the rich text editor?


I use the code by Jayant in this post how-to-insert-text-into-the-textarea-at-the-current-cursor-position to insert to the Evernote editor.

As Evernote editor is the rich text editor, I change the el.value to el.innerText, when I run it, the original formate is upset, and the new text is not where the cursor is, it always inserts to the top line of the editor.

So how to use innerHTML or innerText to insert into the Evernote editor?

function typeInTextarea(newText, el = document.activeElement) {
  const start = el.selectionStart
  const end = el.selectionEnd
  const text = el.innerText
  const before = text.substring(0, start)
  const after  = text.substring(end, text.length)
  el.innerText = (before + newText + after)
  el.selectionStart = el.selectionEnd = start + newText.length
  el.focus()
}

Solution

  • Use this code and test in WYSIWYG HTML editor in Chrome, it works.

    document.execCommand("insertText", "false", text);