forked from codejoust/session.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.min.js
More file actions
8 lines (8 loc) · 7.99 KB
/
Copy pathsession.min.js
File metadata and controls
8 lines (8 loc) · 7.99 KB
1
2
3
4
5
6
7
8
/**
* session.js 0.4.1
* (c) 2012 Iain, CodeJoust
* session.js 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(a,b){var c=.4,d={use_html5_location:!1,ipinfodb_key:!1,gapi_location:!0,location_cookie:"location",location_cookie_timeout:2,session_timeout:32,session_cookie:"first_session"},e=function(){if(a.session&&a.session.options)for(option in a.session.options)d[option]=a.session.options[option];this.modules={api_version:c,locale:g.locale(),current_session:g.session(),original_session:g.session(d.session_cookie,d.session_timeout*24*60*60*1e3),browser:g.browser(),plugins:g.plugins(),device:g.device()},d.use_html5_location?this.modules.location=g.html5_location():d.ipinfodb_key?this.modules.location=g.ipinfodb_location(d.ipinfodb_key):d.gapi_location&&(this.modules.location=g.gapi_location());if(a.session&&a.session.start)var b=a.session.start;var e=0,f,h,i=this,j=function(){e===0&&(a.session=i.modules,b&&b(i.modules))};for(var k in this.modules)f=i.modules[k],typeof f=="function"?(e++,f(function(a){i.modules[k]=a,e--,j()})):i.modules[k]=f;j()},f={detect:function(){return{browser:this.search(this.data.browser),version:this.search(navigator.userAgent)||this.search(navigator.appVersion),os:this.search(this.data.os)}},search:function(a){if(typeof a!="object"){var e=a.indexOf(this.version_string);if(e==-1)return;return parseFloat(a.substr(e+this.version_string.length+1))}for(var b=0;b<a.length;b++){var c=a[b].string,d=a[b].prop;this.version_string=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}},data:{browser:[{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:a.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"}],os:[{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"},{string:navigator.userAgent,subString:"Android",identity:"Android"}]}},g={browser:function(){return f.detect()},locale:function(){var a=(navigator.language||navigator.browserLanguage||navigator.systemLanguage||navigator.userLanguage).split("-");return{country:a[1].toLowerCase(),lang:a[0].toLowerCase()}},device:function(){var c={screen:{width:screen.width,height:screen.height}},d=b.documentElement,e=b.getElementsByTagName("body")[0];c.viewport={width:a.innerWidth||d.clientWidth||e.clientWidth,height:a.innerHeight||d.clientHeight||e.clientHeight},c.is_tablet=!!navigator.userAgent.match(/(iPad|SCH-I800|xoom|kindle)/i),c.is_phone=!c.isTablet&&!!navigator.userAgent.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),c.is_mobile=c.is_tablet||c.is_phone;return c},plugins:function(){var a=function(a){if(navigator.plugins){var b,c=0,d=navigator.plugins.length;for(;c<d;c++){b=navigator.plugins[c];if(b&&b.name&&b.name.toLowerCase().indexOf(a)!==-1)return!0}return!1}return!1};return{flash:a("flash"),silverlight:a("silverlight"),java:a("java"),quicktime:a("quicktime")}},session:function(c,d){var e=h.get_obj(c);if(e==null){e={visits:1,start:(new Date).getTime(),last_visit:(new Date).getTime(),url:a.location.href,path:a.location.pathname,referrer:b.referrer,referrer_info:h.parse_url(b.referrer),search:{engine:null,query:null}};var f=[{name:"Google",host:"google",query:"q"},{name:"Bing",host:"bing.com",query:"q"},{name:"Yahoo",host:"search.yahoo",query:"p"},{name:"AOL",host:"search.aol",query:"q"},{name:"Ask",host:"ask.com",query:"q"},{name:"Baidu",host:"baidu.com",query:"wd"}],g=f.length,i,j,k=0,l="q query term p wd query text".split(" ");for(k=0;k<g;k++){i=f[k];if(e.referrer_info.host.indexOf(i.host)!==-1){e.search.engine=i.name,e.search.query=e.referrer_info.query[i.query],e.search.terms=e.search.query?e.search.query.split(" "):null;break}}if(e.search.engine===null&&e.referrer_info.search.length>1)for(k=0;k<l.length;k++){var m=e.referrer_info.query[l[k]];if(m){e.search.engine="Unknown",e.search.query=m,e.search.terms=m.split(" ");break}}}else e.last_visit=(new Date).getTime(),e.visits++;h.set_cookie(c,h.package_obj(e),d);return e},html5_location:function(){return function(a){navigator.geolocation.getCurrentPosition(function(b){b.source="html5",a(b)},function(b){d.gapi_location?g.gapi_location()(a):a({error:!0,source:"html5"})})}},gapi_location:function(){return function(b){var c=h.get_obj(d.location_cookie);!c||c.source!=="google"?(a.gloaderReady=function(){"google"in a&&(a.google.loader.ClientLocation?(a.google.loader.ClientLocation.source="google",b(a.google.loader.ClientLocation)):b({error:!0,source:"google"}),h.set_cookie(d.location_cookie,h.package_obj(a.google.loader.ClientLocation),d.location_cookie_timeout*60*60*1e3))},h.embed_script("https://www.google.com/jsapi?callback=gloaderReady")):b(c)}},ipinfodb_location:function(b){return function(c){var e=h.get_obj(d.location_cookie);e&&e.source==="ipinfodb"&&c(e),a.ipinfocb=function(a){if(a.statusCode==="OK")a.source="ipinfodb",h.set_cookie(d.location_cookie,h.package_obj(a),d.location_cookie*60*60*1e3),c(a);else{if(d.gapi_location)return g.gapi_location()(c);c({error:!0,source:"ipinfodb",message:a.statusMessage})}},h.embed_script("http://api.ipinfodb.com/v3/ip-city/?key="+b+"&format=json&callback=ipinfocb")}}},h=a.util={parse_url:function(a){var c=b.createElement("a"),d={};c.href=a,query_str=c.search.substr(1);if(query_str!=""){var e=query_str.split("&"),f=0,g=e.length,h;for(;f<g;f++)h=e[f].split("="),h.length===2&&(d[h[0]]=decodeURI(h[1]))}return{host:c.host,path:c.pathname,protocol:c.protocol,port:c.port===""?80:c.port,search:c.search,query:d}},set_cookie:function(a,c,d,e){if(!b.cookie||!a||!c)return null;e=e?"; path="+e:"; path=/";if(d){var f=new Date;f.setTime(f.getTime()+d),d="; expires="+f.toGMTString()}else d="";return b.cookie=a+"="+c+d+e},get_cookie:function(a){var c=a+"=",d=b.cookie.split(";");for(var e=0;e<d.length;e++){var f=d[e];while(f.charAt(0)==" ")f=f.substring(1,f.length);if(f.indexOf(c)==0)return f.substring(c.length,f.length)}return null},embed_script:function(a){var c=b.createElement("script");c.type="text/javascript",c.src=a,b.getElementsByTagName("body")[0].appendChild(c)},package_obj:function(a){a.version=c;var b=i.stringify(a);delete a.version;return b},get_obj:function(a){var b=i.parse(h.get_cookie(a));if(b&&b.version==c){delete b.version;return b}}},i={parse:a.JSON&&a.JSON.parse||function(a){try{return typeof a!="string"||!a?null:(new Function("return "+a))()}catch(b){return null}},stringify:a.JSON&&a.JSON.stringify||function(a){var b=typeof a;if(b==="object"&&a!==null){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=this.stringify(d)),e.push((f?"":'"'+c+'":')+d);return(f?"[":"{")+e.join(",")+(f?"]":"}")}if(b==="string")return'"'+a+'"'}};e()})(window,document)