forked from codejoust/session.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.min.js
8 lines (8 loc) · 8.44 KB
/
session.min.js
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
*/var session_fetch=function(a,b,c){var d=.4,e={use_html5_location:!1,ipinfodb_key:!1,gapi_location:!0,location_cookie:"location",location_cookie_timeout:5,session_timeout:32,session_cookie:"first_session"},f=function(){String.prototype.contains=function(a){if(typeof a=="string")return this.indexOf(a)!==-1;for(var b=0;b<a.length;b++)if(this.indexOf(a[b])!==-1)return!0;return!1};if(a.session&&a.session.options)for(option in a.session.options)e[option]=a.session.options[option];var b={api_version:d,locale:h.locale(),current_session:h.session(),original_session:h.session(e.session_cookie,e.session_timeout*24*60*60*1e3),browser:h.browser(),plugins:h.plugins(),time:h.time(),device:h.device()};e.use_html5_location?b.location=h.html5_location():e.ipinfodb_key?b.location=h.ipinfodb_location(e.ipinfodb_key):e.gapi_location&&(b.location=h.gapi_location());if(a.session&&a.session.start)var c=a.session.start;var f=0,g,i,j=function(b){b&&f--,f===0&&c&&c(a.session)};a.session={};for(var k in b){g=b[k];if(typeof g=="function")try{g(function(b){a.session[k]=b,j(!0)}),f++}catch(l){a.console&&typeof console.log=="function"&&(console.log(l),j(!0))}else a.session[k]=g}j()},g={detect:function(){return{browser:this.search(this.data.browser),version:this.search(c.userAgent)||this.search(c.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:c.userAgent,subString:"Chrome",identity:"Chrome"},{string:c.userAgent,subString:"OmniWeb",versionSearch:"OmniWeb/",identity:"OmniWeb"},{string:c.vendor,subString:"Apple",identity:"Safari",versionSearch:"Version"},{prop:a.opera,identity:"Opera",versionSearch:"Version"},{string:c.vendor,subString:"iCab",identity:"iCab"},{string:c.vendor,subString:"KDE",identity:"Konqueror"},{string:c.userAgent,subString:"Firefox",identity:"Firefox"},{string:c.vendor,subString:"Camino",identity:"Camino"},{string:c.userAgent,subString:"Netscape",identity:"Netscape"},{string:c.userAgent,subString:"MSIE",identity:"Explorer",versionSearch:"MSIE"},{string:c.userAgent,subString:"Gecko",identity:"Mozilla",versionSearch:"rv"},{string:c.userAgent,subString:"Mozilla",identity:"Netscape",versionSearch:"Mozilla"}],os:[{string:c.platform,subString:"Win",identity:"Windows"},{string:c.platform,subString:"Mac",identity:"Mac"},{string:c.userAgent,subString:"iPhone",identity:"iPhone/iPod"},{string:c.userAgent,subString:"iPad",identitiy:"iPad"},{string:c.platform,subString:"Linux",identity:"Linux"},{string:c.userAgent,subString:"Android",identity:"Android"}]}},h={browser:function(){return g.detect()},time:function(){var a=new Date,b=new Date;a.setMonth(0),a.setDate(1),b.setMonth(6),b.setDate(1);return{tz_offset:-(new Date).getTimezoneOffset()/60,observes_dst:a.getTimezoneOffset()!==b.getTimezoneOffset()}},locale:function(){var a=(c.language||c.browserLanguage||c.systemLanguage||c.userLanguage||"").split("-");return a.length==2?{country:a[1].toLowerCase(),lang:a[0].toLowerCase()}:a?{lang:a[0].toLowerCase(),country:null}:{lang:null,country:null}},device:function(){var d={screen:{width:a.screen.width,height:a.screen.height}};d.viewport={width:a.innerWidth||b.body.clientWidth||b.documentElement.clientWidth,height:a.innerHeight||b.body.clientHeight||b.documentElement.clientHeight},d.is_tablet=!!c.userAgent.match(/(iPad|SCH-I800|xoom|kindle)/i),d.is_phone=!d.is_tablet&&!!c.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),d.is_mobile=d.is_tablet||d.is_phone;return d},plugins:function(){var a=function(a){if(c.plugins){var b,d=0,e=c.plugins.length;for(;d<e;d++){b=c.plugins[d];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=i.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:i.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,h,j,k=0,l="q query term p wd query text".split(" ");for(k=0;k<g;k++){h=f[k];if(e.referrer_info.host.indexOf(h.host)!==-1){e.search.engine=h.name,e.search.query=e.referrer_info.query[h.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++;i.set_cookie(c,i.package_obj(e),d);return e},html5_location:function(){return function(a){c.geolocation.getCurrentPosition(function(b){b.source="html5",a(b)},function(b){e.gapi_location?h.gapi_location()(a):a({error:!0,source:"html5"})})}},gapi_location:function(){return function(b){var c=i.get_obj(e.location_cookie);!c||c.source!=="google"?(a.gloader_ready=function(){"google"in a&&(a.google.loader.ClientLocation?(a.google.loader.ClientLocation.source="google",b(a.google.loader.ClientLocation)):b({error:!0,source:"google"}),i.set_cookie(e.location_cookie,i.package_obj(a.google.loader.ClientLocation),e.location_cookie_timeout*60*60*1e3))},i.embed_script("https://www.google.com/jsapi?callback=gloader_ready")):b(c)}},ipinfodb_location:function(b){return function(c){var d=i.get_obj(e.location_cookie);d&&d.source==="ipinfodb"&&c(d),a.ipinfocb=function(a){if(a.statusCode==="OK")a.source="ipinfodb",i.set_cookie(e.location_cookie,i.package_obj(a),e.location_cookie*60*60*1e3),c(a);else{if(e.gapi_location)return h.gapi_location()(c);c({error:!0,source:"ipinfodb",message:a.statusMessage})}},i.embed_script("http://api.ipinfodb.com/v3/ip-city/?key="+b+"&format=json&callback=ipinfocb")}}},i={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(c,d,e,f){if(!c)return null;if(!f)var f={};if(d===null||d===undefined)e=-1;e&&(f.expires=(new Date).getTime()+e);return b.cookie=[encodeURIComponent(c),"=",encodeURIComponent(String(d)),f.expires?"; expires="+(new Date(f.expires)).toUTCString():"","; path="+(f.path?f.path:"/"),f.domain?"; domain="+f.domain:"",a.location&&a.location.protocol==="https:"?"; secure":""].join("")},get_cookie:function(a,c){return(c=(new RegExp("(?:^|; )"+encodeURIComponent(a)+"=([^;]*)")).exec(b.cookie))?decodeURIComponent(c[1]):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=d;var b=j.stringify(a);delete a.version;return b},get_obj:function(a){var b;try{b=j.parse(i.get_cookie(a))}catch(c){}if(b&&b.version==d){delete b.version;return b}}},j={parse:a.JSON&&a.JSON.parse||function(a){return typeof a!="string"||!a?null:(new Function("return "+a))()},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+'"'}};f()};typeof window.exports=="undefined"?session_fetch(window,document,navigator):window.exports.session=session_fetch