Last active
February 4, 2016 06:36
-
-
Save archagon/10233251 to your computer and use it in GitHub Desktop.
The start of a generic DOM filtering bookmarklet, along with sample code. Useful if a website's filters aren't specific enough. SEL is the selector for the main list item and COND is a filtering function that you can customize.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PRINT = false; | |
var LOG = 1; | |
var SEL = ".pin"; | |
var COND = function($node) { | |
var ret = true; | |
ret = checkNum($node, ".pinPrice", ">=", 10); | |
if (!ret) { return false; } | |
ret = checkNum($node, ".pinPrice", "<=", 15); | |
if (!ret) { return false; } | |
return true; | |
}; | |
var firstNumberRegex = /^.*?(\d+(\.\d+)?)/; | |
var lastNumberRegex = /^.*?(\d+(\.\d+)?)[^0-9]*/; | |
var checkNum = function($object, selector, symbol, number) { | |
return check($object, selector, lastNumberRegex, 1, | |
function(val) { | |
if (symbol == "<") { | |
return parseFloat(val) < number; | |
} | |
else if (symbol == "<=") { | |
return parseFloat(val) <= number; | |
} | |
else if (symbol == ">") { | |
return parseFloat(val) > number; | |
} | |
else if (symbol == ">=") { | |
return parseFloat(val) >= number; | |
} | |
else if (symbol == "==") { | |
return parseFloat(val) == number; | |
} | |
else { | |
return false; | |
} | |
}); | |
}; | |
var check = function($object, selector, regex, group, comparator) { | |
var text = $object.find(selector).text().trim(); | |
if (text) { | |
var reg = regex.exec(text); | |
if (reg) { | |
if (group) { | |
reg = reg[group]; | |
} | |
if (LOG >= 2) { | |
console.log(text + " -> " + reg); | |
} | |
var result = comparator(reg); | |
if (!result) { | |
if (LOG >= 1) { | |
console.log("COND FAIL FOR " + text); | |
} | |
return false; | |
} | |
else { | |
return true; | |
} | |
} | |
else { | |
if (LOG >= 1) { | |
console.log("NO REG FOR " + text); | |
} | |
return false; | |
} | |
} | |
else { | |
if (LOG >= 1) { | |
console.log("NO " + selector); | |
} | |
return false; | |
} | |
}; | |
$(SEL).each(function(index) { | |
if(!COND($(this))) { | |
if (!PRINT) { | |
$(this).remove(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote this pretty much on autopilot, so I know parts of the code are really weird. (Don't judge me!) I'll fix later if need be.