-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_html.py
More file actions
74 lines (58 loc) · 2.09 KB
/
patch_html.py
File metadata and controls
74 lines (58 loc) · 2.09 KB
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
import os
import re
def inline_assets():
dist_path = 'dist'
html_path = os.path.join(dist_path, 'index.html')
if not os.path.exists(html_path):
print("Error: index.html not found")
return
with open(html_path, 'r', encoding='utf-8') as f:
html = f.read()
# Find the JS file
assets_path = os.path.join(dist_path, 'assets')
js_file = None
css_file = None
if os.path.exists(assets_path):
for f in os.listdir(assets_path):
if f.endswith('.js') and 'index' in f:
js_file = f
if f.endswith('.css') and 'index' in f:
css_file = f
if not js_file:
print("Error: No JS bundle found")
return
# Read JS
with open(os.path.join(assets_path, js_file), 'r', encoding='utf-8') as f:
js_code = f.read()
# Read CSS
css_code = ""
if css_file:
with open(os.path.join(assets_path, css_file), 'r', encoding='utf-8') as f:
css_code = f.read()
# Create the single-file HTML
html = re.sub(r'<script[^>]*src="[^"]*"[^>]*></script>', '', html)
html = re.sub(r'<link[^>]*rel="stylesheet"[^>]*>', '', html)
html = re.sub(r'<link[^>]*rel="modulepreload"[^>]*>', '', html)
# Inject CSS
if css_code:
html = html.replace('</head>', f'<style>{css_code}</style></head>')
# Inject JS
html = html.replace('</body>', f'<script>{js_code}</script></body>')
# Final cleaning
html = html.replace('type="module"', '')
html = html.replace('crossorigin', '')
# Error Logger with CORRECT ID 'diag'
error_logger = """
<script>
window.onerror = function(msg, url, lineNo) {
var d = document.getElementById('diag');
if(d){ d.style.background='#EF4444'; d.innerText='JS ERROR: '+msg+' L'+lineNo; }
};
</script>
"""
html = html.replace('<head>', f'<head>{error_logger}')
with open(html_path, 'w', encoding='utf-8') as f:
f.write(html)
print(f"Patched index.html with ES5 code and correct diagnostic ID")
if __name__ == "__main__":
inline_assets()