Skip to content

Instantly share code, notes, and snippets.

@jonjaques
Last active December 18, 2015 15:09
Show Gist options
  • Save jonjaques/5802178 to your computer and use it in GitHub Desktop.
Save jonjaques/5802178 to your computer and use it in GitHub Desktop.
Replace Seer
(function($) {
var Memcache = function(hash) {
var self = this,
this.lookupCache = [],
this.valueCache = [];
$.map(hash, function(val, key) {
self.lookupCache.push(key);
self.valueCache.push(val);
});
};
$.extend(Memcache.prototype, {
lookup: function(key) {
var result = this.lookupCache.indexOf(key);
return (result !== -1) ? this.valueCache[result] : false;
},
identity: function(val) {
return val;
}
});
$.fn.replaceSeer = function(hashToProcess, options) {
var store = new Memcache(hashToProcess)
, defaults = {
liveUpdate: true,
valueProcessor: Memcache.prototype.identity,
keyProcessor: Memcache.prototype.identity,
}
, o = $.extend(defaults, options || (options = {}))
, operator = !!$.fn.livequery && o.liveUpdate ? 'livequery' : 'each';
return $(this)[operator](function() {
var el = $(this)
, src = o.valueProcessor(el.attr('src'))
, replacement = store.lookup(o.keyProcessor(src));
if (replacement) {
el.attr('src', replacement);
}
});
};
})(jQuery);
var options = {
// keyProcessor: function(key) {
// return "http://someglobalpath.com/badImages/" + key;
// },
valueProcessor: function(val) {
return "http://someglobalpath.com/images/" + val;
}
};
// left side = image your want to replace,
// right side = image to replace it with
var replacementHash = {
"imageToReplaceOne.jpg": "imageReplacementOne.png",
"imageToReplaceTwo.jpg": "imageReplacementTwo.png",
"imageToReplaceThree.jpg": "imageReplacementThree.png",
"imageToReplaceFour.jpg": "imageReplacementFour.png"
};
$('img').replaceSeer(replacementHash, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment