Reputation:
I'm using the Chrome extension Web Override for a few things and it works great. Right now I'm trying to hide comments from certain users on Stack Overflow but it doesn't seem to be working. I think it's the way the comments are loaded.
Here is the javascript:
$(function() {
$('.comment-user:contains("DevilsAdvocate")').closest('.comment').hide();
});
If I simply paste $('.comment-user:contains("DevilsAdvocate")').closest('.comment').hide();
into the console, it will hide my comments, but it does not do it when I load a page normally.
Upvotes: 2
Views: 125
Reputation: 26878
One possible solution is to use a MutationObserver()
.
This will allow you to listen for any changes on the comments section and react them accordingly. A rough implementation can be seen below. You create the observer and provide it the hide functionality so that it will hide all of the pertinent comments every time there is a mutation.
Then you tell your observer to watch the comments section of posts. I'm sure that you can write a more jquery way of doing this. Right now it only cares about the first .comments
section it finds. This will listen for any child changes, attribute changes, and subtree changes as well.
var observer = new MutationObserver(function (mutations) {
$('.comment-user:contains("DevilsAdvocate")').closest('.comment').hide();
});
observer.observe(document.querySelectorAll(".comments")[0], {
attributes: true,
childList: true,
characterData: true,
subtree: true
});
I have no idea of the performance impacts that this has and jQuery may have its own way of watching DOM changes. A much better way to do this would be to do aspect oriented programming with the comments JavaScript code so that you can call your hider immediately after a new comment is posted or even filter the comment there.
Alternatively, and as far as I can tell unsupportedly, listen for events that the Stack Exchange API might emit letting you know when new comments are loaded.
The issue that comes with this is that I don't believe the Stack Exchange client side JavaScript is part of any public API so any changes to it could break your code.
Upvotes: 1