Skip to content

Instantly share code, notes, and snippets.

@cllu
Forked from meiamsome/hn_search.js
Last active December 9, 2015 08:43
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 cllu/8141fa3e4d9eb286fd70 to your computer and use it in GitHub Desktop.
Save cllu/8141fa3e4d9eb286fd70 to your computer and use it in GitHub Desktop.
HackerNews Who is Hiring TamperMonkey Script
// ==UserScript==
// @name HackerNews WhosHiring
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://news.ycombinator.com/item?id=*
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
// Credit:
// - originally by kristopolous: https://gist.github.com/kristopolous/19260ae54967c2219da8
// - fork by meiamsome: https://gist.github.com/meiamsome/94c1aa5f0b431a21b5aa
// takes arguments and produces an array of functions that accept context
function handle_args() {
var args = Array.prototype.slice.call(arguments);
return args.map(function(arg) {
if(arg instanceof Function) return arg; // Presumably already a contex-accepting function.
if(arg instanceof Array) return and.apply(this, arg); // make arrays behave as and.
// Presuming a string, build a function to check.
var my_regex = new RegExp(arg.toString(), 'i');
return function(context) {
return context.search(my_regex) > -1;
};
});
}
// Perform a logical and on any number of arguments.
function and() {
var args = handle_args.apply(this, arguments);
return function(context) {
return args.every(function(a) {return a(context);});
}
}
// Perform a logical not. This function is also the NAND function, so accepts multiple arguments.
function not() {
var result = and.apply(this, arguments);
return function(context) {
return !result(context);
}
}
var nand = not;
// Performs an or using De Morgan's laws
function or() {
var args = handle_args.apply(this, arguments);
return nand.apply(this, args.map(function(a) {return not(a)}));
}
// Performs an XOR. This only works for two inputs.
function xor() {
if(arguments.length != 2) throw "XOR takes two arguments only.";
return and(or.apply(this, arguments), not(and.apply(this, arguments)));
}
function query() {
var
total = 0, shown = 0,
// HN is done with very unsemantic classes.
job_list = Array.prototype.slice.call(document.querySelectorAll('.c5a,.cae,.c00,.c9c,.cdd,.c73,.c88')),
query = or.apply(this, arguments);
// This traverses up the dom stack trying to find a match of a specific class
function up_to(node, klass) {
if (node.className === 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;
}
// Check each job for a match, and show it if it matches.
job_list.forEach(function(node) {
if(query(node.innerHTML)) {
display(node, 'block');
shown ++;
} else display(node, 'none');
total ++;
});
return {shown: shown, total: total}
}
if (document.title.startsWith('Ask HN: Who is hiring?')) {
console.log("WhosHiring active");
console.log("Usage: query('visa')");
console.log(" query(and('remote', or('python', 'ruby')))");
window.query = query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment