generated from innei-template/vite-react-tailwind-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug_proxy.html
91 lines (74 loc) · 2.58 KB
/
debug_proxy.html
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
80
81
82
83
84
85
86
87
88
89
90
91
<!doctype html>
<html lang="en">
<head>
<title>Debug Proxy</title>
<script type="module">
globalThis['__DEBUG_PROXY__'] = true
const searchParams = new URLSearchParams(window.location.search)
const debugHost = searchParams.get('debug-host')
const resetSessionStorage = () => {
sessionStorage.removeItem('debug-host')
}
const resetParams = searchParams.get('reset')
if (resetParams) {
resetSessionStorage()
}
const debugHostInSessionStorage = sessionStorage.getItem('debug-host')
const host =
debugHost || debugHostInSessionStorage || 'http://localhost:7123'
if (debugHost) {
sessionStorage.setItem('debug-host', debugHost)
}
const createRefreshRuntimeScript = `
import RefreshRuntime from "${host}/@react-refresh";
RefreshRuntime.injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;
window.__vite_plugin_react_preamble_installed__ = true;
`
const $script = document.createElement('script')
$script.innerHTML = createRefreshRuntimeScript
$script.type = 'module'
document.head.append($script)
fetch(`${host}`)
.then((res) => res.text())
.then((html) => {
const parser = new DOMParser()
const doc = parser.parseFromString(html, 'text/html')
const scripts = doc.querySelectorAll('script')
scripts.forEach((script) => {
script.remove()
})
// header meta
const $meta = doc.head.querySelectorAll('meta')
$meta.forEach((meta) => {
document.head.append(meta)
})
const $style = doc.head.querySelectorAll('style')
$style.forEach((style) => {
document.head.append(style)
})
document.body.innerHTML = doc.body.innerHTML
scripts.forEach((script) => {
const $script = document.createElement('script')
$script.type = 'module'
$script.crossOrigin = script.crossOrigin
if (script.src) {
$script.src = new URL(
script.src.startsWith('http')
? new URL(script.src).pathname
: script.src,
host,
).toString()
} else if (script.innerHTML) {
$script.innerHTML = script.innerHTML
} else {
return
}
document.body.append($script)
})
})
</script>
</head>
<body></body>
</html>