I develop chrome extension and I need to get a word of any web page when mouse is over this word.
I don’t want to put all words to specific containers and then get it by id.
For example, if I have <p>The weather is very good today</p> I want to make mouse over the word “very” and get it in my js (for example, alert('very')).
I develop chrome extension and I need to get a word of any web
Share
I’d like to show you a proof of concept for this feature. It’s very naive and weak but shows that it’s possible with an appropriate regex. Basically, hover the green-bordered div to see the results.
With this approach, each paragraph is split into spanned chunks with a regex. The more powerful regex, the better result, of course. Then,
onmousemoveis bubbling, being triggered by a span. Reading a content of the span is a piece of cake.This method is very intrusive, by modifying DOM structure, which you probably don’t want to touch. It works in plain JS though. The more advanced (still easy) approach would be as follows:
onmousemoveto find the paragraph under the mouse.clientXandclientY.position: absolute.spanify()thing on a copy.clientXandclientYshifted by the position of the copy (left, top).This hack still works in plain JS. Happy coding! 😉