Created
July 11, 2013 17:09
-
-
Save karlcow/5977291 to your computer and use it in GitHub Desktop.
Globo redirection algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
glb.behavior.redirectToMobile = { | |
'doc': global.document, | |
'loc': global.location, | |
'expiresAfter': 1814400, | |
'matcherMobile': /android.*mobile|blackberry|docomo|fennec|htc|ip(?:hone|od)|lge|mot|netfront|nokia|opera\sm(?:ini|obi)|samsung-gt-s8500|skyfire|up\.browser|vx10000|wap|windows\s(?:ce|mobile\s7|phone\sos\s7)/i, | |
'matcherNotMobile': /android.*(gt-(?:p|n)|shw-|sc-01c|sch-i800|f-01d|ct100)|tablet|xoom/i, | |
'matcherMobileReferrer': /m\.globo\.com/, | |
'matcherCookieKey': /;?\s*(MOBILEPREF=([01]))/, | |
'matcherIndexHtml': /index\.html$/, | |
'matcherWWW': /^(https?:\/\/)?www\./, | |
'init': function() { | |
if (this.isMobile(global.navigator.userAgent)) { | |
if (this.cameFromMobile(global.document.referrer)) { | |
this.saveChoice(false); | |
} else { | |
if (this.hasCookieKey()) { | |
if (this.wantsMobile()) { | |
this.redirect(); | |
} | |
} else { | |
this.saveChoice(true); | |
this.redirect(); | |
} | |
} | |
} | |
}, | |
'isMobile': function(userAgent) { | |
var isMobile = this.matcherMobile.exec(userAgent) === null ? false : true; | |
if (isMobile) { | |
isMobile = this.matcherNotMobile.exec(userAgent) === null ? true : false; | |
} | |
return isMobile; | |
}, | |
'cameFromMobile': function(referrer) { | |
var matcher = this.matcherMobileReferrer; | |
return !!matcher.exec(referrer); | |
}, | |
'saveChoice': function(wantMobile) { | |
var choice = wantMobile ? 1 : 0, | |
expires = new Date(); | |
expires.setTime(expires.getTime() + this.expiresAfter * 1000); | |
this.doc.cookie = 'MOBILEPREF=' + choice + ';expires=' + expires.toUTCString(); | |
}, | |
'hasCookieKey': function() { | |
var matchCookieKey = this.doc.cookie.match(this.matcherCookieKey); | |
if (matchCookieKey) { | |
return matchCookieKey[1] === undefined ? false : true; | |
} else { | |
return false; | |
} | |
}, | |
'wantsMobile': function() { | |
var matchCookieKey = this.doc.cookie.match(this.matcherCookieKey); | |
if (this.hasCookieKey()) { | |
return matchCookieKey[2] === '1'; | |
} else { | |
return true; | |
} | |
}, | |
'mobileChoosed': function() { | |
this.saveChoice(true); | |
this.redirect(); | |
}, | |
'redirect': function() { | |
var url = this.loc.href; | |
if (this.matcherIndexHtml.exec(url)) { | |
url = url.substring(0, url.length - 10); | |
} | |
url = url.replace(this.matcherWWW, '$1'); | |
url = 'http://m.' + url.substring(7); | |
this.loc.href = url; | |
} | |
}; | |
}(this)); | |
(function() { | |
var shouldRedirect = 'True' == 'True'; | |
if (shouldRedirect) { | |
glb.behavior.redirectToMobile.init(); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment