Skip to content

Instantly share code, notes, and snippets.

@miketaylr
Created June 17, 2016 17:34
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/4443d8cc36cb0a1e90a23617c785c9d8 to your computer and use it in GitHub Desktop.
Save miketaylr/4443d8cc36cb0a1e90a23617c785c9d8 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2015 by Marfeel Solutions (http://www.marfeel.com)
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Marfeel Solutions S.L and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Marfeel Solutions S.L and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Marfeel Solutions SL.
*/
(function(e, t, n) {
function a() {
return "https://" + s.getMarfeelHost() + "/" + s.documentDomain + s.locationPathname + s.locationSearch
}
function f() {
l() && o.userWantsMarfeel() && o.fallbackToClassicVersion(864e5)
}
function l() {
return c() || h()
}
function c() {
return /windows phone/i.test(s.navigatorUserAgent)
}
function h() {
return /(ipad|iphone|ipod).*?OS 4_3/i.test(s.navigatorUserAgent)
}
function p(n) {
var r = t.innerWidth,
i = e.createElement("div"),
s = i.style;
return d(s, n), i.className = "mrf-marfeelBadge", s.position = "fixed", s.display = "block", s.bottom = "25px", s.right = 0, s.zIndex = 99, s.backgroundImage = "url(" + v(n) + ")", s.backgroundSize = "100% 100%", s.webkitBorderTopLeftRadius = "6px", s.webkitBorderBottomLeftRadius = "6px", setInterval(function() {
var e = t.innerWidth;
r != e && (r = e, d(s))
}, 2e3), i.addEventListener("click", function(e) {
e.stopPropagation(), o.redirectToMarfeel()
}), i
}
function d(e, n) {
var r, i, s = t.innerWidth / 1024,
o = Math.abs(t.orientation) === 90 ? "landscape" : "portrait";
n === "s" ? (i = o === "portrait" ? 270 : 240, r = i * .56) : n === "m" ? (i = o === "portrait" ? 160 : 100, r = i * .53) : (i = o === "portrait" ? 140 : 100, r = i * .53), e.height = r * s + "px", e.width = i * s + "px"
}
function v(e) {
return "https://" + s.getMarfeelHost() + "/statics/marfeel/garda/mrf-badge-dflt." + e + ".png"
}
function m(e, t) {
return e += e.indexOf("?") > 0 ? "&" : "?", e + t
}
function g(e) {
return e.status === 0 && !e.getAllResponseHeaders()
}
t.marfeel = t.marfeel || {}, t.mrf = t.mrf || {};
var r = 4,
i = 307,
s, o, u = null;
s = t.marfeel.GardaCUtil = {
locationHref: n.href,
locationSearch: n.search,
locationPathname: n.pathname,
navigatorUserAgent: t.navigator.userAgent,
documentDomain: e.domain,
documentCookie: e.cookie,
windowWidth: t.screen.width,
windowInnerWidth: t.innerWidth,
windowHeight: t.screen.height,
geoEnabledDomains: ["www.sport.es", "especiales.elperiodico.com", "www.elperiodico.cat", "www.elperiodico.com", "www.popsugar.com"],
getMarfeelHost: function() {
return t.mrf.host
},
getMarfeelName: function() {
return t.mrf.name || null
},
showBadge: function() {
return l() || t.mrf.showBadge === !1 ? !1 : !0
},
getBlacklistedUrls: function() {
return t.mrf.blacklist || []
},
setCookieValue: function(t, n, r) {
var i = t + "=" + n;
i += ";expires=" + (new Date(Date.now() + r)).toGMTString(), i += ";domain=" + this.documentDomain, i += ";path=/", e.cookie = i
},
getMarfeelUrl: function(e) {
var t = a(),
n = this.getMarfeelName();
return t = m(t, "marfeeldt=" + e), !n || (t = m(t, "marfeeln=" + n)), t
},
reloadLocation: function() {
n.reload()
},
isGeoDomain: function() {
return s.geoEnabledDomains.indexOf(s.documentDomain) >= 0
},
requestGeo: function() {
var e = /marfeelGeo=(.*?);/i.exec(s.documentCookie);
if (e) t.mrfGeo = JSON.parse(e[1]);
else {
var n = new XMLHttpRequest;
n.timeout = 1e4, n.open("GET", "http://geo.marfeel.com/wally"), n.onreadystatechange = function() {
if (n.readyState === 4 && n.status === 200) try {
t.mrfGeo = JSON.parse(n.responseText), s.setCookieValue("marfeelGeo", n.responseText, 1296e5)
} catch (e) {
console.log("GeoMarfeel: Can't parse response")
}
}, n.send()
}
},
trackError: function(e, t) {
var n = new XMLHttpRequest,
i = s.generateUUID();
n.timeout = 1e4, n.open("POST", "http://www.google-analytics.com/collect"), n.setRequestHeader("Content-type", "application/x-www-form-urlencoded"), n.onreadystatechange = function() {
n.readyState === r && t()
}, n.send("v=1&tid=UA-27070218-1&cid=" + i + "&t=event&ec=Error&ea=GardaC_" + e + "&el=" + s.locationHref + "&ev=0")
},
generateUUID: function() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(e) {
var t = Math.random() * 16 | 0,
n = e == "x" ? t : t & 3 | 8;
return n.toString(16)
})
}
}, o = t.marfeel.GardaC = {
getDeviceType: function() {
if (!u) {
var e = s.navigatorUserAgent,
t = s.windowWidth,
n = s.windowHeight,
r = t > n ? "landscape" : "portrait",
i = r === "portrait" && t <= 635 || r === "landscape" && n <= 635;
/ipad/i.test(e) ? u = "l" : /ip(hone|od)|android.*mobile|bb10/i.test(e) ? u = "s" : /\bSilk\b/i.test(e) ? u = "m" : /android (?!(1|2|3)\.)[0-9](?!.*mobile)/i.test(e) ? u = i ? "m" : "l" : this.isEnabled() || (u = s.windowInnerWidth <= 480 ? "s" : "l")
}
return u
},
setDeviceType: function(e) {
u = e
},
isEnabled: function() {
return !/marfeelgarda=off/i.test(s.locationSearch)
},
userWantsMarfeel: function() {
return !/marfeelgarda=no|fromt=yes/i.test(s.locationSearch + ";" + s.documentCookie)
},
urlIsBlacklisted: function() {
var e = s.getBlacklistedUrls(),
t = s.documentDomain + s.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
},
fallbackToClassicVersion: function(e) {
s.setCookieValue("fromt", "yes", e), s.reloadLocation()
},
reloadWithFallbackToClassicVersion: function(t) {
/mrfRetry=yes/.test(e.cookie) ? s.trackError(0, function() {
o.fallbackToClassicVersion(t)
}) : (s.setCookieValue("mrfRetry", "yes", 3e4), s.reloadLocation())
},
redirectToMarfeel: function() {
var t = s.getMarfeelUrl(this.getDeviceType()),
n = new XMLHttpRequest,
u;
s.isGeoDomain() && s.requestGeo(), n.open("GET", t), n.timeout = 4e4, n.onreadystatechange = function() {
n.readyState === r && (n.status === i ? o.fallbackToClassicVersion(3e4) : n.status > 0 && n.status < 400 ? (e.body.scrollTop = 0, u = e.open("text/html", "replace"), u.write(n.responseText), u.close()) : g(n) ? o.reloadWithFallbackToClassicVersion(18e5) : n.status === 404 ? o.fallbackToClassicVersion(18e5) : s.trackError(n.status, function() {
o.fallbackToClassicVersion(18e5)
}))
}, n.send()
},
showMarfeelBadge: function() {
var n = function() {
t.removeEventListener("DOMContentLoaded", n), e.getElementsByTagName("body")[0].appendChild(p(this.getDeviceType()))
}.bind(this);
e.readyState === "interactive" || e.readyState === "complete" ? n.call(this) : t.addEventListener("DOMContentLoaded", n)
},
perform: function() {
f(), this.getDeviceType() && (!this.urlIsBlacklisted() && this.isExtractable() ? this.userWantsMarfeel() ? this.redirectToMarfeel() : this.isEnabled() && s.showBadge() && this.showMarfeelBadge() : o.userWantsMarfeel() && this.fallbackToClassicVersion(3e4))
}
}, o.perform()
})(document, window, window.location);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment