Spaces:
Sleeping
Sleeping
| # app.py | |
| import streamlit as st | |
| def escape_for_template(text: str) -> str: | |
| """ | |
| Escape backticks and `${…}` so the content | |
| bisa dimasukkan mentah ke dalam JS template literal. | |
| """ | |
| # escape backslash dulu agar hasil replace nggak kacau | |
| text = text.replace("\\", "\\\\") | |
| # escape backtick | |
| text = text.replace("`", "\\`") | |
| # escape ${ so JS gak nge-evaluate sebagai placeholder | |
| text = text.replace("${", "\\${") | |
| return text | |
| st.set_page_config(page_title="CF Worker Escaper") | |
| st.title("🔧 Cloudflare Worker Escaper") | |
| st.markdown( | |
| """ | |
| Upload **index.html** dan **script.js**, | |
| nanti akan keluar: | |
| ```js | |
| const html = `...escaped-html...`; | |
| const script = `...escaped-js...`; | |
| ``` | |
| siap copy–paste ke `worker.js`. | |
| """ | |
| ) | |
| uploaded_html = st.file_uploader("📄 Upload index.html", type=["html","htm"]) | |
| uploaded_js = st.file_uploader("📄 Upload script.js", type=["js"]) | |
| if uploaded_html and uploaded_js: | |
| try: | |
| html_text = uploaded_html.read().decode("utf-8") | |
| js_text = uploaded_js.read().decode("utf-8") | |
| except UnicodeDecodeError: | |
| st.error("Gagal decode file. Pastikan UTF-8.") | |
| st.stop() | |
| esc_html = escape_for_template(html_text) | |
| esc_js = escape_for_template(js_text) | |
| st.subheader("▶️ Generated Constants") | |
| const_block = ( | |
| f"const html = `{esc_html}`;\n\n" | |
| f"const script = `{esc_js}`;\n" | |
| ) | |
| st.code(const_block, language="javascript") | |
| st.download_button( | |
| "💾 Download as escaped_constants.js", | |
| const_block, | |
| file_name="escaped_constants.js", | |
| mime="application/javascript" | |
| ) | |