File size: 2,769 Bytes
3757edd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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()