szili2011 commited on
Commit
3757edd
1 Parent(s): 9b0bf69

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import json
4
+ import zipfile
5
+
6
+ def generate_extension(extension_name, enable_icon, enable_popup, popup_content, background_script):
7
+ # Create directories for the extension
8
+ os.makedirs(extension_name, exist_ok=True)
9
+ os.makedirs(os.path.join(extension_name, "icons"), exist_ok=True)
10
+ os.makedirs(os.path.join(extension_name, "popup"), exist_ok=True)
11
+
12
+ # Generate manifest.json
13
+ manifest_content = {
14
+ "manifest_version": 3,
15
+ "name": extension_name,
16
+ "version": "1.0",
17
+ "action": {
18
+ "default_popup": "popup/popup.html" if enable_popup else None,
19
+ "default_icon": "icons/icon.png" if enable_icon else None,
20
+ },
21
+ "background": {
22
+ "service_worker": "background.js"
23
+ },
24
+ "content_scripts": [
25
+ {
26
+ "matches": ["<all_urls>"],
27
+ "js": ["content.js"]
28
+ }
29
+ ]
30
+ }
31
+
32
+ with open(os.path.join(extension_name, "manifest.json"), "w") as f:
33
+ json.dump(manifest_content, f, indent=4)
34
+
35
+ # Create content.js with a placeholder script
36
+ with open(os.path.join(extension_name, "content.js"), "w") as f:
37
+ f.write("// Content script logic here\n")
38
+
39
+ # Create popup.html and popup.js if enabled
40
+ if enable_popup:
41
+ with open(os.path.join(extension_name, "popup", "popup.html"), "w") as f:
42
+ f.write(popup_content)
43
+
44
+ # Create background.js
45
+ with open(os.path.join(extension_name, "background.js"), "w") as f:
46
+ f.write(background_script)
47
+
48
+ # Create a ZIP file of the generated extension
49
+ zip_filename = f"{extension_name}.zip"
50
+ with zipfile.ZipFile(zip_filename, 'w') as zipf:
51
+ for root, dirs, files in os.walk(extension_name):
52
+ for file in files:
53
+ zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), extension_name))
54
+
55
+ return zip_filename
56
+
57
+ def blockly_interface(code):
58
+ # Placeholder for processing the Blockly code to generate the JavaScript
59
+ return f"Generated code from Blockly:\n{code}"
60
+
61
+ with gr.Blocks() as app:
62
+ gr.Markdown("### Chrome Extension Generator")
63
+
64
+ with gr.Row():
65
+ extension_name = gr.Textbox(label="Extension Name")
66
+ enable_icon = gr.Checkbox(label="Enable Icon")
67
+ enable_popup = gr.Checkbox(label="Enable Popup")
68
+
69
+ popup_content = gr.Textbox(label="Popup Content")
70
+ background_script = gr.Textbox(label="Background Script")
71
+
72
+ generate_btn = gr.Button("Generate Extension")
73
+
74
+ generated_zip = gr.File(label="Download Generated Extension")
75
+
76
+ generate_btn.click(generate_extension, inputs=[extension_name, enable_icon, enable_popup, popup_content, background_script], outputs=generated_zip)
77
+
78
+ app.launch()