Hide messages from particular users

The code below should hide messages from John, Mike, and guest users. For some reason, I get the Uncaught TypeError: Cannot read property 'innerText' of null error message. How to fix it?

I don't work with JS often, so I think the error is probably trivial, but its not easy to spot it myself.

var list_of_blocked = ["john", "mike"];
var style_filter = 'blur(4pt)';

document.querySelectorAll('article').forEach((e) => {
  var e_speaker = e.querySelector('a[itemprop=creator]');
  var is_author_guest = e.querySelector('div.sign').innerText.startsWith('guest');
  if (!e_speaker && !is_author_guest) {
    return;
  }

  var speaker = e_speaker.innerText;
  if (!list_of_blocked.includes(speaker)) {
    return;
  }

  e.style.filter = style_filter;
});
<article itemscope="itemscope">
  <p>message text by john</p>
  <div class="sign">
    <a itemprop="creator">john</a>
  </div>
</article>

<article itemscope="itemscope">
  <p>message text by mike</p>
  <div class="sign">
    <a itemprop="creator">mike</a>
  </div>
</article>

<article itemscope="itemscope">
  <p>message text by guest</p>
  <div class="sign">guest</div>
</article>

<article itemscope="itemscope">
  <p>message text by jane</p>
  <div class="sign">
    <a itemprop="creator">jane</a>
  </div>
</article>



Read more here: https://stackoverflow.com/questions/66280106/hide-messages-from-particular-users

Content Attribution

This content was originally published by john c. j. at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: