1

I am building a browser extension which I eventually will turn into a translator but for its current purpose I just want my extension to console log the word that is clicked on the page. I am using vanilla JS for this part:

window.addEventListener('mouseup', checkWord);

function checkWord() {
    console.log('the word is')
    let word = window.getSelection().toString();
    console.log(word);
}

If I remove the .toString() method the program will return the entire object to the console. However with the .toString() method the console log gives me an empty return. Should I be using a different method to extract the actual word clicked on?

tdammon
  • 599
  • 1
  • 7
  • 26
  • So what is the question? `window.getSelection()` indeed returns an object – falinsky Nov 06 '18 at 14:33
  • Sorry, @falinsky I forgot to mention that with the .toString I am getting a blank console log. – tdammon Nov 06 '18 at 14:34
  • 1
    Simply clicking on something won't select it, so maybe that's why you're getting an empty string. Try actually selecting something and then call toString() – TLP Nov 06 '18 at 14:43

0 Answers0