Commits

Rune Halvorsen  committed 9039914

initial

  • Participants

Comments (0)

Files changed (3)

File csspretty.py

+import cssutils
+
+_preamble = """/**
+ * This file was intercepted by mitmproxy and modified by csspretty.py
+ */
+"""
+
+def _istype(flow, needle):
+    ctype = "".join(flow.response.headers["content-type"]).lower()
+    return needle in ctype
+
+def response(context, flow):
+    if _istype(flow, "css"):
+        flow.response.decode()
+        cssutils.log.enabled = False # not great, but emitting stuff to stderr without checking it doesn't help
+        flow.response.content = _preamble + cssutils.parseString(flow.response.content).cssText
+from subprocess import Popen, PIPE
+
+_cmd = ["uglifyjs", "-b", "-nm", "-nmf", "-nc", "-ns", "--no-seqs", "--no-dead-code"]
+
+_preamble = """
+/**
+ * This file was intercepted by mitmproxy and modified by jspretty.
+ * The following command was called on it:
+ * %s
+ */
+""" % " ".join(_cmd)
+
+
+def _prettify(js):
+    proc = Popen(_cmd, stdin=PIPE, stdout=PIPE)
+    stdout, stderr = proc.communicate(js)
+    return stdout
+
+def _istype(flow, needle):
+    ctype = "".join(flow.response.headers["content-type"]).lower()
+    return needle in ctype
+
+def response(context, flow):
+    if _istype(flow, "javascript"):
+        flow.response.decode()
+        flow.response.content = _preamble + _prettify(flow.response.content)
+

File vendorprefix.py

+import cssutils
+
+_preamble = """/**
+ * This file was intercepted by mitmproxy and modified by vendorprefix.py
+ */
+"""
+
+def _istype(flow, needle):
+    ctype = "".join(flow.response.headers["content-type"]).lower()
+    return needle in ctype
+
+def _variants(propname):
+    unprefixed = propname.split("-", 2)[2]
+    return set("-%s-%s" % (e, unprefixed) for e in ["o", "moz", "ms", "webkit"])
+
+def _prefixed(name):
+    return name.startswith(("-webkit-", "-o-", "-moz-", "-ms-"))
+
+def _addprefixes(src):
+    cssutils.log.enabled = False # not great, but emitting stuff to stderr without checking it doesn't help
+    sheet = cssutils.parseString(src)
+    for rule in sheet:
+        seen = []
+        if rule.type == rule.STYLE_RULE:
+            for prop in rule.style:
+                if _prefixed(prop.name):
+                    seen.append(prop)
+
+        if seen:
+            # a bit hacky.. couldn't find alternative
+            rule.style.cssText = rule.style.cssText + ";/* The following properties added by mitmproxy+vendorprefix.py */"
+            for prop in seen:
+                for variant in _variants(prop.name):
+                    rule.style.setProperty(variant, prop.value, prop.priority)
+
+    return sheet.cssText
+
+
+def response(context, flow):
+    if _istype(flow, "css"):
+        flow.response.decode()
+        flow.response.content = _preamble + _addprefixes(flow.response.content)
+
+
+css = """
+.things {
+    color: red;
+    -o-gradient: unicorn;
+    border: solid gold thick;
+    /* lol. wut. */
+    -webkit-border-radius: 3px;
+    font-family: san-serif;
+}
+
+"""
+
+
+_addprefixes(css)