Skip to content

Commit

Permalink
Add wrong html code case logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dohyeon5626 committed Sep 19, 2024
1 parent 3c269e3 commit fab69ff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
24 changes: 20 additions & 4 deletions show-page-with-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ let showPageWithToken = async (githubUrl, token) => {

// HTML
document.open();
document.write(
(await getContentWithToken(rawUrl, githubUrl)).replace(/<head([^>]*)>/i, `<head$1><base href="${rawUrl}">`)
);
document.write(await getDocumentContentWithToken(rawUrl, githubUrl));
if(!document.head.querySelector('base')) {
let base = document.createElement('base');
base.href = rawUrl;
document.head.appendChild(base);
}
document.close();

// CSS
while(document.querySelector("link[rel=stylesheet]:not([status=clear])")) {
Expand Down Expand Up @@ -51,7 +55,7 @@ let showPageWithToken = async (githubUrl, token) => {
// loadScript()
}

let getContentWithToken = (rawUrl, githubUrl) => {
let getDocumentContentWithToken = (rawUrl, githubUrl) => {
return new Promise((resolve) => {
fetch(rawUrl)
.then(res => {
Expand All @@ -61,4 +65,16 @@ let getContentWithToken = (rawUrl, githubUrl) => {
.then(text => resolve(text))
.catch(() => showPage(githubUrl))
});
}

let getContentWithToken = (rawUrl, githubUrl) => {
return new Promise((resolve) => {
fetch(rawUrl)
.then(res => {
if (!res.ok) throw new Error('400 or 500 에러 발생')
return res.text()
})
.then(text => resolve(text))
.catch(() => resolve(''))
});
}
30 changes: 24 additions & 6 deletions show-page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let showErrorPage = () => {
document.body.innerHTML = `
document.open();
document.write(`
<link rel="stylesheet" href="index.css">
<div id="error">
<h1 id="error-title">404 Not Found</h1>
Expand All @@ -14,7 +15,8 @@ let showErrorPage = () => {
<button id="token-button">Enter</button>
</div>
</div>
`;
`);
document.close();

let token = location.href.split("&")[1];
if (token) document.getElementById("token-input").value = token;
Expand All @@ -32,9 +34,13 @@ let showPage = async (githubUrl) => {

// HTML
document.open();
document.write(
(await getContent(rawUrl)).replace(/<head([^>]*)>/i, `<head$1><base href="${rawUrl}">`)
);
document.write(await getDocumentContent(rawUrl));
if(!document.head.querySelector('base')) {
let base = document.createElement('base');
base.href = rawUrl;
document.head.appendChild(base);
}
document.close();

// CSS
while(document.querySelector("link[rel=stylesheet]:not([status=clear])")) {
Expand Down Expand Up @@ -79,7 +85,7 @@ let showPage = async (githubUrl) => {
// loadScript()
}

let getContent = (rawUrl) => {
let getDocumentContent = (rawUrl) => {
return new Promise((resolve) => {
fetch(rawUrl)
.then(res => {
Expand All @@ -89,4 +95,16 @@ let getContent = (rawUrl) => {
.then(text => resolve(text))
.catch(() => showErrorPage())
});
}

let getContent = (rawUrl) => {
return new Promise((resolve) => {
fetch(rawUrl)
.then(res => {
if (!res.ok) throw new Error('400 or 500 에러 발생')
return res.text()
})
.then(text => resolve(text))
.catch(() => resolve(''))
});
}

0 comments on commit fab69ff

Please sign in to comment.