insertText
テキストボックス((textarea, input:-moz-any(:not([type]), [type=text], [type=password])
)) tbox のカーソル位置へ文字列 text を挿入したいとき,HTMLTextAreaElement の貧弱な機能で実現するとこんな風になる。
var {value, selectionStart, selectionEnd} = tbox; tbox.value = value.slice(0, selectionStart) + text + value.slice(selectionEnd); tbox.selectionEnd = selectionStart + text.length;
一見うまくいくようだが,スクロール位置がリセットされるしアンドゥも狂う。
自然にやるには chrome 権限が必要。*1
const Ci = Components.interfaces; tbox instanceof Ci.nsIDOMNSEditableElement; tbox.editor.QueryInterface(Ci.nsIPlaintextEditor).insertText(text);