Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
3 additions
and
3 deletions.
There are no files selected for viewing
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
"use strict";function parseQueryString(e){var t=e.split("&");if(""===t)return{};for(var n={},o=0;o<t.length;++o){var r=t[o].split("=",2);1===r.length?n[r[0]]="":n[r[0]]=decodeURIComponent(r[1].replace(/+/g," "))}return n}function replace(e){var t,n,o;return t="https://android.googleapis.com/gcm/send/",n=e.search(t),o=e,n!==-1&&(o=o.replace(t,"")),o}function endpointWorkaround(e){if(0!==e.endpoint.indexOf("https://android.googleapis.com/gcm/send"))return e.endpoint;var t=e.endpoint;return e.subscriptionId&&e.endpoint.indexOf(e.subscriptionId)===-1&&(t=e.endpoint+"/"+e.subscriptionId),t}function parseResponce(e){return e.json().then(function(e){if(e.error||!e.notification)throw new Error;var t=e.notification.tag||_roostSW.tag,n=e.notification.message||_roostSW.message,o=e.notification.title||_roostSW.title,r=e.notification.icon||_roostSW.icon;return self.registration.showNotification(o,{icon:r,body:n,tag:t,vibrate:[500,100,500]})})}function getUserData(){var e=endpointWorkaround(self.subscription);e=replace(e);var t=_roostSW.apiUrl+"/pushnotify?version="+_roostSW.version+"&deviceID="+e+"&receiver_id="+(self.userId||"");return fetch(t,{credentials:"include"})}function prepareSubscription(e){return new Promise(function(t,n){return e?(self.subscription=e,void t()):n()})}function connectToDb(){return new Promise(function(e,t){return db?e():(openRequest=indexedDB.open("flirchi-serviceworker",dbVersion),openRequest.onupgradeneeded=function(e){var t=e.target.result;t.objectStoreNames.contains(storeName)||t.createObjectStore(storeName)},openRequest.onsuccess=function(t){db=t.target.result,e()},void(openRequest.onerror=function(e){throw t(),new Error("Open db error",e.message)}))})}function getUserId(){return new Promise(function(e,t){var n=db.transaction([storeName]).objectStore(storeName).get(user_id_key);n.onsuccess=function(t){self.userId=t.target.result?t.target.result.userId:void 0,self._roostSW.message=t.target.result?t.target.result.defaultMessage:self._roostSW.message,self._roostSW.icon=t.target.result?t.target.result.defaultIcon:self._roostSW.icon,self._roostSW.title=t.target.result?t.target.result.defaultTitle:self._roostSW.title,e()},n.onerror=function(){t(n.error)}})}function saveUserId(e){"undefined"!=typeof db&&db.transaction([storeName],"readwrite").objectStore(storeName).put(e,user_id_key)}function onMessage(e){var t=e.data;"userId"in t&&(self._roostSW.message=t.defaultMessage,self._roostSW.title=t.defaultTitle,self._roostSW.icon=t.defaultIcon,self.userId=t.userId,connectToDb().then(function(){saveUserId(t)}))}var _roostSW={version:9,appKey:"AIzaSyARRRo_vRL_bn83hJuyCGv-FwMu4tr-XaI",host:self.location.origin,apiUrl:"https://api.hitwe.com",message:"Hitwe – website for meetings!",title:"Hitwe",icon:self.location.protocol+"//static.phts.io/main/images/fresh/notification_icon.png",link:"/?track_url=chrome_push_default_msg_worker",tag:""},userId,db,dbVersion=1,storeName="data",user_id_key=0,openRequest,CACHE_NAME="CACHE_17";self.addEventListener("install",function(e){e.waitUntil(self.skipWaiting())}),self.addEventListener("activate",function(e){e.waitUntil(caches.keys().then(function(e){return Promise.all(e.map(function(e){if(e!==CACHE_NAME)return console.log("delete cache"),caches"delete"}))}))}),self.addEventListener("message",onMessage),self.onmessage&&self.onmessage.addEventListener("message",onMessage),self.addEventListener("push",function(e){e.waitUntil(self.registration.pushManager.getSubscription().then(prepareSubscription).then(connectToDb).then(getUserId).then(getUserData).then(parseResponce)["catch"](function(e){var t=_roostSW.tag,n=_roostSW.message,o=_roostSW.title,r=_roostSW.icon;return self.registration.showNotification(o,{body:n,icon:r,tag:t,vibrate:[500,100,500]})}))}),self.addEventListener("notificationclick",function(e){var t=e.notification.icon,n={};if(t.indexOf("?")>=0){var o=t.split("?")[1];n=parseQueryString(o),t=n.url||_roostSW.link}else t=_roostSW.link;e.notification.close(),e.waitUntil(clients.matchAll({type:"window"}).then(function(){var e,o=t+"?",r="";delete n.url;for(e in n)r=e+"="+n[e]+"&",o+=r;return o=o.substr(0,o.length-1),clients.openWindow(o)}))}),self.addEventListener("pushsubscriptionchange",function(e){console.log("Subscription expired"),e.waitUntil(self.registration.pushManager.subscribe({userVisibleOnly:!0}).then(function(e){var t=endpointWorkaround(e);t=replace(t);var n=_roostSW.apiUrl+"/pushnotify",o=new FormData;return o.append("uid",self.userId),o.append("version",self.version),o.append("endpoint",t),fetch(n,{method:"post",credentials:"include",body:o})}))}),self.addEventListener("fetch",function(e){var t=e.request.url;self.location.origin.indexOf("https://m.")<0||t.match(/.png|.jpg|.svg/g)&&t.indexOf("cache=1")>=0&&e.respondWith(caches.match(e.request).then(function(t){if(t)return t;var n=e.request.clone();return fetch(n).then(function(t){if(!t||200!==t.status&&0!==t.status)return t;var n=t.clone();return caches.open(CACHE_NAME).then(function(t){console.log("PUT TO CACHE",e.request.url),t.put(e.request,n)}),t})})"catch")}),self.addEventListener("notificationclose",function(){var e=endpointWorkaround(self.subscription);e=replace(e);var t=_roostSW.apiUrl+"/pushnotify?version="+_roostSW.version+"&deviceID="+e+"&receiver_id="+(self.userId||"")+"&closePush=1";fetch(t,{credentials:"include"})});