Skip to content

Instantly share code, notes, and snippets.

@toolmanxyz
Last active Nov 13, 2020
Embed
What would you like to do?
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
// 檢查網址後面有沒有東西
const url = new URL(request.url)
if(url.pathname.slice(1) == '')
return new Response(htmls, {status: 200, headers: { 'Content-Type': 'text/html' }})
// 到 Google sheet 找對應的原網址
const u = 'https://script.google.com/macros/s/AKfycby3mgGuiof9uEpVJsTsAkpkg3QmyNAulXZXJsvo4E3YQaxLIbg/exec?query='+url.pathname.slice(1);
const init = {
headers: {
"content-type": "text/html;charset=UTF-8",
},
}
const response = await fetch(u, init);
const results = await response.text()
// 判斷有沒有找到網址
if(results != "")
return Response.redirect(results, 301);
else
return Response.redirect('https://csie.one/', 301);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment