Skip to content

Instantly share code, notes, and snippets.

@bjankord
Created August 22, 2012 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjankord/3429218 to your computer and use it in GitHub Desktop.
Save bjankord/3429218 to your computer and use it in GitHub Desktop.
ReSct.it Script
var ReSRC = function() {
"use strict";
function h(e, t, n) {
var r = new Array;
t == null && (t = document);
n == null && (n = "*");
var i = t.getElementsByTagName(n), s = i.length, o = new RegExp("(^|\\s)" + e + "(\\s|$)"), u, a;
for (u = 0, a = 0; u < s; u++) if (o.test(i[u].className)) {
r[a] = i[u];
a++;
}
return r;
}
function p(e, t) {
var n = new Image;
n.src = t;
n.onload = function() {
e.src = t;
};
}
function d() {
if (new Date - o < a) setTimeout(d, a); else {
u = !1;
ReSRC.reSrcImages();
}
}
var e = 10, t = 10, n = window.name || "0,0", r = ".resrc.it", i = "http://resrc.it/img/img.jpg?n=" + Math.random(), s = 21912, o = new Date, u = !1, a = 200, f = parseInt((new Date).getTime() / 1e3), l = n.split(","), c = l[0];
Array.prototype.indexOf || (Array.prototype.indexOf = function(e) {
for (var t = 0; t < this.length; t++) if (this[t] === e) return t;
return -1;
});
String.prototype.startsWith = function(e) {
return this.match("^" + e) == e;
};
Number.prototype.between = function(e, t) {
return e < t ? this >= e && this <= t : this >= t && this <= e;
};
window.onresize = function() {
o = new Date;
if (u == 0) {
u = !0;
setTimeout(d, a);
}
};
return {
getBandwidth: function() {
var e, t, n = new Image;
n.src = i;
e = (new Date).getTime();
n.onload = function() {
var t = (new Date).getTime(), n = parseInt(t / 1e3), r = (t - e) / 1e3, i = s * 8, o = Math.round(i / r), u = parseInt(o / 1024).toFixed(0), a = parseInt(u / 1024).toFixed(0);
window.name = n + "," + u;
};
},
getOptimizationParam: function(e) {
var t = parseInt(e), n;
t == 0 ? n = "m" : t.between(1, 50) ? n = "l" : t.between(51, 100) ? n = "m" : t.between(101, 800) ? n = "h" : n = "f";
return "/ro=" + n;
},
getPixelRatio: function() {
return window.devicePixelRatio ? window.devicePixelRatio : 1;
},
getHostURL: function(e) {
var t = Math.floor(Math.random() * 4 + 1), n = ".*?", i = "(-)", s = "((?:[a-z][a-z]+))", o = new RegExp(n + i + s, [ "i" ]), u = o.exec(e);
if (u != null) {
var a = u[2];
return a == "uk" ? "http://app" + t + "-" + "uk" + r + "/" : "http://app" + t + r + "/";
}
return "http://app" + t + r + "/";
},
reSrcImages: function() {
f >= parseInt(c) + t && ReSRC.getBandwidth();
var n = h("resrc");
for (var r = 0; r < n.length; r++) {
var i = n[r].tagName.toLowerCase();
if (i == "img") {
var s = n[r].src.toLowerCase();
s = s.replace("/http://", "//");
s = s.replace("http://", "//");
s = s.replace("//", "");
var o = 0, u = "/", a = s.split(u).slice(0, 1), d = s.split(u).slice(1);
for (var v = 0; v < d.length; v++) if (d[v].startsWith("s=w")) {
var m = "((?:[a-z][a-z]*[0-9]+[a-z0-9]*))", g = new RegExp(m, [ "i" ]), y = g.exec(d[v]);
if (y != null) {
var b = y[1], w = [ "w", "p" ];
b = b.replace(new RegExp("(" + w.join("|") + ")", "g"), "");
o = parseInt(b);
}
d.splice(v, 1);
v--;
} else if (d[v].startsWith("ro=")) {
d.splice(v, 1);
v--;
}
var E = d.join(u), S = "//", x = E.split(S), T, N;
if (x.length == 1) {
T = "";
N = x[0];
} else {
T = x[0];
N = x[1];
}
N.startsWith("/") && (N = N.substring(1));
var C = n[r].getAttribute("data-resrc") || "", k;
C == "retina" ? k = 2 : C == "no-retina" ? k = 1 : k = ReSRC.getPixelRatio();
var L;
n[r].width == 0 ? o == 0 ? L = "" : L = "/s=w" + o + ",pd" + k : L = "/s=w" + Math.ceil(n[r].width / e) * e + ",pd" + k;
var A = ReSRC.getOptimizationParam(l[1]), O = ReSRC.getHostURL(a) + T + L + A + "/http://" + N;
p(n[r], O);
}
}
}
};
}();
ReSRC.reSrcImages();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment