Skip to content

Instantly share code, notes, and snippets.

@harshil93
Forked from kristopolous/hn_seach.js
Last active September 2, 2015 07:13
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 harshil93/a67548fcb50111fe6387 to your computer and use it in GitHub Desktop.
Save harshil93/a67548fcb50111fe6387 to your computer and use it in GitHub Desktop.
hn job query search
function query() {
var
total = 0, shown = 0,
// HN is done with very unsemantic classes.
job_list = Array.prototype.slice.call(document.querySelectorAll('.c5a,.c00,.c9c,.cdd,.c73,.c88')),
query_list = Array.prototype.slice.call(arguments);
// This traverses up the dom stack trying to find a match of a specific class
function upTo(node, klass) {
if (node.className === klass) {
return node;
}
if(node === document.body) {
throw new Exception();
}
return upTo(node.parentNode, klass);
}
function style(node, what) {
upTo(node, 'athing').style.display = what;
}
// Turn them all off
job_list.forEach(function(node) {
style(node, 'none');
total ++;
});
query_list.forEach(function(query) {
if (query.forEach) {
var and_query_list = query.map(function(what) {
return what.toLowerCase();
});
job_list.forEach(function(node) {
var
doesMatch = true,
toTest = node.innerHTML.toLowerCase();
and_query_list.forEach(function(query) {
doesMatch &= (toTest.search(query) > -1);
})
if(doesMatch) {
style(node, 'block');
shown ++;
}
});
} else {
query = query.toLowerCase();
job_list.forEach(function(node) {
if(node.innerHTML.toLowerCase().search(query) !== -1) {
style(node, 'block');
shown ++;
}
});
}
});
return {shown: shown, total: total}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment