This commit is contained in:
ThaUnknown 2021-03-22 18:45:19 +01:00
parent f616622581
commit 7261c95865
3 changed files with 111 additions and 110 deletions

View file

@ -17,15 +17,15 @@ function applySettingsTimeout () {
}
function saveSettings () {
settingsElements.forEach(element => {
element.type == 'checkbox' ? settings[element.id] = element.checked : settings[element.id] = element.value
element.type === 'checkbox' ? settings[element.id] = element.checked : settings[element.id] = element.value
})
localStorage.setItem('settings', JSON.stringify(settings))
}
function renderSettings () {
Object.entries(settings).forEach(setting => {
const settingElement = settingsElements.filter(e => e.id == setting[0])[0]
if (settingElement) settingElement.type == 'checkbox' ? settingElement.checked = setting[1] : settingElement.value = setting[1]
const settingElement = settingsElements.filter(e => e.id === setting[0])[0]
if (settingElement) settingElement.type === 'checkbox' ? settingElement.checked = setting[1] : settingElement.value = setting[1]
})
}
function registerProtocol () {
@ -50,7 +50,7 @@ clearRelCache.onclick = () => {
}
renderSettings()
other1.onclick = () => Notification.requestPermission().then(function (perm) { perm == 'denied' ? other1.checked = false : other1.checked = true })
other1.onclick = () => Notification.requestPermission().then(function (perm) { perm === 'denied' ? other1.checked = false : other1.checked = true })
const searchParams = new URLSearchParams(location.href)
if (searchParams.get('access_token')) {
@ -59,7 +59,7 @@ if (searchParams.get('access_token')) {
}
const userBrowser = (function () {
if (window.chrome) {
if (navigator.userAgent.indexOf('Edg') != -1) {
if (navigator.userAgent.indexOf('Edg') !== -1) {
return 'edge'
} else {
return 'chromium'

130
app/sw.js
View file

@ -1,75 +1,77 @@
self.addEventListener('install', e => {
self.skipWaiting();
});
self.skipWaiting()
})
self.addEventListener('activate', e => {
return self.clients.claim()
return self.clients.claim()
})
self.addEventListener('fetch', evt => {
const { request } = evt
const { url, method, headers } = request
if (!url.includes(self.registration.scope + 'webtorrent/')) return null
const { request } = evt
const { url, method, headers } = request
if (!url.includes(self.registration.scope + 'webtorrent/')) return null
function getConsumer(clients) {
return new Promise(rs => {
// Use race condition for whoever controls the response stream
for (const client of clients) {
const mc = new MessageChannel()
const { port1, port2 } = mc
port1.onmessage = evt => {
rs([evt.data, mc])
}
client.postMessage({
url,
method,
headers: [...headers],
scope: self.registration.scope
}, [port2])
}
})
}
function getConsumer (clients) {
return new Promise(resolve => {
// Use race condition for whoever controls the response stream
for (const client of clients) {
const mc = new MessageChannel()
const { port1, port2 } = mc
port1.onmessage = evt => {
resolve([evt.data, mc])
}
client.postMessage({
url,
method,
headers: [...headers],
scope: self.registration.scope
}, [port2])
}
})
}
evt.respondWith(
clients.matchAll({ type: 'window', includeUncontrolled: true })
.then(getConsumer)
.then(([data, mc]) => {
let tm = null
const body = data.body === 'stream' ? new ReadableStream({
pull(controller) {
return new Promise(rs => {
mc.port1.onmessage = evt => {
if (evt.data) {
controller.enqueue(evt.data) // evt.data is Uint8Array
} else {
clearTimeout(tm)
controller.close() // evt.data is null, means the stream ended
mc.port1.onmessage = null
}
rs()
}
// 'media player' does NOT signal a close on the stream and we cannot close it because it's locked to the reader,
// so we just empty it after 5s of inactivity,
// the browser will request another port anyways
clearTimeout(tm)
tm = setTimeout(() => {
controller.close()
mc.port1.postMessage(false) // send timeout
mc.port1.onmessage = null
rs()
}, 5000)
evt.respondWith(
clients.matchAll({ type: 'window', includeUncontrolled: true })
.then(getConsumer)
.then(([data, mc]) => {
let tm = null
const body = data.body === 'stream'
? new ReadableStream({
pull (controller) {
return new Promise(resolve => {
mc.port1.onmessage = evt => {
if (evt.data) {
controller.enqueue(evt.data) // evt.data is Uint8Array
} else {
clearTimeout(tm)
controller.close() // evt.data is null, means the stream ended
mc.port1.onmessage = null
}
resolve()
}
// 'media player' does NOT signal a close on the stream and we cannot close it because it's locked to the reader,
// so we just empty it after 5s of inactivity,
// the browser will request another port anyways
clearTimeout(tm)
tm = setTimeout(() => {
controller.close()
mc.port1.postMessage(false) // send timeout
mc.port1.onmessage = null
resolve()
}, 5000)
mc.port1.postMessage(true) // send a pull request
})
},
cancel() {
// This event is never executed
mc.port1.postMessage(false) // send a cancel request
}
}) : data.body
mc.port1.postMessage(true) // send a pull request
})
},
cancel () {
// This event is never executed
mc.port1.postMessage(false) // send a cancel request
}
})
: data.body
return new Response(body, data)
})
.catch(console.error)
)
return new Response(body, data)
})
.catch(console.error)
)
})

81
sw.js
View file

@ -1,52 +1,51 @@
'use strict';
'use strict'
const staticCacheName = 'v1.0.0';
const staticCacheName = 'v1.0.0'
const filesToCache = [
'/app/index.html',
'/app/js/settingsHandler.js',
'/app/js/animeHandler.js',
'/app/js/playerHandler.js',
'/app/js/torrentHandler.js',
'/app/js/rangeParser.js',
'/app/js/util.js',
'/app/css/misc.css',
'/app/css/player.css',
'/app/logo.png',
'https://cdn.jsdelivr.net/npm/matroska-subtitles@3.0.1/dist/matroska-subtitles.min.js',
'https://cdn.jsdelivr.net/npm/halfmoon@1.1.0/css/halfmoon-variables.min.css',
'https://cdn.jsdelivr.net/gh/halfmoonui/halfmoon@1.1.0/js/halfmoon.min.js',
'https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js',
'https://fonts.googleapis.com/icon?family=Material+Icons'
];
'/app/index.html',
'/app/js/settingsHandler.js',
'/app/js/animeHandler.js',
'/app/js/playerHandler.js',
'/app/js/torrentHandler.js',
'/app/js/rangeParser.js',
'/app/js/util.js',
'/app/css/misc.css',
'/app/css/player.css',
'/app/logo.png',
'https://cdn.jsdelivr.net/npm/matroska-subtitles@3.0.1/dist/matroska-subtitles.min.js',
'https://cdn.jsdelivr.net/npm/halfmoon@1.1.0/css/halfmoon-variables.min.css',
'https://cdn.jsdelivr.net/gh/halfmoonui/halfmoon@1.1.0/js/halfmoon.min.js',
'https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js',
'https://fonts.googleapis.com/icon?family=Material+Icons'
]
self.addEventListener('install', event => {
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
);
self.skipWaiting();
});
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache)
})
)
self.skipWaiting()
})
self.addEventListener('activate', event => {
const cacheWhitelist = [staticCacheName]
const cacheWhitelist = [staticCacheName];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName)
}
})
);
return self.clients.claim();
});
)
})
)
return self.clients.claim()
})
// self.addEventListener('fetch', event => {
// event.respondWith(
@ -66,4 +65,4 @@ self.addEventListener('activate', event => {
// return caches.match('index.html');
// })
// );
// });
// });