replaceText
2021-01-10 19:56:29 もうメンテしていません
oldText
の文字数だけ Delete
を実行した後に、 insertText
を挿入する関数
script.jsexport 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);
}