diff --git a/src/code.js b/src/code.js index 124e26f..3b20856 100644 --- a/src/code.js +++ b/src/code.js @@ -21,8 +21,8 @@ export default function code(data) { let url = myDomain.replace('https://', '').replace('http://', ''); if (url.slice(-1) === '/') url = url.slice(0, url.length - 1); - return `/* CONFIGURATION STARTS HERE */ - + return ` /* CONFIGURATION STARTS HERE */ + /* Step 1: enter your domain name like fruitionsite.com */ const MY_DOMAIN = '${url}'; @@ -107,6 +107,7 @@ ${slugs return handleOptions(request); } let url = new URL(request.url); + url.hostname = 'www.notion.so'; if (url.pathname === '/robots.txt') { return new Response('Sitemap: https://' + MY_DOMAIN + '/sitemap.xml'); } @@ -115,17 +116,16 @@ ${slugs response.headers.set('content-type', 'application/xml'); return response; } - let fullPathname = request.url.replace("https://" + MY_DOMAIN, ""); - const notionUrl = 'https://www.notion.so' + fullPathname; let response; if (url.pathname.startsWith('/app') && url.pathname.endsWith('js')) { - response = await fetch(notionUrl); + response = await fetch(url.toString()); let body = await response.text(); response = new Response(body.replace(/www.notion.so/g, MY_DOMAIN).replace(/notion.so/g, MY_DOMAIN), response); response.headers.set('Content-Type', 'application/x-javascript'); + return response; } else if ((url.pathname.startsWith('/api'))) { // Forward API - response = await fetch(notionUrl, { + response = await fetch(url.toString(), { body: request.body, headers: { 'content-type': 'application/json;charset=UTF-8', @@ -135,11 +135,12 @@ ${slugs }); response = new Response(response.body, response); response.headers.set('Access-Control-Allow-Origin', '*'); + return response; } else if (slugs.indexOf(url.pathname.slice(1)) > -1) { const pageId = SLUG_TO_PAGE[url.pathname.slice(1)]; return Response.redirect('https://' + MY_DOMAIN + '/' + pageId, 301); } else { - response = await fetch(notionUrl, { + response = await fetch(url.toString(), { body: request.body, headers: request.headers, method: request.method, @@ -185,7 +186,7 @@ ${slugs if (GOOGLE_FONT !== '') { element.append(\` \`, { - html: true + html: true }); } element.append(\`\`, { html: true }) @@ -212,6 +215,7 @@ ${slugs const PAGE_TO_SLUG = {}; const slugs = []; const pages = []; + const el = document.createElement('div'); let redirected = false; Object.keys(SLUG_TO_PAGE).forEach(slug => { const page = SLUG_TO_PAGE[slug]; @@ -231,6 +235,30 @@ ${slugs history.replaceState(history.state, '', '/' + slug); } } + function onDark() { + el.innerHTML = '
'; + document.body.classList.add('dark'); + __console.environment.ThemeStore.setState({ mode: 'dark' }); + }; + function onLight() { + el.innerHTML = '
'; + document.body.classList.remove('dark'); + __console.environment.ThemeStore.setState({ mode: 'light' }); + } + function toggle() { + if (document.body.classList.contains('dark')) { + onLight(); + } else { + onDark(); + } + } + function addDarkModeButton(device) { + const nav = device === 'web' ? document.querySelector('.notion-topbar').firstChild : document.querySelector('.notion-topbar-mobile'); + el.className = 'toggle-mode'; + el.addEventListener('click', toggle); + nav.appendChild(el); + onLight(); + } const observer = new MutationObserver(function() { if (redirected) return; const nav = document.querySelector('.notion-topbar'); @@ -239,6 +267,7 @@ ${slugs || mobileNav && mobileNav.firstChild) { redirected = true; updateSlug(); + addDarkModeButton(nav ? 'web' : 'mobile'); const onpopstate = window.onpopstate; window.onpopstate = function() { if (slugs.includes(getSlug())) {