metube/ui/src/custom-service-worker.js
2023-12-30 04:15:44 +01:00

38 lines
1.1 KiB
JavaScript

const URL_PATTERN =
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi;
self.addEventListener("fetch", (event) => {
if (event.request.method === "GET") {
const url = new URL(event.request.url);
if (url.pathname.endsWith("/share-target")) {
const urlRegExp = new RegExp(URL_PATTERN);
const sharedText = url.searchParams.get("text");
const matches = [...sharedText.matchAll(urlRegExp)].map((m) => m[0]);
event.respondWith(
(async () => {
await Promise.all(
matches.map((url) => {
return fetch("/add", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
url,
quality: "best",
format: "any",
auto_start: true,
}),
});
})
);
return Response.redirect("/", 303);
})()
);
}
}
});
importScripts("./ngsw-worker.js");