minify css

This commit is contained in:
Roy Olav Purser 2021-06-09 15:01:19 +02:00
parent b721dd451f
commit 1b5249daf3
Signed by: roypur
GPG Key ID: E14D26A036F21656
4 changed files with 38 additions and 25 deletions

View File

@ -43,7 +43,7 @@ else:
except Exception as e: except Exception as e:
print(e) print(e)
else: else:
with open("/app/chromecast.css", "w") as f: with open("/app/setup/chromecast.css", "w") as f:
f.write(resp.text) f.write(resp.text)
with open("/app/version/chromecast.txt", "w") as f: with open("/app/version/chromecast.txt", "w") as f:
f.write(chromecast_version) f.write(chromecast_version)

View File

@ -171,9 +171,7 @@ try:
with open("/app/favicon.png", "rb") as f: with open("/app/favicon.png", "rb") as f:
favicon = f.read() favicon = f.read()
with open("/app/style.css", "r") as f: with open("/app/style.css", "r") as f:
custom_style_raw = bytes(f.read().strip(), "utf-8") custom_style = f.read()
b64 = str(base64.b64encode(custom_style_raw), "ascii")
custom_style = f'data:text/css;charset=utf-8;base64,{b64}'
except Exception as e: except Exception as e:
logger.info(e) logger.info(e)
@ -210,7 +208,6 @@ class MainHandler(tornado.web.RequestHandler):
data["videojs_version"] = videojs_version data["videojs_version"] = videojs_version
data["chromecast_version"] = chromecast_version data["chromecast_version"] = chromecast_version
data["font_awesome_version"] = font_awesome_version data["font_awesome_version"] = font_awesome_version
data["custom_style"] = custom_style
rendered_html = template_html.generate(data=data, meta=provider_data.meta(), title=provider_data.title()) rendered_html = template_html.generate(data=data, meta=provider_data.meta(), title=provider_data.title())
self.write(rendered_html) self.write(rendered_html)
else: else:
@ -222,18 +219,25 @@ class MainHandler(tornado.web.RequestHandler):
async def head(self): async def head(self):
await self.handle_any(False) await self.handle_any(False)
class FileHandler(tornado.web.RequestHandler): class PlaylistHandler(tornado.web.RequestHandler):
def get(self): def get(self):
self.set_header("Content-Type", "text/plain; charset=utf-8") self.set_header("Content-Type", "text/plain; charset=utf-8")
self.write(playlist) self.write(playlist)
class IconHandler(tornado.web.RequestHandler): class IconHandler(tornado.web.RequestHandler):
def get(self): def get(self):
self.set_header("Content-Type", "image/png") self.set_header("Content-Type", "image/png")
self.write(favicon) self.write(favicon)
class StyleHandler(tornado.web.RequestHandler):
def get(self):
self.set_header("Content-Type", "text/css; charset=utf-8")
self.write(custom_style)
try: try:
handlers = [] handlers = []
handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler)) handlers.append((tornado.routing.PathMatches("/sources.m3u8"), PlaylistHandler))
handlers.append((tornado.routing.PathMatches("/favicon.ico"), IconHandler)) handlers.append((tornado.routing.PathMatches("/favicon.ico"), IconHandler))
handlers.append((tornado.routing.PathMatches("/style.css"), StyleHandler))
handlers.append((tornado.routing.AnyMatches(), MainHandler)) handlers.append((tornado.routing.AnyMatches(), MainHandler))
app_web = tornado.web.Application(handlers) app_web = tornado.web.Application(handlers)
app_web.listen(8080) app_web.listen(8080)

View File

@ -3,24 +3,33 @@
const fs = require("fs"); const fs = require("fs");
const css = require("css"); const css = require("css");
fs.readFile("/app/chromecast.css", "utf-8", (err, data) => { fs.readFile("/app/style.css", "utf-8", (err, a) => {
const ast = css.parse(data); let data = "";
for(const rule of ast.stylesheet.rules) { if(err === null) {
const decls = []; data = a + "\n";
if(Symbol.iterator in Object(rule.declarations)) {
for(const decl of rule.declarations) {
const isNotUrl = !(decl.value.toLowerCase().startsWith("url"));
const isNotContent = !(decl.property.toLowerCase() == "content");
if(isNotContent && isNotUrl) {
decls.push(decl);
}
}
rule.declarations = decls;
}
} }
fs.appendFile("/app/style.css", css.stringify(ast, {"compress": true}), (err) => { fs.readFile("/app/setup/chromecast.css", "utf-8", (err, b) => {
if(err !== null) { if(err === null) {
console.log(err); data += b + "\n";
} }
const ast = css.parse(data);
for(const rule of ast.stylesheet.rules) {
const decls = [];
if(Symbol.iterator in Object(rule.declarations)) {
for(const decl of rule.declarations) {
const isNotUrl = !(decl.value.toLowerCase().startsWith("url"));
const isNotContent = !(decl.property.toLowerCase() == "content");
if(isNotContent && isNotUrl) {
decls.push(decl);
}
}
rule.declarations = decls;
}
}
fs.writeFile("/app/style.css", css.stringify(ast, {"compress": true}), (err) => {
if(err !== null) {
console.log(err);
}
});
}); });
}); });

View File

@ -10,6 +10,6 @@
<script defer src="{{ data["script"] }}"></script> <script defer src="{{ data["script"] }}"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/video.js/{{ data["videojs_version"] }}/video-js.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/video.js/{{ data["videojs_version"] }}/video-js.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/{{ data["font_awesome_version"] }}/css/all.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/{{ data["font_awesome_version"] }}/css/all.min.css">
<link rel="stylesheet" href="{{ data["custom_style"] }}"> <link rel="stylesheet" href="/style.css">
</head> </head>
</html> </html>