mirror of
https://github.com/danog/telegram-tt.git
synced 2025-01-08 05:48:33 +01:00
2 lines
5.2 KiB
JavaScript
2 lines
5.2 KiB
JavaScript
(()=>{"use strict";const e="tt-media-progressive",t="tt-assets";"undefined"!=typeof window&&window.innerHeight,Math.round(425),new Set(["newMessage","newScheduledMessage","deleteMessages","deleteScheduledMessages","deleteHistory"]),new Set(["image/png","image/gif","image/jpeg","video/mp4","video/avi","video/quicktime"]);const s=524288,n=new Map;var a,i;self.addEventListener("message",(e=>{const{type:t,messageId:s,result:a}=e.data;if("partResponse"===t){const e=n.get(s);e&&e.resolve(a)}})),(i=a||(a={})).True="1",i.False="0";let r=(new Date).valueOf();const o={},c=new Set;function d(e){return e.custom.from_id?parseInt(e.custom.from_id,10):e.custom.chat_id?-1*parseInt(e.custom.chat_id,10):e.custom.channel_id?-1*parseInt(e.custom.channel_id,10):void 0}function l(e){if(e.custom.msg_id)return parseInt(e.custom.msg_id,10)}async function u({chatId:e,messageId:t,body:s,title:n,icon:a}){await self.registration.showNotification(n,{body:s,data:{chatId:e,messageId:t},icon:a||"icon-192x192.png",badge:a||"icon-192x192.png",vibrate:[200,100,200]}),await async function(e){const t=(await self.clients.matchAll({type:"window"})).filter((e=>e.url===self.registration.scope)),s=t[0];s&&0!==t.length&&s.postMessage({type:"playNotificationSound",payload:{id:e}})}(t||e||0)}async function f(e,t){const{chatId:s,messageId:n}=t;if(s){e.postMessage({type:"focusMessage",payload:{chatId:s,messageId:n}});try{await e.focus()}catch(e){}}}self.onsync=()=>{r=(new Date).valueOf()};const p=/[0-9a-f]{20}.*\.(js|css|woff2?|svg|png|jpg|jpeg|json|wasm)$/;self.addEventListener("install",(e=>{e.waitUntil(self.skipWaiting())})),self.addEventListener("activate",(e=>{e.waitUntil(self.caches.delete(t)),e.waitUntil(self.clients.claim())})),self.addEventListener("fetch",(a=>{const{url:i}=a.request;return!i.includes("_websync_")&&(a.respondWith(i.includes("/progressive/")?async function(t){const{url:a}=t.request,i=t.request.headers.get("range"),r=/^bytes=(\d+)-(\d+)?$/g.exec(i||""),o=Number(r[1]);let c=Number(r[2]);if((!c||c-o+1>s)&&(c=o+s-1),0===o&&1===c){const e=t.request.url.match(/fileSize=(\d+)&mimeType=([\w/]+)/),s=e&&Number(e[1]),n=e&&e[2];if(s&&n)return new Response(new Uint8Array(2).buffer,{status:206,statusText:"Partial Content",headers:[["Content-Range",`bytes 0-1/${s}`],["Accept-Ranges","bytes"],["Content-Length","2"],["Content-Type",n]]})}const d=`${a}?start=${o}&end=${c}`,[l,u]=await async function(t){const s=await self.caches.open(e);return Promise.all([s.match(`${t}&type=arrayBuffer`).then((e=>e?e.arrayBuffer():void 0)),s.match(`${t}&type=headers`).then((e=>e?e.json():void 0))])}(d);if(l)return new Response(l,{status:206,statusText:"Partial Content",headers:u});let f;try{f=await async function(e,t){if(!e.clientId)return;const s=await self.clients.get(e.clientId);if(!s)return;const a=(e=>{let t;do{t=String(Math.random()).replace("0.","id")}while(e.hasOwnProperty(t));return t})(n),i={},r=Promise.race([(o=6e4,new Promise((e=>{setTimeout((()=>e()),o)}))).then((()=>Promise.reject(new Error("ERROR_PART_TIMEOUT")))),new Promise(((e,t)=>{Object.assign(i,{resolve:e,reject:t})}))]);var o;return n.set(a,i),r.catch((()=>{})).finally((()=>{n.delete(a)})),s.postMessage({type:"requestPart",messageId:a,params:t}),r}(t,{url:a,start:o,end:c})}catch(e){}if(!f)return new Response("",{status:500,statusText:"Failed to fetch progressive part"});const{arrayBuffer:p,fullSize:g,mimeType:w}=f,m=Math.min(c-o+1,p.byteLength);c=o+m-1;const h=p.slice(0,m),y=[["Content-Range",`bytes ${o}-${c}/${g}`],["Accept-Ranges","bytes"],["Content-Length",String(m)],["Content-Type",w]];return m<=524288&&c<2097151&&async function(t,s,n){const a=await self.caches.open(e);Promise.all([a.put(new Request(`${t}&type=arrayBuffer`),new Response(s)),a.put(new Request(`${t}&type=headers`),new Response(JSON.stringify(n)))])}(d,h,y),new Response(h,{status:206,statusText:"Partial Content",headers:y})}(a):i.startsWith("http")&&i.match(p)?async function(e){const s=await self.caches.open(t),n=await s.match(e.request);if(n)return n;const a=await fetch(e.request);return s.put(e.request,a.clone()),a}(a):fetch(a.request)),!0)})),self.addEventListener("push",(function(e){if((new Date).valueOf()-r<3e3)return;const t=function(e){try{return e.data.json()}catch(e){return}}(e);if(!t||t.mute===a.True)return;const s=function(e){return{chatId:d(e),messageId:l(e),title:e.title||"Telegram WebZ",body:e.description}}(t);c.has(s.messageId)?c.delete(s.messageId):e.waitUntil(u(s))})),self.addEventListener("notificationclick",(function(e){const t=new URL(self.registration.scope).origin;e.notification.close();const{data:s}=e.notification;e.waitUntil((async()=>{const n=(await self.clients.matchAll({type:"window"})).filter((e=>new URL(e.url).origin===t));if(e.waitUntil(Promise.all(n.map((e=>(o[e.id]=s,f(e,s)))))),!self.clients.openWindow||n.length>0)return;const a=await self.clients.openWindow(t);a&&(o[a.id]=s)})())})),self.addEventListener("message",(function(e){if(!e.data)return;const t=e.source;if("clientReady"===e.data.type&&o[t.id]&&(e.waitUntil(f(t,o[t.id])),delete o[t.id]),"newMessageNotification"===e.data.type){const t=e.data.payload;e.waitUntil(u(t)),c.add(t.messageId)}if("notificationHandled"===e.data.type){const t=e.data.payload;c.add(t.messageId)}}))})();
|
|
//# sourceMappingURL=524.8d03e271cccb07e10be9.js.map
|