Jimmyzheng-10 commited on
Commit
2ad4034
·
1 Parent(s): d8b7150

Enhanced HTML cleaning to remove iframeResizer scripts and fix console errors

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -70,10 +70,10 @@ def render_preview(code: str, width: int, height: int, scale: float) -> str:
70
  # Clean up the HTML code to remove problematic script and CSS references
71
  soup = BeautifulSoup(code, 'html.parser')
72
 
73
- # Remove any script tags that reference local assets
74
  for script in soup.find_all('script'):
75
  src = script.get('src', '')
76
- if src and ('assets/' in src or 'index-' in src):
77
  script.decompose()
78
 
79
  # Remove any link tags that reference local CSS assets
@@ -82,6 +82,11 @@ def render_preview(code: str, width: int, height: int, scale: float) -> str:
82
  if href and ('assets/' in href or 'index-' in href):
83
  link.decompose()
84
 
 
 
 
 
 
85
  # Get the cleaned HTML
86
  cleaned_code = str(soup)
87
  safe_code = html.escape(cleaned_code).replace("'", "'")
@@ -153,7 +158,7 @@ def process_and_generate(image_np, image_path_from_state, sidebar_prompt, header
153
  # Clean up problematic script and CSS references
154
  for script in soup.find_all('script'):
155
  src = script.get('src', '')
156
- if src and ('assets/' in src or 'index-' in src):
157
  print(f"Removing problematic script: {src}")
158
  script.decompose()
159
 
@@ -163,8 +168,14 @@ def process_and_generate(image_np, image_path_from_state, sidebar_prompt, header
163
  print(f"Removing problematic CSS link: {href}")
164
  link.decompose()
165
 
 
 
 
 
 
 
166
  for img in soup.find_all('img'):
167
- if img.get('src') and not img['src'].startswith(('http', 'dbata:')):
168
  original_src = img['src']
169
  print(f"Processing image: {original_src}")
170
 
@@ -298,4 +309,10 @@ for path in allowed_paths:
298
  print(f" - {path}")
299
 
300
  if __name__ == "__main__":
301
- demo.launch(allowed_paths=allowed_paths)
 
 
 
 
 
 
 
70
  # Clean up the HTML code to remove problematic script and CSS references
71
  soup = BeautifulSoup(code, 'html.parser')
72
 
73
+ # Remove any script tags that reference local assets or iframeResizer
74
  for script in soup.find_all('script'):
75
  src = script.get('src', '')
76
+ if src and ('assets/' in src or 'index-' in src or 'iframeResizer' in src):
77
  script.decompose()
78
 
79
  # Remove any link tags that reference local CSS assets
 
82
  if href and ('assets/' in href or 'index-' in href):
83
  link.decompose()
84
 
85
+ # Also remove any inline scripts that might contain problematic content
86
+ for script in soup.find_all('script'):
87
+ if script.string and ('assets/' in script.string or 'index-' in script.string):
88
+ script.decompose()
89
+
90
  # Get the cleaned HTML
91
  cleaned_code = str(soup)
92
  safe_code = html.escape(cleaned_code).replace("'", "'")
 
158
  # Clean up problematic script and CSS references
159
  for script in soup.find_all('script'):
160
  src = script.get('src', '')
161
+ if src and ('assets/' in src or 'index-' in src or 'iframeResizer' in src):
162
  print(f"Removing problematic script: {src}")
163
  script.decompose()
164
 
 
168
  print(f"Removing problematic CSS link: {href}")
169
  link.decompose()
170
 
171
+ # Also remove any inline scripts that might contain problematic content
172
+ for script in soup.find_all('script'):
173
+ if script.string and ('assets/' in script.string or 'index-' in script.string):
174
+ print(f"Removing problematic inline script")
175
+ script.decompose()
176
+
177
  for img in soup.find_all('img'):
178
+ if img.get('src') and not img['src'].startswith(('http', 'data:')):
179
  original_src = img['src']
180
  print(f"Processing image: {original_src}")
181
 
 
309
  print(f" - {path}")
310
 
311
  if __name__ == "__main__":
312
+ demo.launch(
313
+ allowed_paths=allowed_paths,
314
+ show_error=True,
315
+ quiet=False,
316
+ favicon_path=None,
317
+ prevent_thread_lock=True
318
+ )