from competitions.app import app from starlette.middleware.base import BaseHTTPMiddleware from starlette.responses import Response FULL_WIDTH_CSS = """ """ class InjectCssMiddleware(BaseHTTPMiddleware): async def dispatch(self, request, call_next): response = await call_next(request) content_type = response.headers.get("content-type", "") if "text/html" not in content_type: return response body = b"" async for chunk in response.body_iterator: body += chunk html = body.decode("utf-8", errors="ignore") if "" in html and FULL_WIDTH_CSS not in html: html = html.replace("", FULL_WIDTH_CSS + "") headers = dict(response.headers) headers.pop("content-length", None) return Response( content=html, status_code=response.status_code, headers=headers, media_type=response.media_type, ) app.add_middleware(InjectCssMiddleware)