Last active
June 29, 2021 06:45
-
-
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
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
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