forked from codejoust/session.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.min.js
8 lines (8 loc) · 7.54 KB
/
session.min.js
1
2
3
4
5
6
7
8
/**
* session.js 0.0.2
* (c) 2012 Iain, CodeJoust
* session is freely distributable under the MIT license.
* Portions of session.js are inspired or borrowed from Underscore.js, and quirksmode.org demo javascript.
* This version uses google's jsapi library for location services.
* For details, see: https://github.com/codejoust/session.js
*/(function(win,doc){var opts={enable_location:!0,session_days:32};"session_opts"in window&&(opts=session_opts);var BrowserDetect={detect_browser:function(){return{browser:this.searchString(this.dataBrowser),version:this.searchVersion(navigator.userAgent)||this.searchVersion(navigator.appVersion),OS:this.searchString(this.dataOS)}},searchString:function(a){for(var b=0;b<a.length;b++){var c=a[b].string,d=a[b].prop;this.versionSearchString=a[b].versionSearch||a[b].identity;if(c){if(c.indexOf(a[b].subString)!=-1)return a[b].identity}else if(d)return a[b].identity}},searchVersion:function(a){var b=a.indexOf(this.versionSearchString);if(b!=-1)return parseFloat(a.substring(b+this.versionSearchString.length+1))},dataBrowser:[{string:navigator.userAgent,subString:"Chrome",identity:"Chrome"},{string:navigator.userAgent,subString:"OmniWeb",versionSearch:"OmniWeb/",identity:"OmniWeb"},{string:navigator.vendor,subString:"Apple",identity:"Safari",versionSearch:"Version"},{prop:window.opera,identity:"Opera",versionSearch:"Version"},{string:navigator.vendor,subString:"iCab",identity:"iCab"},{string:navigator.vendor,subString:"KDE",identity:"Konqueror"},{string:navigator.userAgent,subString:"Firefox",identity:"Firefox"},{string:navigator.vendor,subString:"Camino",identity:"Camino"},{string:navigator.userAgent,subString:"Netscape",identity:"Netscape"},{string:navigator.userAgent,subString:"MSIE",identity:"Explorer",versionSearch:"MSIE"},{string:navigator.userAgent,subString:"Gecko",identity:"Mozilla",versionSearch:"rv"},{string:navigator.userAgent,subString:"Mozilla",identity:"Netscape",versionSearch:"Mozilla"}],dataOS:[{string:navigator.platform,subString:"Win",identity:"Windows"},{string:navigator.platform,subString:"Mac",identity:"Mac"},{string:navigator.userAgent,subString:"iPhone",identity:"iPhone/iPod"},{string:navigator.userAgent,subString:"iPad",identitiy:"iPad"},{string:navigator.platform,subString:"Linux",identity:"Linux"}]},utils={stringify_json:JSON.stringify||function(a){var b=typeof a;if(b!="object"||a===null){b=="string"&&(a='"'+a+'"');return String(a)}var c,d,e=[],f=a&&a.constructor==Array;for(c in a)d=a[c],b=typeof d,b=="string"?d='"'+d+'"':b=="object"&&d!==null&&(d=JSON.stringify(d)),e.push((f?"":'"'+c+'":')+String(d));return(f?"[":"{")+String(e)+(f?"]":"}")},parse_json:JSON.parse||function(str){str===""&&(str='""'),eval("var p="+str+";");return p},set_cookie:function(a,b,c){var d=new Date;d.setDate(d.getDate()+c),document.cookie=a+"="+escape(b)+(c==null?"":";expires="+d.toGMTString())},get_cookie:function(a){if(document.cookie.length>0){c_start=document.cookie.indexOf(a+"=");if(c_start!=-1){c_start=c_start+a.length+1,c_end=document.cookie.indexOf(";",c_start),c_end==-1&&(c_end=document.cookie.length);return unescape(document.cookie.substring(c_start,c_end))}}return null},each:function(a,b,c){if(a!=null)if(a.length===+a.length){for(var d=0,e=a.length;d<e;d++)if(d in a&&b.call(c,a[d],d,a)==={})return}else for(var f in a)if(hasOwnProperty.call(a,f)&&b.call(c,a[f],f,a)==={})return},any:function(a,b,c){b||(b=_.identity);var d=!1;if(a==null)return d;utils.each(a,function(a,e,f){if(d||(d=b.call(c,a,e,f)))return{}});return!!d},search:function(a,b,c){var d;utils.any(a,function(a,e,f){d=b.call(c,a,e,f);if(d)return!0});return d},find:function(a,b,c){var d;utils.any(a,function(a,e,f){if(b.call(c,a,e,f)){d=a;return!0}});return d},is_undef:function(a){return a===void 0},try_obj:function(a,b){for(var c=0;c<a.length;c++){obj_try=b[a[c]];if(!utils.is_undef(obj_try)&&obj_try!=null&&obj_try!="")return obj_try}return null},try_props:function(a){for(var b=0;b<a.length;b++)if(!utils.is_undef(a[b])&&a[b]!=null&&a[b]!="")return a[b]}},modules={location:function(a){var b=function(a){var b=document.createElement("script");b.type="text/javascript",b.src=a,document.getElementsByTagName("head")[0].appendChild(b)};return function(c){var d=null;utils.get_cookie(a)?c(utils.parse_json(utils.get_cookie(a))):(win.gloader_loaded=function(){"google"in window&&(c(win.google.loader.ClientLocation),utils.set_cookie(a,utils.stringify_json(win.google.loader.ClientLocation),72e5))},b("https://www.google.com/jsapi?callback=gloader_loaded"))}},locale:function(){var a=utils.search(["language","browserLanguage","systemLanguage","userLanguage"],function(a){return navigator[a]}),b=a.split("-");return b.length==2?{country:b[1],lang:b[0]}:{country:a}},browser:function(){return BrowserDetect.detect_browser()},device:function(){var a={screen:{width:screen.width,height:screen.height},viewport:{}},b=doc.documentElement,c=doc.getElementsByTagName("body")[0],d=navigator.userAgent;a.viewport.width=win.innerWidth||b.clientWidth||c.clientWidth,a.viewport.height=win.innerHeight||b.clientHeight||c.clientHeight,a.is_phone=!!d.match(/(iPhone|iPod|blackberry|android 0.5|htc|lg|midp|mmp|mobile|nokia|opera mini|palm|pocket|psp|sgh|smartphone|symbian|treo mini|Playstation Portable|SonyEricsson|Samsung|MobileExplorer|PalmSource|Benq|Windows Phone|Windows Mobile|IEMobile|Windows CE|Nintendo Wii)/i),a.is_tablet=!!d.match(/(iPad|SCH-I800|xoom|kindle)/i),a.is_mobile=a.is_tablet||a.is_phone;return a},plugins:function(){var a=function(a){if(navigator.plugins)return!!utils.find(navigator.plugins,function(b){if(b&&b.name&&b.name.toLowerCase().indexOf(a)!==-1)return!0})};return{flash:a("flash"),silverlight:a("silverlight"),java:a("java"),quicktime:a("quicktime")}},session:function(a,b){if(a)var c=utils.get_cookie(a);if(!c){c={visits:1,search:{engine:null,query:null}};var d=[{nm:"Google",url:"https?://(?:www.)?(?:images.)?google.(?:com|[a-z]{2}|com?.[a-z]{2})",query:"q"},{nm:"Bing",url:"https?://(?:www.)?bing.com",query:"q"},{nm:"Yahoo",url:"https?://(?:www.)?(?:.+.)?search.yahoo.(?:com|[a-z]{2}|com?.[a-z]{2})",query:"p"},{nm:"AOL",url:"https?://(?:www.)?(?:aol)?search.aol.(?:com|[a-z]{2}|com?.[a-z]{2})",query:"q"},{nm:"Ask",url:"https?://(?:www.)?(?:[a-z]+.)?ask.com",query:"q"},{nm:"Baidu",url:"https?://(?:www.)?baidu.com",query:"wd"}];utils.find(d,function(a){var b=RegExp(a.url+"/.*[?&]"+a.query+"=([^&]+)").exec(doc.referrer);if(b){c.search.engine=a.nm,c.search.query=a.query;return!0}}),c.search.engine||utils.find(["q","query","term","p","wd","query","text"],function(a){var b=RegExp("[?&]"+a+"=([^&]+)").exec(doc.referrer);if(b){c.search.engine="Unknown",c.search.query=b;return!0}}),c.referrer=doc.referrer,c.url=win.location.href,c.path=win.location.pathname,c.start=(new Date).getTime(),c.last_visit=c.start,a&&utils.set_cookie(a,JSON.stringify(c),b)}else c=JSON.parse(c),c.last_visit=(new Date).getTime(),c.visits+=1,utils.set_cookie(a,JSON.stringify(c),b);return c}},session_loader={modules:{locale:modules.locale(),cur_session:modules.session(),orig_session:modules.session("first_session",864e5*(opts.session_days||32)),browser:modules.browser(),plugins:modules.plugins(),device:modules.device()},init:function(){opts.enable_location&&(session_loader.modules.location=modules.location("location"));var a=0,b=function(){a==0&&win.session_loaded&&win.session_loaded(win.session)};win.modules=session_loader.modules,win.session={api_version:.2};for(module_name in session_loader.modules)(function(c){var d=session_loader.modules[c];if(typeof d=="function"){var e=d;typeof e=="function"?(a++,e(function(d){win.session[c]=d,a--,b()})):win.session[c]=e}else win.session[c]=d})(module_name)}};session_loader.init()})(window,document)