Creating a highlighter for Chrome Extension

Viewed 20

I want to create a highlighter feature for my Chrome extension. I am not sure what is wrong with my code, I am using Javascript. The code is below. Note - I am using the attribute as a substitute for highlighting, it's not permanent.

function getText() {
 if (document.getSelection) {
    return document.getSelection().toString();
 } else {
    if (document.selection) {
        var text2 = document.selection.createRange();
    }
  }
}
function replaceText() {
  let tex = getText();
  if (document.getSelection) {
     alert("hi");
     document.getSelection().innerHTML = document.getSelection().replace(tex, "<u>" + 
     tex + "</u>");
  } else {
    if (document.selection) {
        document.Selection().createRange().innerHTML = tex.replace(tex, "<u>" + tex + " 
        <u>");
        alert("hello");
    }
}
    alert(tex);
}

let btn = document.createElement("button");
btn.innerHTML = "Highlight";
btn.onclick = function () {
replaceText();
};
document.body.appendChild(btn);

[Better view of the Code] 1

0 Answers
Related