Skip to content

Instantly share code, notes, and snippets.

@bmcminn
Last active December 19, 2015 05:19
Show Gist options
  • Save bmcminn/5903413 to your computer and use it in GitHub Desktop.
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.
// ==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);
@bmcminn
Copy link
Author

bmcminn commented Jul 1, 2013

TODO:

  • Add jQuery test and dependency loading provided jQuery and jQuery.cookie() are not available for whatever reason.
  • Change filter logic to target location text as well.
  • Add check to determine which search context we're on and store regex cookie for that search context.
    • /search/sss? vs /search/aba?

Other features are welcome, so please suggest :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment