Прослушиватель событий кликов Javascript срабатывает только один разJquery

Программирование на jquery
Ответить Пред. темаСлед. тема
Гость
 Прослушиватель событий кликов Javascript срабатывает только один раз

Сообщение Гость »


I have a Chrome extension that intercepts and checks tweets before they get posted. To do this, I've add an event listener to the Tweet button. Sine the content is dynamic, I use the solution proposed in this thread:

initialize : function() { let that = this; let jsInitChecktimer = setInterval(checkForJsFinished, 111); function checkForJsFinished () { if (document.querySelector("div[data-testid='tweetButtonInline']")) { clearInterval (jsInitChecktimer); console.log("Button found"); that.addSubmitNewTweetClickHandler(); } } }, addSubmitNewTweetClickHandler : function() { let that = this; let buttonSubmitTweet = document.querySelector("div[data-testid='tweetButtonInline']"); buttonSubmitTweet.addEventListener('click', function(e) { console.log("CLICK"); // Stop default event from happening e.preventDefault(); e.stopImmediatePropagation(); // Do stuff }); }, If the tweet passed the checks alright, it gets submitted by programmatically triggering the event using .trigger('click').

This works fine, but only once. After a tweet has been submitted and posted, the event listener on the Tweet button is gone, and I cannot intercept the next tweet to check it. I've tried calling initialize() after submitted again -- maybe the button gets removed and newly added to the DOM (it actually disappears fire a moment when submitting a tweet) -- but the querySelector finds the button immediately. But even after calling initialize() again, no click even on the Tweet button fires.

What could be the issue here? My problem is that I don't even know where to look for and how to debug this.


Источник: https://stackoverflow.com/questions/590 ... -only-once
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Jquery»