mirror of
https://github.com/notion-enhancer/notion-enhancer.git
synced 2025-04-11 15:59:03 +00:00
???: ...and it doesn't work. i will look at this again later
This commit is contained in:
parent
bf07257ae8
commit
d3840c5922
@ -123,7 +123,7 @@ const initDatabase = (namespace, fallbacks = {}) => {
|
|||||||
namespace,
|
namespace,
|
||||||
fallbacks,
|
fallbacks,
|
||||||
operation: type,
|
operation: type,
|
||||||
...args,
|
args,
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
get: (key) => operation("get", { key }),
|
get: (key) => operation("get", { key }),
|
||||||
|
300
src/worker.js
300
src/worker.js
@ -8,93 +8,19 @@
|
|||||||
|
|
||||||
const IS_ELECTRON = typeof module !== "undefined";
|
const IS_ELECTRON = typeof module !== "undefined";
|
||||||
|
|
||||||
if (IS_ELECTRON) {
|
|
||||||
const { app, ipcMain } = require("electron"),
|
|
||||||
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") {
|
|
||||||
// todo
|
|
||||||
} else if (message === "reload-app") {
|
|
||||||
reloadApp();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
ipcMain.handle("notion-enhancer", (_event, message) => {
|
|
||||||
if (message === "get-user-data-folder") {
|
|
||||||
return app.getPath("userData");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} 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({
|
|
||||||
windowId: chrome.windows.WINDOW_ID_CURRENT,
|
|
||||||
}),
|
|
||||||
notionTabs = openTabs.filter(isNotionTab);
|
|
||||||
notionTabs.forEach((tab) => chrome.tabs.reload(tab.id));
|
|
||||||
};
|
|
||||||
|
|
||||||
// listen for invoke: https://developer.chrome.com/docs/extensions/mv3/messaging/
|
|
||||||
|
|
||||||
chrome.action.onClicked.addListener(openEnhancerMenu);
|
|
||||||
chrome.runtime.onMessage.addListener((msg, sender) => {
|
|
||||||
if (msg?.channel !== "notion-enhancer") return;
|
|
||||||
if (sender.tab && msg.message === "load-complete") {
|
|
||||||
if (tabQueue.has(sender.tab.id)) {
|
|
||||||
chrome.tabs.sendMessage(sender.tab.id, {
|
|
||||||
channel: "notion-enhancer",
|
|
||||||
message: "open-menu",
|
|
||||||
});
|
|
||||||
tabQueue.delete(sender.tab.id);
|
|
||||||
}
|
|
||||||
} else if (msg.message === "reload-app") {
|
|
||||||
reloadNotionTabs();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let __db, __statements, __transactions;
|
let __db, __statements, __transactions;
|
||||||
const initDatabase = (namespace, fallbacks = {}) => {
|
const initDatabase = async () => {
|
||||||
if (Array.isArray(namespace)) namespace = namespace.join("__");
|
if (!IS_ELECTRON) return chrome.storage.local;
|
||||||
namespace = namespace ? namespace + "__" : "";
|
|
||||||
const namespaceify = (key) =>
|
|
||||||
key.startsWith(namespace) ? key : namespace + key;
|
|
||||||
|
|
||||||
// schema:
|
// schema:
|
||||||
// - ("agreedToTerms") -> string: semver
|
// - ("agreedToTerms") -> string: semver
|
||||||
// - ("lastTelemetryPing") -> string: iso
|
// - ("lastTelemetryPing") -> string: iso
|
||||||
// - ("telemetryEnabled") -> boolean
|
// - ("telemetryEnabled") -> boolean
|
||||||
// - ("profileIds") -> $profileId[]
|
// - ("profileIds") -> $profileId[]
|
||||||
// - ("activeProfile") -> $profileId
|
// - ("activeProfile") -> $profileId
|
||||||
// - $profileId: ("profileName") -> string
|
// - $profileId: ("profileName") -> string
|
||||||
// - $profileId__enabledMods: ($modId) -> boolean
|
// - $profileId__enabledMods: ($modId) -> boolean
|
||||||
// - $profileId__$modId: ($optionKey) -> value
|
// - $profileId__$modId: ($optionKey) -> value
|
||||||
|
|
||||||
__db ??= (async () => {
|
|
||||||
if (!IS_ELECTRON) return;
|
|
||||||
|
|
||||||
const table = "kvstore",
|
const table = "kvstore",
|
||||||
{ app } = require("electron"),
|
{ app } = require("electron"),
|
||||||
@ -127,58 +53,156 @@ const initDatabase = (namespace, fallbacks = {}) => {
|
|||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
return db;
|
return db;
|
||||||
})();
|
},
|
||||||
|
executeOperation = async (namespace, fallbacks, operation, args) => {
|
||||||
|
namespace ??= "";
|
||||||
|
if (Array.isArray(namespace)) namespace = namespace.join("__");
|
||||||
|
if (namespace?.length) namespace += "__";
|
||||||
|
const namespaceify = (key) =>
|
||||||
|
key.startsWith(namespace) ? key : namespace + key;
|
||||||
|
|
||||||
return {
|
await (__db ??= initDatabase());
|
||||||
async get(key) {
|
switch (operation) {
|
||||||
await __db;
|
case "get": {
|
||||||
let value;
|
const key = namespaceify(args.key);
|
||||||
const fallback = fallbacks[key];
|
let value;
|
||||||
key = namespaceify(key);
|
if (IS_ELECTRON) {
|
||||||
if (IS_ELECTRON) {
|
try {
|
||||||
try {
|
value = JSON.parse(__statements.select.get(key)?.value);
|
||||||
value = JSON.parse(__statements.select.get(key)?.value);
|
} catch {}
|
||||||
} catch {}
|
} else value = (await chrome.storage.local.get([key]))[key];
|
||||||
} else value = (await chrome.storage.local.get([key]))[key];
|
return value ?? fallbacks[key];
|
||||||
return value ?? fallback;
|
}
|
||||||
},
|
case "set": {
|
||||||
async set(key, value) {
|
const key = namespaceify(args.key),
|
||||||
await __db;
|
value = args.value;
|
||||||
key = namespaceify(key);
|
return IS_ELECTRON
|
||||||
return IS_ELECTRON
|
? // returns true instead of transaction completion data type
|
||||||
? // returns true instead of transaction completion data type
|
(__transactions.set({ [key]: JSON.stringify(value) }), true)
|
||||||
(__transactions.set({ [key]: JSON.stringify(value) }), true)
|
: chrome.storage.local.set({ [key]: value });
|
||||||
: chrome.storage.local.set({ [key]: value });
|
}
|
||||||
},
|
case "remove": {
|
||||||
async remove(keys) {
|
let { keys } = args;
|
||||||
await __db;
|
if (!Array.isArray(args.keys)) keys = [keys];
|
||||||
keys = Array.isArray(keys) ? keys : [keys];
|
keys = keys.map(namespaceify);
|
||||||
keys = keys.map(namespaceify);
|
return IS_ELECTRON
|
||||||
return IS_ELECTRON
|
? (__transactions.remove(keys), true)
|
||||||
? (__transactions.remove(keys), true)
|
: chrome.storage.local.remove(keys);
|
||||||
: chrome.storage.local.remove(keys);
|
}
|
||||||
},
|
|
||||||
async export() {
|
case "export": {
|
||||||
await __db;
|
// returns key/value pairs within scope w/out namespace
|
||||||
// returns key/value pairs within scope w/out namespace
|
// prefix e.g. to streamline importing from one profile and
|
||||||
// prefix e.g. to streamline importing from one profile and
|
// then into another (where a diff. namespace is used)
|
||||||
// then into another (where a diff. namespace is used)
|
let entries = IS_ELECTRON
|
||||||
let entries = IS_ELECTRON
|
? __statements.dump.all().map(({ key, value }) => [key, value])
|
||||||
? __statements.dump.all().map(({ key, value }) => [key, value])
|
: Object.entries(await chrome.storage.local.get());
|
||||||
: Object.entries(await chrome.storage.local.get());
|
entries = entries
|
||||||
entries = entries
|
.filter(([key]) => key.startsWith(namespace))
|
||||||
.filter(([key]) => key.startsWith(namespace))
|
.map(([key, value]) => [key.slice(namespace.length), value]);
|
||||||
.map(([key, value]) => [key.slice(namespace.length), value]);
|
return Object.fromEntries(entries);
|
||||||
return Object.fromEntries(entries);
|
}
|
||||||
},
|
case "import": {
|
||||||
async import(obj) {
|
let entries = Object.entries(args.obj);
|
||||||
await __db;
|
entries = entries.map(([key, value]) => [namespace + key, value]);
|
||||||
let entries = Object.entries(obj);
|
entries = Object.fromEntries(entries);
|
||||||
entries = entries.map(([key, value]) => [namespace + key, value]);
|
return IS_ELECTRON
|
||||||
entries = Object.fromEntries(entries);
|
? (__transactions.set(entries), true)
|
||||||
return IS_ELECTRON
|
: chrome.storage.local.set(entries);
|
||||||
? (__transactions.set(entries), true)
|
}
|
||||||
: chrome.storage.local.set(entries);
|
}
|
||||||
},
|
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
if (IS_ELECTRON) {
|
||||||
|
const { app, ipcMain } = require("electron"),
|
||||||
|
reloadApp = () => {
|
||||||
|
const args = process.argv.slice(1).filter((arg) => arg !== "--startup");
|
||||||
|
app.relaunch({ args });
|
||||||
|
app.exit();
|
||||||
|
};
|
||||||
|
|
||||||
|
ipcMain.handle("notion-enhancer:db", (_event, message) => {
|
||||||
|
return executeOperation(
|
||||||
|
message.namespace,
|
||||||
|
message.fallbacks,
|
||||||
|
message.operation,
|
||||||
|
message.args
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
ipcMain.on("notion-enhancer", (_event, message) => {
|
||||||
|
if (message === "open-menu") {
|
||||||
|
// todo
|
||||||
|
} 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({
|
||||||
|
windowId: chrome.windows.WINDOW_ID_CURRENT,
|
||||||
|
}),
|
||||||
|
notionTabs = openTabs.filter(isNotionTab);
|
||||||
|
notionTabs.forEach((tab) => chrome.tabs.reload(tab.id));
|
||||||
|
};
|
||||||
|
|
||||||
|
// listen for invoke: https://developer.chrome.com/docs/extensions/mv3/messaging/
|
||||||
|
ipcMain.handle("notion-enhancer:db", (_event, message) => {
|
||||||
|
return executeOperation(
|
||||||
|
message.namespace,
|
||||||
|
message.fallbacks,
|
||||||
|
message.operation,
|
||||||
|
message.args
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
chrome.action.onClicked.addListener(openEnhancerMenu);
|
||||||
|
chrome.runtime.onConnect.addListener((port) => {
|
||||||
|
port.onMessage.addListener((msg, sender) => {
|
||||||
|
if (msg?.channel === "notion-enhancer:db") {
|
||||||
|
const { invocation } = msg,
|
||||||
|
res = executeOperation(
|
||||||
|
msg.namespace,
|
||||||
|
msg.fallbacks,
|
||||||
|
msg.operation,
|
||||||
|
msg.args
|
||||||
|
);
|
||||||
|
if (invocation) port.postMessage({ invocation, message: res });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg?.channel === "notion-enhancer") {
|
||||||
|
if (sender.tab && msg.message === "load-complete") {
|
||||||
|
if (tabQueue.has(sender.tab.id)) {
|
||||||
|
chrome.tabs.sendMessage(sender.tab.id, {
|
||||||
|
channel: "notion-enhancer",
|
||||||
|
message: "open-menu",
|
||||||
|
});
|
||||||
|
tabQueue.delete(sender.tab.id);
|
||||||
|
}
|
||||||
|
} else if (msg.message === "reload-app") {
|
||||||
|
reloadNotionTabs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user