test-req / server.ts
coyotte508
πŸ”Š show error message
131324f
import Koa from "koa";
import bodyParser from "koa-bodyparser";
const app = new Koa();
app.use(bodyParser());
app.use(async (ctx) => {
if (ctx.request.method === "POST") {
const { cookie } = ctx.request.body as { cookie: string };
ctx.set("Set-Cookie", [
`cookie-none=${cookie.replace(/[^A-Za-z0-9]+/g, '-')}; SameSite=None; Secure`,
`cookie-lax=${cookie.replace(/[^A-Za-z0-9]+/g, '-')}; SameSite=Lax; Secure`,
]);
ctx.redirect( "/");
} else {
ctx.body = `<html>
<body>
<pre>${JSON.stringify(Object.fromEntries(Object.entries(ctx.request.headers)), null, 2)}</pre>
<p>Cookie SameSite=None: ${ctx.cookies.get("cookie-none")?.replace(/</g, '$lt;')}</p>
<p>Cookie SameSite=Lax: ${ctx.cookies.get("cookie-lax")?.replace(/</g, '$lt;')}</p>
<form method="POST">
<label>Cookie value<br>
<input type="text" name="cookie" />
</label>
<button>Send</button>
</form>
<p>Browser supports storage access API: <span id="storage-access-api"></span></p>
<p>Page can store cookies: <span id="page-can-store-cookies"></span></p>
<button id="check-storage-access" type="button">Re-check storage access</button>
<button id="request-storage-access" style="display: none;" type="button">Request storage access</button>
<p style="color: red" id="error-p"></p>
</body>
<script>
const storageAccess = document.getElementById("storage-access-api");
const pageCanStoreCookies = document.getElementById("page-can-store-cookies");
const requestStorageAccess = document.getElementById("request-storage-access");
const checkStorageAccessBtn = document.getElementById("check-storage-access");
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
function checkStorageAccess() {
if ("hasStorageAccess" in document) {
storageAccess.innerText = "Yes";
document.hasStorageAccess().then((hasAccess) => {
console.log("has access", hasAccess);
pageCanStoreCookies.innerText = hasAccess ? "Yes" : (isFirefox ? "No, on firefox" : "No");
if (!hasAccess) {
requestStorageAccess.style.display = "block";
}
});
} else {
storageAccess.innerText = "No";
}
}
checkStorageAccess();
requestStorageAccess.addEventListener("click", () => {
document.requestStorageAccess().then(() => {
pageCanStoreCookies.innerText = "Yes";
requestStorageAccess.style.display = "none";
}).catch((e) => {
console.error(e);
document.getElementById("error-p").innerText = e.message;
})
});
checkStorageAccessBtn.addEventListener("click", checkStorageAccess);
</script>
</html>`;
}
});
app.listen(7860);