I'm working on a chrome extension that extracts every word clicked. This code works great for any word outside of a link:
$(document).click(function(e) {
var t = '';
var s = window.getSelection();
if (s.isCollapsed) {
s.modify('move', 'forward', 'character');
s.modify('move', 'backward', 'word');
s.modify('extend', 'forward', 'word');
t = s.toString();
s.modify('move', 'forward', 'character');
} else {
t = s.toString();
}
console.log(t);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#">foo bar</a><br>
words not in link
However I also need to get the words clicked inside a link. so for example with this html:
<a href="#">foo bar</a>
I need to get "foo" when the word foo is clicked. Is there a way do to that with jQuery?