Greasy Fork is available in English.

Συζητήσεις » Ανάπτυξη

Delete event listener upon activation

§
Δημοσιεύτηκε: 06/04/2024

Greetings!

I am attempting to remove the event listener once it is called.

document.addEventListener("DOMContentLoaded", function() {
  document.body.addEventListener("mouseover", async function(e) {
    if (e.target && e.target.nodeName == "A") {
      hyperLink = e.target;

      // Attempts to remove event listener
      // /questions/4386300/javascript-dom-how-to-remove-all-event-listeners-of-a-dom-object

      //hyperLink.removeEventListener('mouseover', addEventListenerToHyperlinks);
      //hyperLink.replaceWith(hyperLink.cloneNode(true));
      //let clone = hyperLink.cloneNode(true);
      //hyperLink.replaceWith(clone);

      await xhrHyperLink(hyperLink);
    }
  });
});

Whoever sees this, please consider helping.

§
Δημοσιεύτηκε: 06/04/2024

add }, { once: true }); in the end

§
Δημοσιεύτηκε: 07/04/2024

It does not work:

document.addEventListener("DOMContentLoaded", function() {
  document.body.addEventListener("mouseover", async function(e) {
    if (e.target && e.target.nodeName == "A") {
      hyperLink = e.target;
      await xhrHyperLink(hyperLink);
  }, { once: true });
});

When adding { once: true } to event mouseover the event does not work - at all, and adding { once: true } to event DOMContentLoaded makes no change.

§
Δημοσιεύτηκε: 07/04/2024

It should work though

https://streamable.com/midyv7

§
Δημοσιεύτηκε: 07/04/2024

I have added { once: true } to Proxy Redirect. Please see for yourself, that the functionality to change links, as shown in the video is not working.

In the video, I present the issue caused by the event being executed more than once.

§
Δημοσιεύτηκε: 07/04/2024

Answering to myself: Of course it does not work for links, because the "event listener" is directed at the element "body" document.body.addEventListener.

§
Δημοσιεύτηκε: 07/04/2024

Issue has been solved!

      for (linkElement of document.links) {
        linkElement.addEventListener("mouseover",
          async function(e) {
            if (e.target && e.target.nodeName == "A") {
              hyperLink = e.target;
              await xhrHyperLink(hyperLink);
            }
          },
          {
            once: true
          }
        );
      }

Thank you hacker09 and Konf!

Δημοσίευση απάντησης

Συνδεθείτε για να δημοσιεύσετε μια απάντηση.