mirror of
https://github.com/notion-enhancer/notion-enhancer.git
synced 2025-04-08 06:29:03 +00:00
perf(load): import all deps at once + wait for load.mjs completion, fix(electron): use ipc for reloadApp
This commit is contained in:
parent
72332acc58
commit
c19262a4ce
@ -31,10 +31,12 @@ const readFile = (file) => {
|
|||||||
return require(path.resolve(`${__dirname}/../${file}`));
|
return require(path.resolve(`${__dirname}/../${file}`));
|
||||||
},
|
},
|
||||||
reloadApp = () => {
|
reloadApp = () => {
|
||||||
const { app } = require("electron"),
|
const { app, ipcRenderer } = require("electron");
|
||||||
args = process.argv.slice(1).filter((arg) => arg !== "--startup");
|
if (app) {
|
||||||
app.relaunch({ args });
|
const args = process.argv.slice(1).filter((arg) => arg !== "--startup");
|
||||||
app.exit();
|
app.relaunch({ args });
|
||||||
|
app.exit();
|
||||||
|
} else ipcRenderer.send("notion-enhancer", "reload-app");
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendMessage = (channel, message) => {
|
const sendMessage = (channel, message) => {
|
||||||
|
@ -411,8 +411,6 @@ useState(["rerender"], async () => {
|
|||||||
// the notion:// protocol csp bypass allows scripts to
|
// the notion:// protocol csp bypass allows scripts to
|
||||||
// set iframe globals via $iframe.contentWindow
|
// set iframe globals via $iframe.contentWindow
|
||||||
}
|
}
|
||||||
// load stylesheets from enabled themes
|
// load stylesheets and api globals
|
||||||
await import("../../load.mjs");
|
(await import("../../load.mjs")).default.then(render);
|
||||||
// wait for api globals to be available
|
|
||||||
requestIdleCallback(() => render());
|
|
||||||
});
|
});
|
||||||
|
@ -26,6 +26,8 @@ if (isElectron()) {
|
|||||||
} = globalThis.__enhancerApi;
|
} = globalThis.__enhancerApi;
|
||||||
|
|
||||||
module.exports = async (target, __exports, __eval) => {
|
module.exports = async (target, __exports, __eval) => {
|
||||||
|
if (target === "main/main") require("./worker.js");
|
||||||
|
|
||||||
// clientStyles
|
// clientStyles
|
||||||
// clientScripts
|
// clientScripts
|
||||||
if (target === "renderer/preload") {
|
if (target === "renderer/preload") {
|
||||||
|
16
src/load.mjs
16
src/load.mjs
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
(async () => {
|
export default (async () => {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
const { enhancerUrl } = globalThis.__enhancerApi,
|
const { enhancerUrl } = globalThis.__enhancerApi,
|
||||||
isMenu = location.href.startsWith(enhancerUrl("/core/menu/index.html")),
|
isMenu = location.href.startsWith(enhancerUrl("/core/menu/index.html")),
|
||||||
@ -17,13 +17,15 @@
|
|||||||
// avoid repeat logging
|
// avoid repeat logging
|
||||||
if (!isMenu) console.log("notion-enhancer: loading...");
|
if (!isMenu) console.log("notion-enhancer: loading...");
|
||||||
|
|
||||||
await import("./assets/icons.svg.js");
|
await Promise.all([
|
||||||
await import("./vendor/twind.min.js");
|
import("./assets/icons.svg.js"),
|
||||||
await import("./vendor/lucide.min.js");
|
import("./vendor/twind.min.js"),
|
||||||
await import("./vendor/htm.min.js");
|
import("./vendor/lucide.min.js"),
|
||||||
|
import("./vendor/htm.min.js"),
|
||||||
|
import("./api/events.js"),
|
||||||
|
import("./api/mods.js"),
|
||||||
|
]);
|
||||||
await import("./api/interface.js");
|
await import("./api/interface.js");
|
||||||
await import("./api/events.js");
|
|
||||||
await import("./api/mods.js");
|
|
||||||
const { getMods, getProfile } = globalThis.__enhancerApi,
|
const { getMods, getProfile } = globalThis.__enhancerApi,
|
||||||
{ isEnabled, optionDefaults, initDatabase } = globalThis.__enhancerApi;
|
{ isEnabled, optionDefaults, initDatabase } = globalThis.__enhancerApi;
|
||||||
|
|
||||||
|
102
src/worker.js
102
src/worker.js
@ -6,46 +6,70 @@
|
|||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const notionUrl = "https://www.notion.so/",
|
const isElectron = () => {
|
||||||
isNotionTab = (tab) => tab?.url?.startsWith(notionUrl);
|
try {
|
||||||
|
return typeof module !== "undefined";
|
||||||
|
} catch {}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
const tabQueue = new Set(),
|
if (isElectron()) {
|
||||||
openEnhancerMenu = async (tab) => {
|
const { app, ipcMain } = require("electron"),
|
||||||
if (!isNotionTab(tab)) {
|
reloadApp = () => {
|
||||||
|
const args = process.argv.slice(1).filter((arg) => arg !== "--startup");
|
||||||
|
app.relaunch({ args });
|
||||||
|
app.exit();
|
||||||
|
};
|
||||||
|
|
||||||
|
ipcMain.on("notion-enhancer", (event, message) => {
|
||||||
|
if (message === "open-menu") {
|
||||||
|
//
|
||||||
|
} else if (message === "reload-app") {
|
||||||
|
reloadApp();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const notionUrl = "https://www.notion.so/",
|
||||||
|
isNotionTab = (tab) => tab?.url?.startsWith(notionUrl);
|
||||||
|
|
||||||
|
const tabQueue = new Set(),
|
||||||
|
openEnhancerMenu = async (tab) => {
|
||||||
|
if (!isNotionTab(tab)) {
|
||||||
|
const openTabs = await chrome.tabs.query({
|
||||||
|
windowId: chrome.windows.WINDOW_ID_CURRENT,
|
||||||
|
});
|
||||||
|
tab = openTabs.find(isNotionTab);
|
||||||
|
tab ??= await chrome.tabs.create({ url: notionUrl });
|
||||||
|
}
|
||||||
|
chrome.tabs.highlight({ tabs: [tab.index] });
|
||||||
|
if (tab.status === "complete") {
|
||||||
|
chrome.tabs.sendMessage(tab.id, {
|
||||||
|
channel: "notion-enhancer",
|
||||||
|
message: "open-menu",
|
||||||
|
});
|
||||||
|
} else tabQueue.add(tab.id);
|
||||||
|
},
|
||||||
|
reloadNotionTabs = async () => {
|
||||||
const openTabs = await chrome.tabs.query({
|
const openTabs = await chrome.tabs.query({
|
||||||
windowId: chrome.windows.WINDOW_ID_CURRENT,
|
windowId: chrome.windows.WINDOW_ID_CURRENT,
|
||||||
});
|
}),
|
||||||
tab = openTabs.find(isNotionTab);
|
notionTabs = openTabs.filter(isNotionTab);
|
||||||
tab ??= await chrome.tabs.create({ url: notionUrl });
|
notionTabs.forEach((tab) => chrome.tabs.reload(tab.id));
|
||||||
}
|
};
|
||||||
chrome.tabs.highlight({ tabs: [tab.index] });
|
|
||||||
if (tab.status === "complete") {
|
|
||||||
chrome.tabs.sendMessage(tab.id, {
|
|
||||||
channel: "notion-enhancer",
|
|
||||||
message: "open-menu",
|
|
||||||
});
|
|
||||||
} else tabQueue.add(tab.id);
|
|
||||||
},
|
|
||||||
reloadNotionTabs = async () => {
|
|
||||||
const openTabs = await chrome.tabs.query({
|
|
||||||
windowId: chrome.windows.WINDOW_ID_CURRENT,
|
|
||||||
}),
|
|
||||||
notionTabs = openTabs.filter(isNotionTab);
|
|
||||||
notionTabs.forEach((tab) => chrome.tabs.reload(tab.id));
|
|
||||||
};
|
|
||||||
|
|
||||||
chrome.action.onClicked.addListener(openEnhancerMenu);
|
chrome.action.onClicked.addListener(openEnhancerMenu);
|
||||||
chrome.runtime.onMessage.addListener((msg, sender) => {
|
chrome.runtime.onMessage.addListener((msg, sender) => {
|
||||||
if (msg?.channel !== "notion-enhancer") return;
|
if (msg?.channel !== "notion-enhancer") return;
|
||||||
if (sender.tab && msg.message === "load-complete") {
|
if (sender.tab && msg.message === "load-complete") {
|
||||||
if (tabQueue.has(sender.tab.id)) {
|
if (tabQueue.has(sender.tab.id)) {
|
||||||
chrome.tabs.sendMessage(sender.tab.id, {
|
chrome.tabs.sendMessage(sender.tab.id, {
|
||||||
channel: "notion-enhancer",
|
channel: "notion-enhancer",
|
||||||
message: "open-menu",
|
message: "open-menu",
|
||||||
});
|
});
|
||||||
tabQueue.delete(sender.tab.id);
|
tabQueue.delete(sender.tab.id);
|
||||||
|
}
|
||||||
|
} else if (msg.message === "reload-app") {
|
||||||
|
reloadNotionTabs();
|
||||||
}
|
}
|
||||||
} else if (msg.message === "reload-app") {
|
});
|
||||||
reloadNotionTabs();
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
Loading…
Reference in New Issue
Block a user