Skip to content

Instantly share code, notes, and snippets.

@frosas
Last active November 2, 2023 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frosas/4cadd8392a3c4af82ef640cbedea3027 to your computer and use it in GitHub Desktop.
Save frosas/4cadd8392a3c4af82ef640cbedea3027 to your computer and use it in GitHub Desktop.
Hacker News Comments Filter

Usage

Copy/paste index.js into your browser console. Then,

// Non-Angular Javascript contract positions in London or remote
hn.filter(
  hn.or(/(javascript|typescript)/i, /ES\d/, 'JS'),
  hn.not(/angular/i),
  /contract/i,
  hn.or(hn.and('ONSITE', /london/i), 'REMOTE')
);

(note hn.and() is applied implicitly to hn.filter() arguments)

Custom functions are welcome too:

hn.filter(commentEl => commentEl.textContent.length < 1000); // TL;DR

Pass nothing to remove any filtering (or simply reload the page):

hn.filter();

Reference

Based on https://gist.github.com/kristopolous/19260ae54967c2219da8

hn = (() => {
const filter = (...matcherLikes) => {
const stats = { shown: 0, total: 0 };
document.querySelectorAll('.comtr').forEach(commentEl => {
const show = buildMatcher(matcherLikes)(commentEl);
commentEl.style.display = show ? 'block' : 'none';
stats.total++;
if (show) stats.shown++;
});
return stats;
};
const buildMatcher = matcherLike => {
if (Array.isArray(matcherLike)) return buildAndMatcher(...matcherLike);
if (typeof matcherLike == 'string') return buildStringMatcher(matcherLike);
if (matcherLike instanceof RegExp) return buildRegexpMatcher(matcherLike);
return matcherLike;
};
const buildStringMatcher = string => el => el.textContent.includes(string);
const buildRegexpMatcher = regexp => el => regexp.test(el.textContent);
const buildAndMatcher = (...matcherLikes) => el => matcherLikes.every(matcherLike => buildMatcher(matcherLike)(el));
const buildOrMatcher = (...matcherLikes) => el => matcherLikes.some(matcherLike => buildMatcher(matcherLike)(el));
const buildNotMatcher = matcherLikes => el => !buildMatcher(matcherLikes)(el);
return {
filter,
and: buildAndMatcher,
or: buildOrMatcher,
not: buildNotMatcher
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment