Skip to content

Instantly share code, notes, and snippets.

@ericlewis
Created December 14, 2020 17:17
Show Gist options
  • Save ericlewis/0e1b1b61a4c1eb4f6c998da56923efdc to your computer and use it in GitHub Desktop.
Save ericlewis/0e1b1b61a4c1eb4f6c998da56923efdc to your computer and use it in GitHub Desktop.
Passthrough script for dark mode
/**
* An object with different URLs to fetch
* @param {Object} ORIGINS
*/
const ORIGINS = {
"darknews.ericlewis.workers.dev": "news.ycombinator.com"
}
async function handleRequest(request) {
const url = new URL(request.url)
// Check if incoming hostname is a key in the ORIGINS object
if (url.hostname in ORIGINS) {
const target = ORIGINS[url.hostname]
url.hostname = target
// If it is, proxy request to that third party origin
const t = await fetch(url.toString(), request);
const tt = await t.text();
const customScripts = `<style type="text/css">
body {
background-color: #232834;
}
.fatitem textarea {
background-color: #232834;
color: #fafafa;
}
#hnmain {
background-color: #1F2430;
}
a:link.storylink,
#hnmain>tbody>tr:first-child>td a,
.commtext,
.commtext a:link,
td {
color: #fafafa;
}
#hnmain>tbody>tr:first-child>td {
background-color: #4a3226;
}
a:link,
.sitebit,
.subtext,
.sitestr,
.subtext a:link,
.rank,
a.morelink,
.yclinks a,
.comhead a:link,
.comhead,
.hnmore a:link,
.reply a:link {
color: #8c96ac;
}
.pagetop {
color: #795848;
}
a:visited {
color: #979cf4;
}
p,
.commtext.c00 {
color: #8c96ac;
}
.votearrow {
overflow: visible;
position: relative;
}
.votearrow::before {
content: "";
width: 0;
height: 0;
left: 1px;
top: 3px;
display: block;
position: absolute;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-bottom: 7px solid #bebfd1;
}</style></body>`
const ttt = tt.replace(/<\/body>/, customScripts).replace("y18.gif", "https://news.ycombinator.com/y18.gif")
return new Response(ttt, {
headers: t.headers
})
}
// Otherwise, process request as normal
return await fetch(request)
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
@ericlewis
Copy link
Author

This uses a slightly modified version of the CSS posted on this page: https://news.ycombinator.com/item?id=23197966. It mostly works and this was just quickly thrown together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment