Skip to content

Instantly share code, notes, and snippets.

@miketaylr
Created March 25, 2015 15:02
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 miketaylr/22b5064d813f3575b679 to your computer and use it in GitHub Desktop.
Save miketaylr/22b5064d813f3575b679 to your computer and use it in GitHub Desktop.
(function(e, t) {
function a() {
return t !== t.top;
}
function f() {
var e = /.*(iPad|iPhone|iPod).*OS ([0-9])_([0-9]).*/g, t = e.exec(i.navigatorUserAgent);
if (t) {
var n = t[1].toLowerCase();
r = {
name: n,
os: "ios",
version: parseInt(t[2]),
subversion: parseInt(t[3]),
type: n === "ipad" ? "l" : "s"
}, r.browserHistory = r.version > 4;
if (r.version >= 5 || r.version === 4 && r.subversion >= 3) return !0;
}
return !1;
}
function l() {
var e = /.*Android ([0-9])\.([0-9]).*; (.*) Build.* (Chrome|.+)\/.*? (Mobile|.+ )?.*Safari/g, t = e.exec(i.navigatorUserAgent), n = i.windowWidth, s = i.windowHeight, o = n > s ? "landscape" : "portrait", u = o === "portrait" && n <= 635 || o === "landscape" && s <= 635;
if (t) {
r = {
name: t[3],
os: "android",
version: parseInt(t[1]),
subversion: parseInt(t[2]),
browser: t[4] == "Chrome" ? "chrome" : "aosp",
type: t[5] ? "s" : u ? "m" : "l"
}, r.browserHistory = r.browser === "chrome";
if (r.version >= 4) return !0;
}
return !1;
}
function c() {
var e = /.*BB10; ([\d\w]*)\).*Version\/(\d*)\.(\d*)\..*Mobile.*/g, t = e.exec(i.navigatorUserAgent);
if (t) {
r = {
name: t[1],
os: "bb10",
version: parseInt(t[2]),
subversion: parseInt(t[3]),
type: "s"
}, r.browserHistory = !0;
if (r.version === 10 && r.subversion >= 1) return !0;
}
return !1;
}
function h() {
var e = /\bSilk\b/g;
return e.test(i.navigatorUserAgent) ? (r = {
name: "kindle",
os: "kindle",
type: "m"
}, r.browserHistory = !1, !0) : !1;
}
function p() {
var n = t.innerWidth, r = e.createElement("div"), i = r.style, o = d(i);
return r.className = "mrf-marfeelBadge", i.position = "fixed", i.display = "block",
i.bottom = "25px", i.right = 0, i.zIndex = 99, i.backgroundImage = "url(" + v() + ")",
i.backgroundSize = "100% 100%", i.webkitBorderTopLeftRadius = "6px", i.webkitBorderBottomLeftRadius = "6px",
setInterval(function() {
var e = t.innerWidth;
n != e && (n = e, d(i));
}, 2e3), r.addEventListener("click", function(e) {
e.stopPropagation(), s.redirectToMarfeel("r=1");
}), r;
}
function d(e) {
var n, i, s = t.innerWidth / 1024, o = Math.abs(t.orientation) === 90 ? "landscape" : "portrait";
r.type === "s" ? (i = o === "portrait" ? 270 : 240, n = i * .56) : r.type === "m" ? (i = o === "portrait" ? 160 : 100,
n = i * .53) : (i = o === "portrait" ? 140 : 100, n = i * .53), e.height = n * s + "px",
e.width = i * s + "px";
}
function v() {
return "//" + i.getMarfeelHost() + "/statics/marfeel/garda/mrf-badge-dflt." + r.type + ".png";
}
function m(e, t) {
return e += e.indexOf("?") > 0 ? "&" : "?", e + t;
}
t.marfeel = t.marfeel || {};
var n = 4, r = {}, i, s, o = 30, u = 1411745690095;
i = t.marfeel.GardaBUtil = {
locationSearch: t.location.search,
locationPathname: t.location.pathname === "/" ? "/index" : t.location.pathname,
navigatorUserAgent: t.navigator.userAgent,
documentDomain: e.domain,
windowWidth: t.screen.width,
windowHeight: t.screen.height,
getMarfeelHost: function() {
return t.mrf && t.mrf.host ? t.mrf.host : t.mrfHost || "b.marfeel.com";
},
getSupportedDeviceTypes: function() {
return t.mrf && t.mrf.dt ? t.mrf.dt : t.mrfDt || "lms";
},
showBadge: function() {
return t.mrf && t.mrf.showBadge === !1 ? !1 : !0;
},
getBlacklistedUrls: function() {
return t.mrf && t.mrf.blacklistedUrls ? t.mrf.blacklistedUrls : [];
},
setCookieValue: function(t, n) {
var r = new Date(), i = t + "=" + n;
r.setMinutes(r.getMinutes() + o), i += ";expires=" + r.toGMTString(), i += ";domain=" + this.documentDomain,
i += ";path=/", e.cookie = i;
},
getCookieValue: function(t) {
var n, r, i = e.cookie;
t = t.trim(), i = i.split(";");
for (n = 0; n < i.length; n += 1) {
r = i[n], r = r.split("=");
if (r[0].trim() === t) return r[1];
}
return null;
},
getMarfeelUrl: function(e) {
var t = "//" + i.getMarfeelHost() + "/" + i.documentDomain + i.locationPathname + i.locationSearch;
return e && (t = m(t, e)), r.type && (t = m(t, "marfeeldt=" + r.type)), t;
},
setLocation: function(e) {
t.location.href = e;
},
requestGeo: function() {
var e = new XMLHttpRequest();
e.timeout = 1e4, e.open("GET", "//geo.marfeel.com/wally"), e.onreadystatechange = function() {
if (e.readyState === 4 && e.status === 200) try {
t.mrfGeo = JSON.parse(e.responseText);
} catch (n) {
console.log("GeoMarfeel: Can't parse response");
}
}, e.send();
}
}, s = t.marfeel.GardaB = {
captureOriginalDocument: !1,
getDevice: function() {
return r;
},
setDevice: function(e) {
r = e;
},
isEnabled: function() {
return i.locationSearch.indexOf("marfeelgarda=off") === -1;
},
isDeviceSupported: function() {
var e = f() || h() || l() || c(), t = e && i.getSupportedDeviceTypes().indexOf(r.type) >= 0;
return !this.isEnabled() || e && t;
},
urlIsBlacklisted: function() {
var e = i.getBlacklistedUrls(), t = i.documentDomain + i.locationPathname;
for (var n = 0, r = e.length; n < r; n++) if (t.indexOf(e[n]) >= 0) return !0;
return !1;
},
isExtractable: function() {
var t = e.querySelector('meta[name="mrf-extractable"]');
return t && t.getAttribute("content") === "false" ? !1 : !0;
},
comesFromMarfeel: function() {
var e = i.locationSearch.indexOf("fromt=yes") > -1 || i.getCookieValue("FromMarfeel") === "YES" || i.getCookieValue("FromMarfeelOnError") === "YES";
return i.setCookieValue("FromMarfeel", "NO"), e;
},
userWantsMarfeel: function() {
var e = parseInt(i.getCookieValue("MarfeelCreation")) > u;
return e ? i.getCookieValue("MarfeelGarda") !== "NO" : !0;
},
fallbackToClassicVersion: function(t) {
i.setLocation(m(t || e.location.href, "fromt=yes"));
},
redirectToMarfeel: function(t) {
var r = i.getMarfeelUrl(t), o = new XMLHttpRequest(), u;
i.documentDomain === "dailycaller.com" && i.requestGeo(), o.open("GET", r), o.timeout = 1e4,
o.ontimeout = function() {
s.fallbackToClassicVersion();
}, o.onreadystatechange = function() {
o.readyState === n && (o.status > 0 && o.status < 400 ? (u = e.open("text/html", "replace"),
u.write(o.responseText), u.close()) : s.fallbackToClassicVersion());
}, o.send();
},
showMarfeelBadge: function() {
var n = function() {
t.removeEventListener("DOMContentLoaded", n), t === t.top && e.getElementsByTagName("body")[0].appendChild(p());
};
e.readyState === "interactive" || e.readyState === "complete" ? t === t.top && e.getElementsByTagName("body")[0].appendChild(p()) : t.addEventListener("DOMContentLoaded", n);
},
perform: function() {
!a() && this.isDeviceSupported() && !this.urlIsBlacklisted() && this.isExtractable() && (!this.comesFromMarfeel() && this.userWantsMarfeel() ? ((i.documentDomain.indexOf("www.capital.fr") === -1 || i.documentDomain.indexOf("www.nationalgeographic.fr") === -1 || i.documentDomain.indexOf("www.geo.fr") === -1) && e.write('<plaintext style="display:none">'),
!this.captureOriginalDocument || e.readyState === "interactive" || e.readyState === "complete" ? this.redirectToMarfeel() : this.captureOriginalDocument && t.addEventListener("DOMContentLoaded", function() {
s.redirectToMarfeel();
})) : this.isEnabled() && i.showBadge() && this.showMarfeelBadge());
}
}, s.perform();
})(document, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment