Skip to content

nbitzz/about.xkcd.uk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation


xkcd.uk

Hi, this is the about page for xkcd.uk, a simple xkcd URL shortener / lengthener running off CF workers. When inputting a number, you will be redirected to xkcd.com/number. Otherwise, xkcd.uk conducts a search on DuckDuckGo: \site:xkcd.com string, and follows the redirect, leading you to (hopefully) that comic.

import { load } from "cheerio";

const ddg_parsing_regex = /.*\; url=(.*)/
const xkcd_number_check = /^\/(\d+)$/

export default {
    async fetch(request, env, ctx) {
        const url = new URL(request.url);

        if (xkcd_number_check.test(url.pathname)) 
            // Hey, they're using a number!
            // Let's redirect them to xkcd.com/$1.
            return Response.redirect(`https://xkcd.com/${xkcd_number_check.exec(url.pathname)?.[1]}`)
        // Oh, they're not using a number...
        // Let's conduct a search, then.
        else {
            let response = await fetch("https://duckduckgo.com/?q=" + encodeURIComponent(`\\site:xkcd.com ${url.pathname.slice(1)}`));
            let html = await response.text()
            let $ = load(html)

            let content = $(`meta[http-equiv=refresh]`).get(0)?.attribs.content
            let ddg_redirect_pathname = content?.match(ddg_parsing_regex)?.[1]
            let ddg_url = new URL(`https://duckduckgo.com${ddg_redirect_pathname}`).searchParams.get("uddg")
            
            if (content && ddg_redirect_pathname && ddg_url) {
                return Response.redirect(ddg_url)
            }
            else return new Response(html, {headers: response.headers})
        }
    },
};

^ this is the worker that powers xkcd.uk ^

For examples, try xkcd.uk/standards, or xkcd.uk/927.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published