Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,26 @@
|
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
"""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
st.set_page_config(page_title="CF Worker Escaper")
|
| 10 |
st.title("🔧 Cloudflare Worker Escaper")
|
| 11 |
|
| 12 |
st.markdown(
|
| 13 |
"""
|
| 14 |
-
Upload
|
| 15 |
-
|
| 16 |
```js
|
| 17 |
const html = `...escaped-html...`;
|
| 18 |
const script = `...escaped-js...`;
|
|
@@ -29,21 +38,21 @@ if uploaded_html and uploaded_js:
|
|
| 29 |
html_text = uploaded_html.read().decode("utf-8")
|
| 30 |
js_text = uploaded_js.read().decode("utf-8")
|
| 31 |
except UnicodeDecodeError:
|
| 32 |
-
st.error("
|
| 33 |
st.stop()
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
st.subheader("▶️ Generated Constants")
|
| 39 |
const_block = (
|
| 40 |
-
f"const html = `{
|
| 41 |
-
f"const script = `{
|
| 42 |
)
|
| 43 |
st.code(const_block, language="javascript")
|
| 44 |
|
| 45 |
st.download_button(
|
| 46 |
-
"💾 Download as
|
| 47 |
const_block,
|
| 48 |
file_name="escaped_constants.js",
|
| 49 |
mime="application/javascript"
|
|
|
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
|
| 5 |
+
def escape_for_template(text: str) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Escape backticks and `${…}` so the content
|
| 8 |
+
bisa dimasukkan mentah ke dalam JS template literal.
|
| 9 |
+
"""
|
| 10 |
+
# escape backslash dulu agar hasil replace nggak kacau
|
| 11 |
+
text = text.replace("\\", "\\\\")
|
| 12 |
+
# escape backtick
|
| 13 |
+
text = text.replace("`", "\\`")
|
| 14 |
+
# escape ${ so JS gak nge-evaluate sebagai placeholder
|
| 15 |
+
text = text.replace("${", "\\${")
|
| 16 |
+
return text
|
| 17 |
|
| 18 |
st.set_page_config(page_title="CF Worker Escaper")
|
| 19 |
st.title("🔧 Cloudflare Worker Escaper")
|
| 20 |
|
| 21 |
st.markdown(
|
| 22 |
"""
|
| 23 |
+
Upload **index.html** dan **script.js**,
|
| 24 |
+
nanti akan keluar:
|
| 25 |
```js
|
| 26 |
const html = `...escaped-html...`;
|
| 27 |
const script = `...escaped-js...`;
|
|
|
|
| 38 |
html_text = uploaded_html.read().decode("utf-8")
|
| 39 |
js_text = uploaded_js.read().decode("utf-8")
|
| 40 |
except UnicodeDecodeError:
|
| 41 |
+
st.error("Gagal decode file. Pastikan UTF-8.")
|
| 42 |
st.stop()
|
| 43 |
|
| 44 |
+
esc_html = escape_for_template(html_text)
|
| 45 |
+
esc_js = escape_for_template(js_text)
|
| 46 |
|
| 47 |
st.subheader("▶️ Generated Constants")
|
| 48 |
const_block = (
|
| 49 |
+
f"const html = `{esc_html}`;\n\n"
|
| 50 |
+
f"const script = `{esc_js}`;\n"
|
| 51 |
)
|
| 52 |
st.code(const_block, language="javascript")
|
| 53 |
|
| 54 |
st.download_button(
|
| 55 |
+
"💾 Download as escaped_constants.js",
|
| 56 |
const_block,
|
| 57 |
file_name="escaped_constants.js",
|
| 58 |
mime="application/javascript"
|