generated at
replaceText
2021-01-10 19:56:29 もうメンテしていません
代わりにscrapbox-insert-textを使ってください
hr
oldText の文字数だけ Delete を実行した後に、 insertText を挿入する関数
script.js
export function replaceText(cursor, oldText, newText) { const isFirefox = () => { const userAgent = window.navigator.userAgent.toLowerCase(); if (userAgent.indexOf('firefox') != -1) { return true; } return false; }; setTimeout(() => { // Deleteを実行 for(const _ of oldText.match(/./ug)) { cursor.dispatchEvent(new KeyboardEvent('keydown', {bubbles: true, cancelable: true, keyCode: 46})); } // Insertを実行 if (isFirefox()) { const start = cursor.selectionStart; // in this case maybe 0 cursor.setRangeText(newText); cursor.selectionStart = cursor.selectionEnd = start + newText.length; const uiEvent = document.createEvent('UIEvent'); uiEvent.initEvent('input', true, false); cursor.dispatchEvent(uiEvent); } else { document.execCommand('insertText', false, newText); } }, 50); }