Allow for set contexts

This commit is contained in:
Idrees Hassan
2025-11-16 09:25:48 -05:00
parent 76e55a3caa
commit a5e81e4265
9 changed files with 2953 additions and 15 deletions

View File

@@ -847,6 +847,7 @@
const SAVE_KEY = "birbSaveData";
const ROOT_PATH = "";
const SET_CONTEXT = "__CONTEXT__";
/**
* @typedef {import('./application.js').BirbSaveData} BirbSaveData
@@ -1033,7 +1034,7 @@
*/
isContextActive() {
// @ts-expect-error
return typeof chrome !== "undefined";
return typeof chrome !== "undefined" && typeof chrome.storage !== "undefined" && typeof chrome.storage.sync !== "undefined";
}
/**
@@ -1077,6 +1078,7 @@
}
class ObsidianContext extends Context {
/**
* @override
* @returns {boolean}
@@ -1164,15 +1166,29 @@
}
}
const CONTEXTS = [
const contextProcessingOrder = [
new UserScriptContext(),
new ObsidianContext(),
new BrowserExtensionContext(),
new LocalContext()
];
const CONTEXTS_BY_KEY = {
"local": LocalContext,
"userscript": UserScriptContext,
"browser-extension": BrowserExtensionContext,
"obsidian": ObsidianContext
};
/**
* Determines and returns the current context
* @returns {Context}
*/
function getContext() {
for (const context of CONTEXTS) {
if (CONTEXTS_BY_KEY[SET_CONTEXT]) {
return new CONTEXTS_BY_KEY[SET_CONTEXT]();
}
for (const context of contextProcessingOrder) {
if (context.isContextActive()) {
return context;
}