Skip to content

Instantly share code, notes, and snippets.

@robertfairley
Forked from kristopolous/hn_seach.js
Last active May 5, 2018 02:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertfairley/e068e02f8cd7c6558bfee3d8e3bb290d to your computer and use it in GitHub Desktop.
Save robertfairley/e068e02f8cd7c6558bfee3d8e3bb290d to your computer and use it in GitHub Desktop.
hn job query search

HN Job Query

for "Who's Hiring"

Conveniently query HackerNews 'Who's Hiring' posts for relevant positions using a console function in Chrome.


Usage

Add these files to a folder and load the folder as an unpacked extension in Chrome. If you don't know how to load an unpacked extension, follow these instructions:

https://developer.chrome.com/extensions/getstarted

To use the function, just open your console and form your query using the hnJobQuery function.

Example:

hnJobQuery(['remote', 'toronto'],['remote', 'python']);
▶︎ {shown: 18, total: 499}

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

Relevant: https://news.ycombinator.com/item?id=10313519

const scriptElement = document.createElement('script');
scriptElement.innerHTML = `
function hnJobQuery() {
var
// HN is done with very unsemantic classes.
job_list = Array.prototype.slice.call(document.querySelectorAll('.c5a,.cae,.c00,.c9c,.cdd,.c73,.c88')),
query_list = Array.prototype.slice.call(arguments),
shown = 0, total = job_list.length;
// Traverses up the dom stack trying to find a match of a specific class
function up_to(node, klass) {
if (node.classList.contains(klass)) {
return node;
}
if(node === document.body) {
throw new Exception();
}
return up_to(node.parentNode, klass);
}
function display(node, what) {
up_to(node, 'athing').style.display = what;
}
// If we have a RegEx, return it
// Otherwise make it a case insensitive regex.
function destring(what) {
return what.test ? what : new RegExp(what.toString(), 'i');
}
// Hide all the postings
job_list.forEach(function(node) {
display(node, 'none');
});
query_list.forEach(function(query) {
if (query.forEach) {
var and_query_list = query.map(destring);
job_list.forEach(function(node) {
var
doesMatch = true,
toTest = node.innerHTML;
and_query_list.forEach(function(query) {
doesMatch &= toTest.search(query) > -1;
})
if(doesMatch) {
display(node, 'block');
shown ++;
}
});
} else {
query = destring(query);
job_list.forEach(function(node) {
if(node.innerHTML.search(query) > -1) {
display(node, 'block');
shown ++;
}
});
}
});
return {shown: shown, total: total}
}
`;
scriptElement.onload = _ => {
this.remove();
};
(document.body).appendChild(scriptElement);
{
"manifest_version": 2,
"name": "HackerNews Job Search Query",
"version": "0.0.0",
"description": "Conveniently query HackerNews 'Who's Hiring' posts for relevant positions.",
"permissions": [
"activeTab"
],
"content_scripts": [{
"matches": [
"http://news.ycombinator.com/*",
"https://news.ycombinator.com/*"
],
"js": [
"injector.js"
]
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment