!function(t){function e(e){for(var r,i,o=e[0],u=e[1],l=e[2],c=0,f=[];c1;const p=(t,e,n)=>{const r=t[f].get(e);if(r){const e=r.value;if(g(t,e)){if(m(t,r),!t[o])return}else n&&(t[d]&&(r.value.now=Date.now()),t[c].unshiftNode(r));return e.value}},g=(t,e)=>{if(!e||!e.maxAge&&!t[u])return!1;const n=Date.now()-e.now;return e.maxAge?n>e.maxAge:t[u]&&n>t[u]},y=t=>{if(t[a]>t[s])for(let e=t[c].tail;t[a]>t[s]&&null!==e;){const n=e.prev;m(t,e),e=n}},m=(t,e)=>{if(e){const n=e.value;t[l]&&t[l](n.key,n.value),t[a]-=n.length,t[f].delete(n.key),t[c].removeNode(e)}};class j{constructor(t,e,n,r,s){this.key=t,this.value=e,this.length=n,this.now=r,this.maxAge=s||0}}const b=(t,e,n,r)=>{let s=n.value;g(t,s)&&(m(t,n),t[o]||(s=void 0)),s&&e.call(r,s.value,s.key,t)};t.exports=class{constructor(t){if("number"==typeof t&&(t={max:t}),t||(t={}),t.max&&("number"!=typeof t.max||t.max<0))throw new TypeError("max must be a non-negative number");this[s]=t.max||1/0;const e=t.length||v;if(this[i]="function"!=typeof e?v:e,this[o]=t.stale||!1,t.maxAge&&"number"!=typeof t.maxAge)throw new TypeError("maxAge must be a number");this[u]=t.maxAge||0,this[l]=t.dispose,this[h]=t.noDisposeOnSet||!1,this[d]=t.updateAgeOnGet||!1,this.reset()}set max(t){if("number"!=typeof t||t<0)throw new TypeError("max must be a non-negative number");this[s]=t||1/0,y(this)}get max(){return this[s]}set allowStale(t){this[o]=!!t}get allowStale(){return this[o]}set maxAge(t){if("number"!=typeof t)throw new TypeError("maxAge must be a non-negative number");this[u]=t,y(this)}get maxAge(){return this[u]}set lengthCalculator(t){"function"!=typeof t&&(t=v),t!==this[i]&&(this[i]=t,this[a]=0,this[c].forEach(t=>{t.length=this[i](t.value,t.key),this[a]+=t.length})),y(this)}get lengthCalculator(){return this[i]}get length(){return this[a]}get itemCount(){return this[c].length}rforEach(t,e){e=e||this;for(let n=this[c].tail;null!==n;){const r=n.prev;b(this,t,n,e),n=r}}forEach(t,e){e=e||this;for(let n=this[c].head;null!==n;){const r=n.next;b(this,t,n,e),n=r}}keys(){return this[c].toArray().map(t=>t.key)}values(){return this[c].toArray().map(t=>t.value)}reset(){this[l]&&this[c]&&this[c].length&&this[c].forEach(t=>this[l](t.key,t.value)),this[f]=new Map,this[c]=new r,this[a]=0}dump(){return this[c].map(t=>!g(this,t)&&{k:t.key,v:t.value,e:t.now+(t.maxAge||0)}).toArray().filter(t=>t)}dumpLru(){return this[c]}set(t,e,n){if((n=n||this[u])&&"number"!=typeof n)throw new TypeError("maxAge must be a number");const r=n?Date.now():0,o=this[i](e,t);if(this[f].has(t)){if(o>this[s])return m(this,this[f].get(t)),!1;const i=this[f].get(t).value;return this[l]&&(this[h]||this[l](t,i.value)),i.now=r,i.maxAge=n,i.value=e,this[a]+=o-i.length,i.length=o,this.get(t),y(this),!0}const d=new j(t,e,o,r,n);return d.length>this[s]?(this[l]&&this[l](t,e),!1):(this[a]+=d.length,this[c].unshift(d),this[f].set(t,this[c].head),y(this),!0)}has(t){if(!this[f].has(t))return!1;const e=this[f].get(t).value;return!g(this,e)}get(t){return p(this,t,!0)}peek(t){return p(this,t,!1)}pop(){const t=this[c].tail;return t?(m(this,t),t.value):null}del(t){m(this,this[f].get(t))}load(t){this.reset();const e=Date.now();for(let n=t.length-1;n>=0;n--){const r=t[n],s=r.e||0;if(0===s)this.set(r.k,r.v);else{const t=s-e;t>0&&this.set(r.k,r.v,t)}}}prune(){this[f].forEach((t,e)=>p(this,e,!1))}}},28:function(t,e,n){"use strict";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function s(t,e){for(var n=0;n0?i.ids.pop():i.maxId++,i.inFlight[r]=e,n.port.postMessage(new p(r,s,a).deflate()),o});var s=chrome.runtime.connect({name:e});s.onMessage.addListener(function(t){console.log(t);var e=(t.type,n.requestCallback),r=e.inFlight[t.id];console.assert(void 0!==r),e.ids.push(t.id),r(t)}),this.port=s,this.requestCallback={inFlight:{},ids:[],maxId:0}}},301:function(t,e,n){var r={"./af":87,"./af.js":87,"./ar":88,"./ar-dz":89,"./ar-dz.js":89,"./ar-kw":90,"./ar-kw.js":90,"./ar-ly":91,"./ar-ly.js":91,"./ar-ma":92,"./ar-ma.js":92,"./ar-sa":93,"./ar-sa.js":93,"./ar-tn":94,"./ar-tn.js":94,"./ar.js":88,"./az":95,"./az.js":95,"./be":96,"./be.js":96,"./bg":97,"./bg.js":97,"./bm":98,"./bm.js":98,"./bn":99,"./bn.js":99,"./bo":100,"./bo.js":100,"./br":101,"./br.js":101,"./bs":102,"./bs.js":102,"./ca":103,"./ca.js":103,"./cs":104,"./cs.js":104,"./cv":105,"./cv.js":105,"./cy":106,"./cy.js":106,"./da":107,"./da.js":107,"./de":108,"./de-at":109,"./de-at.js":109,"./de-ch":110,"./de-ch.js":110,"./de.js":108,"./dv":111,"./dv.js":111,"./el":112,"./el.js":112,"./en-SG":113,"./en-SG.js":113,"./en-au":114,"./en-au.js":114,"./en-ca":115,"./en-ca.js":115,"./en-gb":116,"./en-gb.js":116,"./en-ie":117,"./en-ie.js":117,"./en-il":118,"./en-il.js":118,"./en-nz":119,"./en-nz.js":119,"./eo":120,"./eo.js":120,"./es":121,"./es-do":122,"./es-do.js":122,"./es-us":123,"./es-us.js":123,"./es.js":121,"./et":124,"./et.js":124,"./eu":125,"./eu.js":125,"./fa":126,"./fa.js":126,"./fi":127,"./fi.js":127,"./fo":128,"./fo.js":128,"./fr":129,"./fr-ca":130,"./fr-ca.js":130,"./fr-ch":131,"./fr-ch.js":131,"./fr.js":129,"./fy":132,"./fy.js":132,"./ga":133,"./ga.js":133,"./gd":134,"./gd.js":134,"./gl":135,"./gl.js":135,"./gom-latn":136,"./gom-latn.js":136,"./gu":137,"./gu.js":137,"./he":138,"./he.js":138,"./hi":139,"./hi.js":139,"./hr":140,"./hr.js":140,"./hu":141,"./hu.js":141,"./hy-am":142,"./hy-am.js":142,"./id":143,"./id.js":143,"./is":144,"./is.js":144,"./it":145,"./it-ch":146,"./it-ch.js":146,"./it.js":145,"./ja":147,"./ja.js":147,"./jv":148,"./jv.js":148,"./ka":149,"./ka.js":149,"./kk":150,"./kk.js":150,"./km":151,"./km.js":151,"./kn":152,"./kn.js":152,"./ko":153,"./ko.js":153,"./ku":154,"./ku.js":154,"./ky":155,"./ky.js":155,"./lb":156,"./lb.js":156,"./lo":157,"./lo.js":157,"./lt":158,"./lt.js":158,"./lv":159,"./lv.js":159,"./me":160,"./me.js":160,"./mi":161,"./mi.js":161,"./mk":162,"./mk.js":162,"./ml":163,"./ml.js":163,"./mn":164,"./mn.js":164,"./mr":165,"./mr.js":165,"./ms":166,"./ms-my":167,"./ms-my.js":167,"./ms.js":166,"./mt":168,"./mt.js":168,"./my":169,"./my.js":169,"./nb":170,"./nb.js":170,"./ne":171,"./ne.js":171,"./nl":172,"./nl-be":173,"./nl-be.js":173,"./nl.js":172,"./nn":174,"./nn.js":174,"./pa-in":175,"./pa-in.js":175,"./pl":176,"./pl.js":176,"./pt":177,"./pt-br":178,"./pt-br.js":178,"./pt.js":177,"./ro":179,"./ro.js":179,"./ru":180,"./ru.js":180,"./sd":181,"./sd.js":181,"./se":182,"./se.js":182,"./si":183,"./si.js":183,"./sk":184,"./sk.js":184,"./sl":185,"./sl.js":185,"./sq":186,"./sq.js":186,"./sr":187,"./sr-cyrl":188,"./sr-cyrl.js":188,"./sr.js":187,"./ss":189,"./ss.js":189,"./sv":190,"./sv.js":190,"./sw":191,"./sw.js":191,"./ta":192,"./ta.js":192,"./te":193,"./te.js":193,"./tet":194,"./tet.js":194,"./tg":195,"./tg.js":195,"./th":196,"./th.js":196,"./tl-ph":197,"./tl-ph.js":197,"./tlh":198,"./tlh.js":198,"./tr":199,"./tr.js":199,"./tzl":200,"./tzl.js":200,"./tzm":201,"./tzm-latn":202,"./tzm-latn.js":202,"./tzm.js":201,"./ug-cn":203,"./ug-cn.js":203,"./uk":204,"./uk.js":204,"./ur":205,"./ur.js":205,"./uz":206,"./uz-latn":207,"./uz-latn.js":207,"./uz.js":206,"./vi":208,"./vi.js":208,"./x-pseudo":209,"./x-pseudo.js":209,"./yo":210,"./yo.js":210,"./zh-cn":211,"./zh-cn.js":211,"./zh-hk":212,"./zh-hk.js":212,"./zh-tw":213,"./zh-tw.js":213};function s(t){var e=a(t);return n(e)}function a(t){var e=r[t];if(!(e+1)){var n=new Error("Cannot find module '"+t+"'");throw n.code="MODULE_NOT_FOUND",n}return e}s.keys=function(){return Object.keys(r)},s.resolve=a,t.exports=s,s.id=301},308:function(t,e,n){"use strict";function r(t){var e=this;if(e instanceof r||(e=new r),e.tail=null,e.head=null,e.length=0,t&&"function"==typeof t.forEach)t.forEach(function(t){e.push(t)});else if(arguments.length>0)for(var n=0,s=arguments.length;n1)n=e;else{if(!this.head)throw new TypeError("Reduce of empty list with no initial value");r=this.head.next,n=this.head.value}for(var s=0;null!==r;s++)n=t(n,r.value,s),r=r.next;return n},r.prototype.reduceReverse=function(t,e){var n,r=this.tail;if(arguments.length>1)n=e;else{if(!this.tail)throw new TypeError("Reduce of empty list with no initial value");r=this.tail.prev,n=this.tail.value}for(var s=this.length-1;null!==r;s--)n=t(n,r.value,s),r=r.prev;return n},r.prototype.toArray=function(){for(var t=new Array(this.length),e=0,n=this.head;null!==n;e++)t[e]=n.value,n=n.next;return t},r.prototype.toArrayReverse=function(){for(var t=new Array(this.length),e=0,n=this.tail;null!==n;e++)t[e]=n.value,n=n.prev;return t},r.prototype.slice=function(t,e){(e=e||this.length)<0&&(e+=this.length),(t=t||0)<0&&(t+=this.length);var n=new r;if(ethis.length&&(e=this.length);for(var s=0,a=this.head;null!==a&&sthis.length&&(e=this.length);for(var s=this.length,a=this.tail;null!==a&&s>e;s--)a=a.prev;for(;null!==a&&s>t;s--,a=a.prev)n.push(a.value);return n},r.prototype.reverse=function(){for(var t=this.head,e=this.tail,n=t;null!==n;n=n.prev){var r=n.prev;n.prev=n.next,n.next=r}return this.head=e,this.tail=t,this};try{n(309)(r)}catch(t){}},309:function(t,e,n){"use strict";t.exports=function(t){t.prototype[Symbol.iterator]=function*(){for(let t=this.head;t;t=t.next)yield t.value}}},32:function(t,e,n){"use strict";n.d(e,"a",function(){return l});var r=n(3),s=n.n(r);function a(t,e){for(var n=0;n0&&void 0!==arguments[0]&&arguments[0];return new Promise(function(e){return chrome.identity.getAuthToken({interactive:t},function(t){return e([t,!chrome.runtime.lastError])})}).then(function(t){var e=o(t,2),n=e[0];if(e[1])return n;throw l.notLoggedIn})}function d(){return null===c?f(!1).then(function(){return c=!0}).catch(function(){return c=!1}).then(function(){return c}):Promise.resolve(c)}function v(){return d().then(function(t){if(t)return f(!1);throw l.notLoggedIn})}function p(){return d().then(function(t){if(t)throw l.notLoggedOut;return f(!0).then(function(){return c=!0})})}function g(){return v().then(function(t){return fetch("https://accounts.google.com/o/oauth2/revoke?".concat(h({token:t})),{method:"GET",async:!0}).then(function(e){return function(t){return new Promise(function(e){return chrome.identity.removeCachedAuthToken({token:t},function(){return e()})})}(t)})}).then(function(){return c=!1})}function y(t){return fetch("".concat(u,"/users/me/calendarList?").concat(h({access_token:t})),{method:"GET",async:!0}).then(function(t){return t.json()}).then(function(t){return t.items})}function m(t){return fetch("".concat(u,"/colors?").concat(h({access_token:t})),{method:"GET",async:!0}).then(function(t){return t.json()})}function j(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null,s=arguments.length>4&&void 0!==arguments[4]?arguments[4]:null,a=arguments.length>5&&void 0!==arguments[5]?arguments[5]:100,o=[];return function n(c,f){return fetch("".concat(u,"/calendars/").concat(t,"/events?").concat(h({access_token:e,pageToken:c,syncToken:f,timeMin:r,timeMax:s,maxResults:a})),{method:"GET",async:!0}).then(function(t){if(200===t.status)return t.json();throw 410===t.status?l.invalidSyncToken:l.otherError}).then(function(t){return o.push.apply(o,i(t.items)),t.nextPageToken?n(t.nextPageToken,""):{nextSyncToken:t.nextSyncToken,results:o}})}("",n)}var b=function(){function t(e,n){var r=this,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{maxCachedItems:100,nDaysPerSlot:10,largeQuery:10};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.calId=e,this.name=n,this.syncToken="",this.cache=new s.a({max:a.maxCachedItems,dispose:function(t,e){return r.onRemoveSlot(t,e)}}),this.eventMeta={},this.options=a,this.divider=864e5*this.options.nDaysPerSlot}var e,n,r;return e=t,(n=[{key:"dateToCacheKey",value:function(t){return Math.floor(t/this.divider)}},{key:"dateRangeToCacheKeys",value:function(t){return{start:this.dateToCacheKey(t.start),end:this.dateToCacheKey(new Date(t.end.getTime()-1))}}},{key:"getSlot",value:function(t){if(this.cache.has(t))return this.cache.get(t);var e={};return this.cache.set(t,e),e}},{key:"onRemoveSlot",value:function(t,e){for(var n in e){console.assert(this.eventMeta[n]);var r=this.eventMeta[n].keys;r.delete(t),0===r.size&&delete this.eventMeta[n]}}},{key:"slotStartDate",value:function(t){return new Date(t*this.divider)}},{key:"slotEndDate",value:function(t){return new Date((t+1)*this.divider)}},{key:"addEvent",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1];this.eventMeta.hasOwnProperty(t.id)&&this.removeEvent(t);for(var n=this.dateRangeToCacheKeys(t),r=n.start,s=n.end,a=this.cache.length,i=new Set,o=r;o<=s;o++)i.add(o),this.cache.has(o)||a++;if(this.eventMeta[t.id]={keys:i,summary:t.summary},e||!(a>this.options.maxCachedItems))if(r===s)this.getSlot(r)[t.id]={start:t.start,end:t.end,id:t.id};else{this.getSlot(r)[t.id]={start:t.start,end:this.slotEndDate(r),id:t.id},this.getSlot(s)[t.id]={start:this.slotStartDate(s),end:t.end,id:t.id};for(var u=r+1;u=n||r[a].end<=e||s.push({id:a,start:r[a].startn?n:r[a].end,summary:this.eventMeta[a].summary});return s}},{key:"getCachedEvents",value:function(t){for(var e=this.dateRangeToCacheKeys(t),n=e.start,r=e.end,s=this.getSlotEvents(n,t.start,t.end),a=n+1;an&&s.push.apply(s,i(this.getSlotEvents(r,t.start,t.end))),s}},{key:"sync",value:function(){var t=this;return this.token.then(function(e){return j(t.calId,e,t.syncToken).then(function(n){var r=n.results.map(function(n){return n.start?Promise.resolve(n):function(t,e,n){return fetch("".concat(u,"/calendars/").concat(t,"/events/").concat(e,"?").concat(h({access_token:n})),{method:"GET",async:!0}).then(function(t){return t.json()})}(t.calId,n.id,e)});return Promise.all(r).then(function(e){e.forEach(function(e){e.start=new Date(e.start.dateTime),e.end=new Date(e.end.dateTime),"confirmed"===e.status?t.addEvent(e):"cancelled"===e.status&&t.removeEvent(e)}),t.syncToken=n.nextSyncToken})})}).catch(function(e){if(e!==l.invalidSyncToken)throw e;t.syncToken="",t.sync()})}},{key:"getEvents",value:function(t,e){for(var n=this,r=this.dateRangeToCacheKeys({start:t,end:e}),s={},a=r.start;a<=r.end;a++)this.cache.has(a)||(s.hasOwnProperty("start")||(s.start=a),s.end=a);return console.log("start: ".concat(t," end: ").concat(e)),s.hasOwnProperty("start")?(console.assert(s.start<=s.end),s.end-s.start+1>this.options.largeQuery?(console.log("encounter large query, use direct fetch"),this.token.then(function(r){return j(n.calId,r,null,t.toISOString(),e.toISOString()).then(function(n){var r=[];return n.results.forEach(function(t){console.assert(t.start),t.start=new Date(t.start.dateTime),t.end=new Date(t.end.dateTime),r.push(t)}),r.filter(function(n){return!(n.start>=e||n.end<=t)}).map(function(n){return{id:n.id,start:n.starte?e:n.end,summary:n.summary}})})})):(console.log("fetching short event list"),this.token.then(function(t){return j(n.calId,t,null,n.slotStartDate(s.start).toISOString(),n.slotEndDate(s.end).toISOString()).then(function(t){t.results.forEach(function(t){"confirmed"===t.status&&(console.assert(t.start),t.start=new Date(t.start.dateTime),t.end=new Date(t.end.dateTime),n.addEvent(t,!0))}),""===n.syncToken&&(n.syncToken=t.nextSyncToken)})}).then(function(){return n.sync()}).then(function(){return n.getCachedEvents({start:t,end:e})}))):(console.log("cache hit"),this.sync().then(function(){return n.getCachedEvents({start:t,end:e})}))}},{key:"token",get:function(){return v()}}])&&a(e.prototype,n),r&&a(e,r),t}()},852:function(t,e,n){"use strict";n.r(e);var r=n(46),s=n(28),a=n(32),i=[],o=[],u={},l={},h={trackedPeriods:[{name:"Today",start:a.a.days(1),end:a.a.days(0)},{name:"Yesterday",start:a.a.days(2),end:a.a.days(1)},{name:"This Week",start:a.a.weeks(1),end:a.a.weeks(0)},{name:"This Month",start:a.a.months(1),end:a.a.months(0)}]};chrome.runtime.onConnect.addListener(function(t){console.assert("main"==t.name),t.onMessage.addListener(function(e){var n=s.a.inflate(e);switch(console.log(n),n.type){case s.c.updatePatterns:"analyze"==n.data.id?o=n.data.patterns:i=n.data.patterns,t.postMessage(n.genResp(null));break;case s.c.getPatterns:var a;a="analyze"==n.data.id?o:i,t.postMessage(n.genResp(a));break;case s.c.updateCalendars:for(var c in u=n.data)l.hasOwnProperty(c)||(l[c]=new r.a(c,u[c].summary));t.postMessage(n.genResp(null));break;case s.c.getCalendars:var f=u;n.data.enabledOnly&&(f=Object.keys(u).filter(function(t){return u[t].enabled}).reduce(function(t,e){return t[e]=u[e],t},{})),t.postMessage(n.genResp(f));break;case s.c.getCalEvents:l[n.data.id].getEvents(new Date(n.data.start),new Date(n.data.end)).catch(function(t){return console.log("cannot load calendar ".concat(n.data.id),t),[]}).then(function(e){console.log(e);var r=n.genResp(e.map(function(t){return{id:t.id,start:t.start.getTime(),end:t.end.getTime()}}));console.log(r),t.postMessage(r)});break;case s.c.updateConfig:for(var d in n.data)h[d]=n.data[d];t.postMessage(n.genResp(null));break;case s.c.getConfig:var v={};n.data.forEach(function(t){return v[t]=h[t]}),t.postMessage(n.genResp(v));break;default:console.error("unknown msg type")}})}),chrome.browserAction.onClicked.addListener(function(){chrome.tabs.create({url:"index.html"})})}});