Skip to content

Instantly share code, notes, and snippets.

@frosas frosas/README.md
Last active Nov 4, 2017

Embed
What would you like to do?
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 = (...matches) => {
const stats = { shown: 0, total: 0 };
document.querySelectorAll('.comtr').forEach(commentEl => {
const show = normalize(matches)(commentEl);
commentEl.style.display = show ? 'block' : 'none';
stats.total++;
if (show) stats.shown++;
});
return stats;
};
const normalize = match => {
if (Array.isArray(match)) return and(...match);
if (typeof match == 'string') return string(match);
if (match instanceof RegExp) return regexp(match);
return match;
};
const regexp = pattern => el => pattern.test(el.textContent);
const string = searchString => el => el.textContent.includes(searchString);
const and = (...matches) => string => matches.every(match => normalize(match)(string));
const or = (...matches) => string => matches.some(match => normalize(match)(string));
const not = match => string => !normalize(match)(string);
return { filter, and, or, not };
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.