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.