/** * notion-enhancer * (c) 2023 dragonwocky (https://dragonwocky.me/) * (https://notion-enhancer.github.io/) under the MIT license */ const notionSidebar = `.notion-sidebar-container .notion-sidebar > :nth-child(3) > div > :nth-child(2)`; export default async (api, db) => { const { html, platform, version, getMods, isEnabled, enhancerUrl, onMessage, sendMessage, addMutationListener, addKeyListener, initDatabase, } = api, openMenuHotkey = await db.get("openMenuHotkey"), menuButtonIconStyle = await db.get("menuButtonIconStyle"), loadThemeOverrides = await db.get("loadThemeOverrides"), customStyles = (await db.get("customStyles"))?.content; // appearance const enabledThemes = (await getMods("themes")).map((theme) => isEnabled(theme.id) ), forceLoadOverrides = loadThemeOverrides === "Enabled", autoLoadOverrides = loadThemeOverrides === "Auto" && (await Promise.all(enabledThemes)).some((enabled) => enabled); if (forceLoadOverrides || autoLoadOverrides) { document.head.append(html``); } if (customStyles) { const $customStyles = html``; document.head.append($customStyles); } // menu let $menuModal, $menuFrame, _notionTheme; const updateTheme = (force = false) => { const darkMode = document.body.classList.contains("dark"), notionTheme = darkMode ? "dark" : "light"; if (notionTheme !== _notionTheme || force) { _notionTheme = notionTheme; const msg = { namespace: "notion-enhancer", hotkey: openMenuHotkey, theme: notionTheme, icon: menuButtonIconStyle, }; $menuFrame?.contentWindow.postMessage(msg, "*"); } }; const openMenu = () => { updateTheme(true); $menuModal?.setAttribute("open", true); $menuFrame?.contentWindow.focus(); }, closeMenu = () => $menuModal?.removeAttribute("open"); $menuFrame = html``; $menuModal = html`
${$menuFrame}
`; document.body.append($menuModal); const $menuButton = html`
notion-enhancer
`; addMutationListener(notionSidebar, () => { if (document.contains($menuButton)) return; document.querySelector(notionSidebar)?.append($menuButton); }); document.querySelector(notionSidebar)?.append($menuButton); window.addEventListener("focus", () => updateTheme(true)); window.addEventListener("message", (event) => { if (event.data?.namespace !== "notion-enhancer") return; if (event.data?.action === "close-menu") closeMenu(); if (event.data?.action === "open-menu") openMenu(); }); addMutationListener("body", () => { if ($menuModal?.hasAttribute("open")) updateTheme(); }); onMessage("notion-enhancer", (message) => { if (message === "open-menu") openMenu(); }); addKeyListener(openMenuHotkey, (event) => { event.preventDefault(); openMenu(); }); addKeyListener("Escape", () => { if (document.activeElement?.nodeName === "INPUT") return; closeMenu(); }); sendMessage("notion-enhancer", "load-complete"); if ((await initDatabase().get("agreedToTerms")) === version) { // telemetry } };