Skip to content

Instantly share code, notes, and snippets.

@YenTheFirst
Created August 24, 2011 20:52
Show Gist options
  • Save YenTheFirst/1169191 to your computer and use it in GitHub Desktop.
Save YenTheFirst/1169191 to your computer and use it in GitHub Desktop.
mustachify the interwebs!
//mustachify the interwebs!
//
// tested only on google chrome, so far
//
// ==UserScript==
// @name mustache image
// @include http://*
// ==/UserScript==
//we need jquery, 'cus otherwise searching for all the stupid background-image using inline styles is a pain.
//the followind borrowed from http://erikvold.com/blog/index.cfm/2010/6/14/using-jquery-with-a-user-script
// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script.addEventListener('load', function() {
var script2 = document.createElement("script");
script2.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script2);
}, false);
document.body.appendChild(script);
}
//end of borrow
//'main' function
addJQuery(function(){
//console.log("script started");
var mustache_$ = $.noConflict(true); //revert "$" back to whatever the page wants
window.check_url = function(url){
//console.log("checking url "+url);
return (url.indexOf("http://mustachify.me")==-1) && (url.indexOf("data:") != 0)
}
window.mustache_url = function(url){
return "http://mustachify.me/?src="+escape(url)
}
window.recheck_mustache_images = function(){
//console.log("faoehdoaenth loop running");
mustache_$("img").each(function(i,img){
//console.log("fakj checking image "+img.src);
if(check_url(img.src)){
img.src = mustache_url(img.src);
}
});
mustache_$("[style*=background-image]").each(function(i,elem){
//console.log("fake checking bgimage "+elem.style["background-image"]);
var url = elem.style["background-image"];
var m = url.match("url\\((.*)\\)");
if (m){
if(check_url(m[1])){
elem.style["background-image"] = "url("+mustache_url(m[1])+")"
}
}
});
}
window.recheck_mustache_images();
//this part gets the ajax-loaded images
window.setInterval("recheck_mustache_images();",30*1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment