Skip to content

Commit

Permalink
Update insertCaret function.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdulcet committed Jul 22, 2020
1 parent 78a023c commit d9f97f3
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/content_scripts/autocorrect.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,30 @@ function getCaretPosition(target) {
function insertCaret(target, atext) {
const isSuccess = document.execCommand("insertText", false, atext);

if(isSuccess) {
return;
}

// Firefox input and textarea fields: https://bugzilla.mozilla.org/show_bug.cgi?id=1220696
if (!isSuccess && typeof target.setRangeText === "function") {
if (typeof target.setRangeText === "function") {
const start = target.selectionStart;
target.setRangeText(atext);
const end = target.selectionEnd;

if (start !== undefined && end !== undefined) {
target.setRangeText(atext);

target.selectionStart = target.selectionEnd = start + atext.length;
target.selectionStart = target.selectionEnd = start + atext.length;

// Notify any possible listeners of the change
const event = document.createEvent("UIEvent");
event.initEvent("input", true, false);
target.dispatchEvent(event);
// Notify any possible listeners of the change
const event = document.createEvent("UIEvent");
event.initEvent("input", true, false);
target.dispatchEvent(event);

return;
}
}

throw new Error("nothing selected");
}

/**
Expand Down

0 comments on commit d9f97f3

Please sign in to comment.