Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import json | |
import zipfile | |
def generate_extension(extension_name, enable_icon, enable_popup, popup_content, background_script): | |
# Create directories for the extension | |
os.makedirs(extension_name, exist_ok=True) | |
os.makedirs(os.path.join(extension_name, "icons"), exist_ok=True) | |
os.makedirs(os.path.join(extension_name, "popup"), exist_ok=True) | |
# Generate manifest.json | |
manifest_content = { | |
"manifest_version": 3, | |
"name": extension_name, | |
"version": "1.0", | |
"action": { | |
"default_popup": "popup/popup.html" if enable_popup else None, | |
"default_icon": "icons/icon.png" if enable_icon else None, | |
}, | |
"background": { | |
"service_worker": "background.js" | |
}, | |
"content_scripts": [ | |
{ | |
"matches": ["<all_urls>"], | |
"js": ["content.js"] | |
} | |
] | |
} | |
with open(os.path.join(extension_name, "manifest.json"), "w") as f: | |
json.dump(manifest_content, f, indent=4) | |
# Create content.js with a placeholder script | |
with open(os.path.join(extension_name, "content.js"), "w") as f: | |
f.write("// Content script logic here\n") | |
# Create popup.html and popup.js if enabled | |
if enable_popup: | |
with open(os.path.join(extension_name, "popup", "popup.html"), "w") as f: | |
f.write(popup_content) | |
# Create background.js | |
with open(os.path.join(extension_name, "background.js"), "w") as f: | |
f.write(background_script) | |
# Create a ZIP file of the generated extension | |
zip_filename = f"{extension_name}.zip" | |
with zipfile.ZipFile(zip_filename, 'w') as zipf: | |
for root, dirs, files in os.walk(extension_name): | |
for file in files: | |
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), extension_name)) | |
return zip_filename | |
def blockly_interface(code): | |
# Placeholder for processing the Blockly code to generate the JavaScript | |
return f"Generated code from Blockly:\n{code}" | |
with gr.Blocks() as app: | |
gr.Markdown("### Chrome Extension Generator") | |
with gr.Row(): | |
extension_name = gr.Textbox(label="Extension Name") | |
enable_icon = gr.Checkbox(label="Enable Icon") | |
enable_popup = gr.Checkbox(label="Enable Popup") | |
popup_content = gr.Textbox(label="Popup Content") | |
background_script = gr.Textbox(label="Background Script") | |
generate_btn = gr.Button("Generate Extension") | |
generated_zip = gr.File(label="Download Generated Extension") | |
generate_btn.click(generate_extension, inputs=[extension_name, enable_icon, enable_popup, popup_content, background_script], outputs=generated_zip) | |
app.launch() | |