129 lines
3.7 KiB
JavaScript
129 lines
3.7 KiB
JavaScript
(function (root) {
|
|
"use strict";
|
|
|
|
const FONT_STYLE_KEY = "glagolifyFontStyle";
|
|
const FONT_STYLES = new Set(["round", "angular-sans", "angular", "missal-angular"]);
|
|
const DEFAULT_FONT_STYLE = "round";
|
|
|
|
function createBackgroundController(api) {
|
|
const tabStates = new Map();
|
|
let fontStyle = DEFAULT_FONT_STYLE;
|
|
const fontStyleReady = Promise.resolve(api.storage.local.get(FONT_STYLE_KEY))
|
|
.then((stored) => {
|
|
if (stored && FONT_STYLES.has(stored[FONT_STYLE_KEY])) {
|
|
fontStyle = stored[FONT_STYLE_KEY];
|
|
}
|
|
return fontStyle;
|
|
})
|
|
.catch(() => fontStyle);
|
|
|
|
function validTabId(tabId) {
|
|
return Number.isInteger(tabId) && tabId >= 0;
|
|
}
|
|
|
|
async function broadcast(tabId, enabled) {
|
|
await fontStyleReady;
|
|
try {
|
|
await api.tabs.sendMessage(tabId, {
|
|
type: "glagolify:set-enabled",
|
|
enabled,
|
|
fontStyle
|
|
});
|
|
return true;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
api.runtime.onMessage.addListener((message, sender) => {
|
|
if (!message || typeof message.type !== "string") {
|
|
return undefined;
|
|
}
|
|
|
|
if (message.type === "glagolify:document-ready") {
|
|
const tabId = sender && sender.tab && sender.tab.id;
|
|
return fontStyleReady.then(() => ({
|
|
enabled: validTabId(tabId) && tabStates.get(tabId) === true,
|
|
fontStyle
|
|
}));
|
|
}
|
|
|
|
if (message.type === "glagolify:get-tab-state") {
|
|
const tabId = message.tabId;
|
|
return fontStyleReady.then(async () => {
|
|
if (!validTabId(tabId)) {
|
|
return { enabled: false, available: false, fontStyle };
|
|
}
|
|
|
|
const enabled = tabStates.get(tabId) === true;
|
|
const available = await broadcast(tabId, enabled);
|
|
return { enabled, available, fontStyle };
|
|
});
|
|
}
|
|
|
|
if (message.type === "glagolify:set-tab-enabled") {
|
|
const tabId = message.tabId;
|
|
if (!validTabId(tabId) || typeof message.enabled !== "boolean") {
|
|
return fontStyleReady.then(() => ({
|
|
enabled: false,
|
|
available: false,
|
|
fontStyle
|
|
}));
|
|
}
|
|
|
|
return fontStyleReady.then(async () => {
|
|
tabStates.set(tabId, message.enabled);
|
|
const available = await broadcast(tabId, message.enabled);
|
|
return {
|
|
enabled: message.enabled,
|
|
available,
|
|
fontStyle
|
|
};
|
|
});
|
|
}
|
|
|
|
if (message.type === "glagolify:set-font-style") {
|
|
if (!FONT_STYLES.has(message.fontStyle)) {
|
|
return fontStyleReady.then(() => ({ fontStyle, accepted: false }));
|
|
}
|
|
|
|
return fontStyleReady.then(async () => {
|
|
await api.storage.local.set({ [FONT_STYLE_KEY]: message.fontStyle });
|
|
fontStyle = message.fontStyle;
|
|
await Promise.all(
|
|
[...tabStates]
|
|
.filter(([, tabEnabled]) => tabEnabled)
|
|
.map(([tabId]) => broadcast(tabId, true))
|
|
);
|
|
return { fontStyle, accepted: true };
|
|
});
|
|
}
|
|
|
|
return undefined;
|
|
});
|
|
|
|
api.tabs.onRemoved.addListener((tabId) => {
|
|
tabStates.delete(tabId);
|
|
});
|
|
|
|
return {
|
|
tabStates,
|
|
broadcast,
|
|
fontStyleReady,
|
|
getFontStyle: () => fontStyle
|
|
};
|
|
}
|
|
|
|
const exported = Object.freeze({ createBackgroundController });
|
|
|
|
if (typeof browser === "object" && browser.runtime && browser.tabs) {
|
|
createBackgroundController(browser);
|
|
}
|
|
|
|
if (typeof module === "object" && module.exports) {
|
|
module.exports = exported;
|
|
}
|
|
|
|
root.GlagolifyBackground = exported;
|
|
})(typeof globalThis === "object" ? globalThis : this);
|