Skip to content

Instantly share code, notes, and snippets.

@victor9000
Last active July 22, 2017 18:58
Show Gist options
  • Save victor9000/853fecb3ba13e6524797890edd50a528 to your computer and use it in GitHub Desktop.
Save victor9000/853fecb3ba13e6524797890edd50a528 to your computer and use it in GitHub Desktop.
Bookmarklet to filter comments on Hacker News based on keywords
javascript: (function() {
/* inject script */
var v_jq = null;
var v_element = document.createElement('script');
v_element.src = '//code.jquery.com/jquery-latest.min.js';
/* identify comments that do not meet the filter condition */
var v_filter = function(v_comments, v_filters, v_is_whitelist) {
v_discarded = v_comments.filter(function(v_comment) {
var v_text = v_jq(v_comment).text().trim().toLowerCase();
var v_contains_token = function(v_token) {
return v_text.indexOf(v_token) > -1;
};
/* blacklist: if some elements match then discard the comment */
var v_discard = v_filters.some(v_contains_token);
/* whitelist: if some elements do not match then discard the comment */
if (v_is_whitelist === true)
v_discard = !v_discard;
return v_discard;
});
return v_discarded;
};
/* hide each comment in the discard pile */
var v_hide = function(v_discarded) {
v_discarded.forEach(function(v_comment) {
v_comment.closest('tr.athing.comtr').style.display="none";
});
};
/* wait for jquery to load to begin processing */
v_element.onload = function() {
/* play nice with other instances of jquery */
v_jq = jQuery.noConflict(true);
/* start filtering comments */
var v_comments = v_jq('.comment').toArray();
var v_discarded = v_filter(v_comments, ['seattle'], true);
v_hide(v_discarded);
};
document.head.appendChild(v_element);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment