0

I am listening the user select event in google chrome extension content script like this:

document.addEventListener(TransGlobal.MOUSE_UP, firstMouseUp);

export async function firstMouseUp(e: MouseEvent) {
  if (selection && selection.toString().trim().length > 0) {
    // do some select logic
  }
}

Now I am facing a problem that the firstMouseUp will trigger when select element and unselect element. when double click the webpage event then trigger the select event, it works fine. But I did not want to trigger the select when click the free area of the web, when click the free area of the web page, the selected element will be unselected, also trigger the firstMouseUp, that make me crazy, what should I do to recognize the select or unselect? I found some text the unselect will trigger, for example, this page https://stackoverflow.com/help/badges/2278/custodian?userid=16801334. when I select the Custodian and unselected, both will trigger. other text unselect did not trigger:

enter image description here

Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • Where does `selection` within `firstMouseUp` come from? – Peter Seliger Mar 09 '22 at 15:11
  • Also have a look at/into ... [_"How to detect the deselection of a before selected text?"_](https://stackoverflow.com/questions/70900476/how-to-detect-the-deselection-of-a-before-selected-text). The approach of enabling/allowing a custom `deselect` event-type upon an `select`able element-node might help in finding a solution for the problem described by the OP. – Peter Seliger Mar 09 '22 at 15:16
  • 1
    Can you use `selectionchange` event instead? – wOxxOm Mar 09 '22 at 18:51
  • I tried selectionchange, still have this problem. select and unselect will trigger the event, but I just want selection trigger event, unselect did not trigger or I could know which one was selection and which one was unselection.@wOxxOm – Dolphin Mar 10 '22 at 02:32

0 Answers0