notion-enhancer/src/init.js

65 lines
2.1 KiB
JavaScript

/**
* notion-enhancer
* (c) 2024 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
* (https://notion-enhancer.github.io/) under the MIT license
*/
"use strict";
globalThis.__enhancerApi ??= {};
const whenReady = new Promise((res, rej) => {
globalThis.__enhancerApi.__isReady = res;
}),
isElectron = () => {
try {
return typeof module !== "undefined";
} catch {}
return false;
};
Object.assign((globalThis.__enhancerApi ??= {}), {
whenReady: (callback) => whenReady.then(callback),
});
if (isElectron()) {
require("./common/system.js");
require("./common/registry.js");
const { enhancerUrl } = globalThis.__enhancerApi,
{ getMods, isEnabled, modDatabase } = globalThis.__enhancerApi;
const mainScript = ".webpack/main/index",
rendererScript = ".webpack/renderer/tab_browser_view/preload";
module.exports = async (target, __exports, __eval) => {
if (target === mainScript) require("./worker.js");
else {
// expose globalThis.__enhancerApi to scripts
const { contextBridge } = require("electron"),
__getEnhancerApi = () => globalThis.__enhancerApi;
contextBridge.exposeInMainWorld("__getEnhancerApi", __getEnhancerApi);
// load clientStyles, clientScripts
document.addEventListener("readystatechange", () => {
if (document.readyState !== "complete") return false;
const $script = document.createElement("script");
$script.type = "module";
$script.src = enhancerUrl("load.mjs");
document.head.append($script);
});
}
// load electronScripts
for (const mod of await getMods()) {
if (!mod.electronScripts || !(await isEnabled(mod.id))) continue;
const db = await modDatabase(mod.id);
for (let [scriptTarget, script] of mod.electronScripts ?? []) {
if (target !== scriptTarget) continue;
script = require(`./${mod._src}/${script}`);
script(globalThis.__enhancerApi, db, __exports, __eval);
}
}
};
} else {
import(chrome.runtime.getURL("/common/system.js")) //
.then(() => import(chrome.runtime.getURL("/load.mjs")));
}