Skip to content

Commit 61e18de

Browse files
committed
feat: submitted URL sniffing
1 parent 4a812e2 commit 61e18de

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/add-api.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<script type="text/javascript">
2020
var form = document.getElementById('add_api_form');
2121
form.onsubmit = addApi;
22-
function addApi(e) {
22+
async function addApi(e) {
2323
e.preventDefault();
2424

2525
try {
@@ -41,6 +41,22 @@
4141
alert(ex.message);
4242
return false;
4343
}
44+
try {
45+
const res = await fetch(`https://cors.redoc.ly/${form.url.value}`,
46+
{method:'head'});
47+
const ct = res.headers.get('content-type');
48+
if (res.status >= 400) {
49+
alert(`Error ${res.status} accessing that URL`);
50+
return false;
51+
}
52+
if (ct && ct.startsWith('text/html')) {
53+
alert('That looks like a web-page, not a machine-readable API definition');
54+
return(false);
55+
}
56+
}
57+
catch (ex) {
58+
console.log(ex.message);
59+
}
4460

4561
var details = {
4662
format: form['spec-format'].value,

0 commit comments

Comments
 (0)