Skip to content

Instantly share code, notes, and snippets.

@mbround18
Last active June 29, 2021 06:45
Show Gist options
  • Save mbround18/07cc1d102a29c07d4cba58d8ab30c0f8 to your computer and use it in GitHub Desktop.
Save mbround18/07cc1d102a29c07d4cba58d8ab30c0f8 to your computer and use it in GitHub Desktop.
Service worker for injecting styles or scripts: https://dev.to/mbround18/how-to-hijack-your-head-38dn
class ElementHandler {
element(element) {
element.append(`<link
rel="stylesheet"
type="text/css"
data-id="foundry-login"
href="https://cdn.jsdelivr.net/gh/TheEpicSnowWolf/Foundry-VTT-Prettier-Login-Screen@main/foundry_login.css"
>`, {html: true});
console.log("injected");
}
}
async function handleRequest(req) {
const res = await fetch(req)
if (res.url.includes('dnd.example.com/join')) {
return new HTMLRewriter().on("head", new ElementHandler()).transform(res)
}
return res;
}
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment