Last active
December 19, 2015 05:19
-
-
Save bmcminn/5903413 to your computer and use it in GitHub Desktop.
JS: Userscript that adds a persistent REGEX filter field to craigslist search pages.
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
// ==UserScript== | |
// @name CList Filter | |
// @version 0.2 | |
// @namespace giggleboxstudios | |
// @include *.craigslist.org/search/* | |
// @grant none | |
// ==/UserScript== | |
/*jshint strict:true, evil:true, boss:false, laxcomma:true, laxbreak:true, latedef:true, immed:true, newcap:true, noarg:true, nonew:true, plusplus:true, regexp:true, undef:true, unused:true, trailing:true, eqeqeq:true, curly:true, camelcase:true, bitwise:true */ | |
// ============================== DO NOT EDIT BELOW HERE ============================== | |
var widgetName = 'clist-filter' | |
, $cookieName = widgetName | |
, $searchContainer = '#hoodpicker' | |
, $regexField = '#clist-regex' | |
, $searchForm = '#searchform' | |
, $listings = $('.row') | |
, match = { | |
filter: '' | |
, currentPage: /[\w\d:\/\.]+\/([\w]{1,})[\w\d\:\/\.\?\=-_+]+/gi | |
} | |
, temp = '' | |
, tempRegex = '' | |
; | |
(function($) { | |
"use strict"; | |
// Load jQuery (if needed) and load our widget script from the server | |
if (window.top !== window.self) { return; } | |
// Regex field contents to be appended to $searchContainer | |
temp = '<span style="display: inline-block; padding: 0 10px 0 30px;">regex filter:</span>' | |
+ '<input id="clist-regex" name="clist-regex" class="pants dv" value="" placeholder="regex filter">' | |
; | |
// Cache search form | |
$searchForm = $($searchForm); | |
// Cache clist search container | |
$searchContainer = $($searchContainer).parent().append(temp); | |
// Cache regex input field | |
$regexField = $($regexField); | |
tempRegex = $regexField.val(); | |
// Check if our cookie is available | |
if ($.cookie($cookieName) !== '') { | |
tempRegex = $.cookie($cookieName); | |
$regexField.attr('value', tempRegex); | |
match.filter = new RegExp(tempRegex, 'gi'); | |
} | |
// Function that updates regex field | |
function updateRegex() { | |
// Assign the value to our regex cookie | |
if (tempRegex !== $regexField.val()) { | |
$regexField.attr('value', $regexField.val()); | |
$.cookie($cookieName, $regexField.val()); | |
} | |
return false; | |
} | |
// When we leave the regex field | |
$regexField.on('blur', function() { | |
updateRegex(); | |
}); | |
$searchForm.on('submit', function() { | |
updateRegex(); | |
}); | |
// Run the regex | |
$listings.each(function() { | |
var $this = $(this) | |
, name = $this.find('.pl > a').text() | |
, location = $this.find('.pnr > small').text() | |
; | |
if (!name.match(match.filter) && !location.match(match.filter)) { | |
$this.remove(); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO:
Change filter logic to target location text as well./search/sss?
vs/search/aba?
Other features are welcome, so please suggest :)