File size: 8,060 Bytes
bee6636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
export function errorTemplate(trace: string, fetchedURL: string) {
	// turn script into a data URI so we don"t have to escape any HTML values
	const script = `

                errorTrace.value = ${JSON.stringify(trace)};

                fetchedURL.textContent = ${JSON.stringify(fetchedURL)};

                for (const node of document.querySelectorAll("#hostname")) node.textContent = ${JSON.stringify(location.hostname)};

                reload.addEventListener("click", () => location.reload());

                version.textContent = ${JSON.stringify($scramjetVersion)};

                build.textContent = ${JSON.stringify($scramjetVersion)};



                document.getElementById('copy-button').addEventListener('click', async () => {

                    const text = document.getElementById('errorTrace').value;

                    await navigator.clipboard.writeText(text);

                    const btn = document.getElementById('copy-button');

                    btn.textContent = 'Copied!';

                    setTimeout(() => btn.textContent = 'Copy', 2000);

                });

        `;

	return `<!DOCTYPE html>

            <html>

                <head>

                    <meta charset="utf-8" />

                    <title>Scramjet</title>

                    <style>

                    :root {

                        --deep: #080602;

                        --shallow: #181412;

                        --beach: #f1e8e1;

                        --shore: #b1a8a1;

                        --accent: #ffa938;

                        --font-sans: -apple-system, system-ui, BlinkMacSystemFont, sans-serif;

                        --font-monospace: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;

                    }



                    *:not(div,p,span,ul,li,i,span) {

                        background-color: var(--deep);

                        color: var(--beach);

                        font-family: var(--font-sans);

                    }



                    textarea,

                    button {

                        background-color: var(--shallow);

                        border-radius: 0.6em;

                        padding: 0.6em;

                        border: none;

                        appearance: none;

                        font-family: var(--font-sans);

                        color: var(--beach);

                    }



                    button.primary {

                        background-color: var(--accent);

                        color: var(--deep);

                        font-weight: bold;

                    }



                    textarea {

                        resize: none;

                        height: 20em;

                        text-align: left;

                        font-family: var(--font-monospace);

                    }



                    body {

                        width: 100vw;

                        height: 100vh;

                        justify-content: center;

                        align-items: center;

                    }



                    body,

                    html,

                    #inner {

                        display: flex;

                        align-items: center;

                        flex-direction: column;

                        gap: 0.5em;

                        overflow: hidden;

                    }



                    #inner {

                        z-index: 100;

                    }



                    #cover {

                        position: absolute;

                        width: 100%;

                        height: 100%;

                        background-color: color-mix(in srgb, var(--deep) 70%, transparent);

                        z-index: 99;

                    }



                    #info {

                        display: flex;

                        flex-direction: row;

                        align-items: flex-start;

                        gap: 1em;

                    }



                    #version-wrapper {

                        width: auto;

                        text-align: right;

                        position: absolute;

                        top: 0.5rem;

                        right: 0.5rem;

                        font-size: 0.8rem;

                        color: var(--shore)!important;

                        i {

                            background-color: color-mix(in srgb, var(--deep), transparent 50%);

                            border-radius: 9999px;

                            padding: 0.2em 0.5em;

                        }

                        z-index: 101;

                    }



                    #errorTrace-wrapper {

                        position: relative;

                        width: fit-content;

                    }



                    #copy-button {

                        position: absolute;

                        top: 0.5em;

                        right: 0.5em;

                        padding: 0.23em;

                        cursor: pointer;

                        opacity: 0;

                        transition: opacity 0.4s;

                        font-size: 0.9em;

                    }



                    #errorTrace-wrapper:hover #copy-button {

                        opacity: 1;

                    }

                    </style>

                </head>

                <body>

                    <div id="cover"></div>

                    <div id="inner">

                        <h1 id="errorTitle">Uh oh!</h1>

                        <p>There was an error loading <b id="fetchedURL"></b></p>

                        <!-- <p id="errorMessage">Internal Server Error</p> -->



                        <div id="info">

                            <div id="errorTrace-wrapper">

                                <textarea id="errorTrace" cols="40" rows="10" readonly></textarea>

                                <button id="copy-button" class="primary">Copy</button>

                            </div>

                            <div id="troubleshooting">

                                <p>Try:</p>

                                <ul>

                                    <li>Checking your internet connection</li>

                                    <li>Verifying you entered the correct address</li>

                                    <li>Clearing the site data</li>

                                    <li>Contacting <b id="hostname"></b>'s administrator</li>

                                    <li>Verify the server isn't censored</li>

                                </ul>

                                <p>If you're the administrator of <b id="hostname"></b>, try:</p>

                                    <ul>

                                    <li>Restarting your server</li>

                                    <li>Updating Scramjet</li>

                                    <li>Troubleshooting the error on the <a href="https://github.com/MercuryWorkshop/scramjet" target="_blank">GitHub repository</a></li>

                                </ul>

                            </div>

                        </div>

                        <br>

                        <button id="reload" class="primary">Reload</button>

                    </div>

                    <p id="version-wrapper"><i>Scramjet v<span id="version"></span> (build <span id="build"></span>)</i></p>

                    <script src="${"data:application/javascript," + encodeURIComponent(script)}"></script>

                </body>

            </html>

        `;
}

export function renderError(err: unknown, fetchedURL: string) {
	const headers = {
		"content-type": "text/html",
	};
	if (crossOriginIsolated) {
		headers["Cross-Origin-Embedder-Policy"] = "require-corp";
	}

	return new Response(errorTemplate(String(err), fetchedURL), {
		status: 500,
		headers: headers,
	});
}