Sebebeb commited on
Commit
2ff06ca
·
verified ·
1 Parent(s): 88a7efd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -130,10 +130,17 @@ def rewrite_srcset(value, base):
130
  return ", ".join(out)
131
 
132
 
133
- def rewrite_html(raw, doc_url):
134
  soup = BeautifulSoup(raw, "html.parser")
135
  base = doc_url
136
 
 
 
 
 
 
 
 
137
  b = soup.find("base", href=True)
138
  if b:
139
  base = urljoin(doc_url, b["href"])
@@ -154,6 +161,9 @@ def rewrite_html(raw, doc_url):
154
  continue
155
  for attr in ("integrity", "nonce", "ping"):
156
  tag.attrs.pop(attr, None)
 
 
 
157
 
158
  if tag.name == "form" and (tag.get("method") or "get").lower() == "get":
159
  # Browsers drop the query string of a GET form's action, so carry the
@@ -277,7 +287,7 @@ def proxy():
277
  kind = ctype.split(";")[0].strip().lower()
278
 
279
  if kind in ("text/html", "application/xhtml+xml"):
280
- out = rewrite_html(r.content, target)
281
  resp = Response(out, status=r.status_code, content_type="text/html; charset=utf-8")
282
  resp.headers["Cache-Control"] = "no-store"
283
  return resp
 
130
  return ", ".join(out)
131
 
132
 
133
+ def rewrite_html(raw, doc_url, nojs=False):
134
  soup = BeautifulSoup(raw, "html.parser")
135
  base = doc_url
136
 
137
+ if nojs:
138
+ # Serve the server-rendered page only: drop scripts, show <noscript> content.
139
+ for s in soup.find_all("script"):
140
+ s.extract()
141
+ for n in soup.find_all("noscript"):
142
+ n.unwrap()
143
+
144
  b = soup.find("base", href=True)
145
  if b:
146
  base = urljoin(doc_url, b["href"])
 
161
  continue
162
  for attr in ("integrity", "nonce", "ping"):
163
  tag.attrs.pop(attr, None)
164
+ if nojs:
165
+ for attr in [a for a in tag.attrs if a.lower().startswith("on")]:
166
+ del tag.attrs[attr]
167
 
168
  if tag.name == "form" and (tag.get("method") or "get").lower() == "get":
169
  # Browsers drop the query string of a GET form's action, so carry the
 
287
  kind = ctype.split(";")[0].strip().lower()
288
 
289
  if kind in ("text/html", "application/xhtml+xml"):
290
+ out = rewrite_html(r.content, target, request.cookies.get("px_nojs") == "1")
291
  resp = Response(out, status=r.status_code, content_type="text/html; charset=utf-8")
292
  resp.headers["Cache-Control"] = "no-store"
293
  return resp