mirror of
https://github.com/notion-enhancer/notion-enhancer.git
synced 2025-04-03 12:19:02 +00:00
chore: notion v4.0.0 compat
use https://www.notion.so/api/__notion-enhancer for internal asset proxy
This commit is contained in:
parent
d5e0810915
commit
7dde3b8b3c
@ -44,8 +44,7 @@ const patches = {
|
||||
));
|
||||
|
||||
// https://github.com/notion-enhancer/notion-enhancer/issues/160:
|
||||
// enable the notion:// protocol, windows-style tab layouts, and
|
||||
// quitting the app when the last window is closed on linux
|
||||
// run the app in windows mode on linux (instead of macos mode)
|
||||
const isWindows =
|
||||
/(?:"win32"===process\.platform(?:(?=,isFullscreen)|(?=&&\w\.BrowserWindow)|(?=&&\(\w\.app\.requestSingleInstanceLock)))/g,
|
||||
isWindowsOrLinux = '["win32","linux"].includes(process.platform)';
|
||||
@ -55,20 +54,6 @@ const patches = {
|
||||
// so the notion-enhancer can be require()-d into it
|
||||
replace(/sandbox:!0/g, `sandbox:!1,nodeIntegration:!0,session:require('electron').session.fromPartition("persist:notion")`);
|
||||
|
||||
// bypass webRequest filter to load enhancer menu
|
||||
replace(/(\w)\.top!==\w\?(\w)\(\{cancel:!0\}\)/, "$1.top!==$1?$2({})");
|
||||
|
||||
// serve enhancer sources over the notion:// protocol
|
||||
const protocolHandler = /try\{const \w=await \w\.assetCache\.handleRequest\(\w\);/,
|
||||
protocolInterceptor = `{const n="notion://www.notion.so/__notion-enhancer/";if(e.url.startsWith(n))return require("electron").net.fetch(\`file://\${require("path").join(__dirname,"..","..","node_modules","notion-enhancer",e.url.slice(n.length))}\`)}`;
|
||||
prepend(protocolHandler, protocolInterceptor);
|
||||
|
||||
// disable csp entirely. risky? yes. necessary? also yes.
|
||||
// potentially could extend default policy instead someday.
|
||||
const headerHandler = /\.onHeadersReceived\(\(\((\w),\w\)=>{/,
|
||||
cspDisabler = `{const responseHeaders=$1.responseHeaders;if(responseHeaders){delete responseHeaders["content-security-policy"];return t({responseHeaders})}}`
|
||||
append(headerHandler, cspDisabler);
|
||||
|
||||
// expose the app's config + cache + preferences to the global namespace
|
||||
// e.g. to enable development mode or check if keep in background is enabled
|
||||
prepend(/\w\.exports=JSON\.parse\('\{"env":"production"/, "globalThis.__notionConfig=");
|
||||
|
@ -21,11 +21,12 @@ const platform = IS_ELECTRON
|
||||
version = IS_ELECTRON
|
||||
? require("notion-enhancer/package.json").version
|
||||
: chrome.runtime.getManifest().version,
|
||||
// forms a url to a notion-enhancer asset or source file
|
||||
// that can be accessed reliably over http
|
||||
enhancerUrl = (target) =>
|
||||
// packages a url to access notion-enhancer assets and sources,
|
||||
// proxies via api in desktop app to bypass service worker cache
|
||||
enhancerUrl = (target = "") =>
|
||||
IS_ELECTRON
|
||||
? `notion://www.notion.so/__notion-enhancer/${target.replace(/^\//, "")}`
|
||||
? "https://www.notion.so/api/__notion-enhancer/" +
|
||||
target.replace(/^\//, "")
|
||||
: chrome.runtime.getURL(target),
|
||||
// require a file from the root of notion's app/ folder,
|
||||
// only available in an electron main process
|
||||
@ -96,10 +97,6 @@ const readFile = (file) => {
|
||||
{ resolve } = require("path");
|
||||
return fsp.readFile(resolve(`${__dirname}/../${file}`), "utf-8");
|
||||
}
|
||||
// prefer using versions of files cached by the app
|
||||
// or routed through the notion-enhancer's url interception
|
||||
const notionProtocol = "notion://www.notion.so/";
|
||||
file = file.replace(/^https:\/\/www\.notion\.so\//, notionProtocol);
|
||||
} else file = file.startsWith("http") ? file : enhancerUrl(file);
|
||||
return fetch(file).then((res) => res.text());
|
||||
},
|
||||
@ -112,8 +109,6 @@ const readFile = (file) => {
|
||||
const { resolve } = require("path");
|
||||
return require(resolve(`${__dirname}/../${file}`));
|
||||
}
|
||||
const notionProtocol = "notion://www.notion.so/";
|
||||
file = file.replace(/^https:\/\/www\.notion\.so\//, notionProtocol);
|
||||
} else file = file.startsWith("http") ? file : enhancerUrl(file);
|
||||
return fetch(file).then((res) => res.json());
|
||||
};
|
||||
|
@ -43,10 +43,11 @@ const shouldLoadThemeOverrides = async (api, db) => {
|
||||
};
|
||||
|
||||
const insertMenu = async (api, db) => {
|
||||
const notionSidebar = `.notion-sidebar-container .notion-sidebar > :nth-child(3) > div > :nth-child(2)`,
|
||||
const notionSettings = `.notion-sidebar-container .notion-sidebar [role="button"]:has(.newSidebarSettings)`,
|
||||
{ html, addMutationListener, removeMutationListener } = api,
|
||||
{ addKeyListener, platform, enhancerUrl, onMessage } = api,
|
||||
menuButtonIconStyle = await db.get("menuButtonIconStyle"),
|
||||
menuButtonLabel = await db.get("menuButtonLabel"),
|
||||
openMenuHotkey = await db.get("openMenuHotkey"),
|
||||
menuPing = {
|
||||
channel: "notion-enhancer",
|
||||
@ -84,18 +85,18 @@ const insertMenu = async (api, db) => {
|
||||
icon="notion-enhancer${menuButtonIconStyle === "Monochrome"
|
||||
? "?mask"
|
||||
: " text-[16px]"}"
|
||||
>notion-enhancer
|
||||
>${menuButtonLabel}
|
||||
<//>`;
|
||||
const appendToDom = () => {
|
||||
if (!document.body.contains($modal)) document.body.append($modal);
|
||||
else if (!document.body.contains($button)) {
|
||||
document.querySelector(notionSidebar)?.append($button);
|
||||
document.querySelector(notionSettings)?.after($button);
|
||||
} else removeMutationListener(appendToDom);
|
||||
};
|
||||
html`<${Tooltip}>
|
||||
<b>Configure the notion-enhancer and its mods</b>
|
||||
<//>`.attach($button, "right");
|
||||
addMutationListener(notionSidebar, appendToDom);
|
||||
addMutationListener(notionSettings, appendToDom);
|
||||
addMutationListener(".notion-app-inner", updateMenuTheme, { subtree: false });
|
||||
appendToDom();
|
||||
|
||||
|
@ -15,10 +15,10 @@ function MenuButton(
|
||||
class: `notion-enhancer--menu-button flex select-none
|
||||
cursor-pointer rounded-[6px] text-[14px] font-medium
|
||||
transition hover:bg-[color:var(--theme--bg-hover)]
|
||||
w-full h-[30px] px-[8px] py-[4px] items-center`,
|
||||
w-full h-[30px] px-[10px] py-[4px] items-center`,
|
||||
});
|
||||
return html`<div ...${props}>
|
||||
<div class="flex items-center justify-center size-[22px] mr-[8px]">
|
||||
<div class="flex items-center justify-center text-[18px] mr-[10px]">
|
||||
<i class="i-${icon}"></i>
|
||||
</div>
|
||||
<div>${children}</div>
|
||||
|
@ -44,10 +44,16 @@
|
||||
"description": "Loads the styling required for a theme to customise Notion's interface. Disabling this may increase client performance, but will also disable all themes.",
|
||||
"values": ["Auto", "Enabled", "Disabled"]
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"key": "menuButtonLabel",
|
||||
"description": "Sets the text to label the notion-enhancer button added to Notion's sidebar with.",
|
||||
"value": "notion-enhancer"
|
||||
},
|
||||
{
|
||||
"type": "select",
|
||||
"key": "menuButtonIconStyle",
|
||||
"description": "Sets whether the notion-enhancer icon added to Notion's sidebar should be coloured or monochrome. The latter style will match the theme's icon colour for users who would like the icon to be less noticeable.",
|
||||
"description": "Sets whether the icon beside the notion-enhancer button added to Notion's sidebar should be coloured or monochrome. The latter style will match the theme's icon colour for users who would like the icon to be less noticeable.",
|
||||
"values": ["Colour", "Monochrome"]
|
||||
},
|
||||
{
|
||||
|
@ -6,6 +6,8 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
const { escape } = require("querystring");
|
||||
|
||||
const IS_ELECTRON = typeof module !== "undefined";
|
||||
|
||||
let __db, __statements, __transactions;
|
||||
@ -113,15 +115,31 @@ const initDatabase = async () => {
|
||||
};
|
||||
|
||||
if (IS_ELECTRON) {
|
||||
const { ipcMain } = require("electron"),
|
||||
{ reloadApp } = globalThis.__enhancerApi;
|
||||
ipcMain.handle("notion-enhancer", ({}, message) => {
|
||||
if (message?.action !== "query-database") return;
|
||||
const { namespace, query, args } = message.data;
|
||||
return queryDatabase(namespace, query, args);
|
||||
});
|
||||
ipcMain.on("notion-enhancer", ({}, message) => {
|
||||
if (message === "reload-app") reloadApp();
|
||||
const { reloadApp, enhancerUrl } = globalThis.__enhancerApi,
|
||||
{ ipcMain, session, app, net } = require("electron");
|
||||
app.on("ready", () => {
|
||||
// proxies notion-enhancer sources over www.notion.so via https
|
||||
const { protocol } = session.fromPartition("persist:notion");
|
||||
protocol.handle("https", (req) => {
|
||||
if (req.url.startsWith(enhancerUrl())) {
|
||||
let url = req.url.slice(enhancerUrl().length);
|
||||
url = `file://${require("path").join(__dirname, url)}`;
|
||||
return net.fetch(url);
|
||||
} else return net.fetch(req);
|
||||
});
|
||||
// webRequest.onHeadersReceived(({ details: { responseHeaders }, callback }) => {
|
||||
// delete responseHeaders["content-security-policy"];
|
||||
// return callback({ responseHeaders });
|
||||
// });
|
||||
|
||||
ipcMain.handle("notion-enhancer", ({}, message) => {
|
||||
if (message?.action !== "query-database") return;
|
||||
const { namespace, query, args } = message.data;
|
||||
return queryDatabase(namespace, query, args);
|
||||
});
|
||||
ipcMain.on("notion-enhancer", ({}, message) => {
|
||||
if (message === "reload-app") reloadApp();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
const notionUrl = "https://www.notion.so/",
|
||||
|
Loading…
Reference in New Issue
Block a user