2023-06-03 19:43:41 +00:00
|
|
|
/* eslint-disable no-continue */
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
/* eslint-disable max-len */
|
|
|
|
/* eslint-disable no-ternary */
|
2022-10-24 19:57:51 -04:00
|
|
|
var titlepref = chrome.i18n.getMessage("titlePreface");
|
|
|
|
var titleprefpriv = chrome.i18n.getMessage("titlePrefacePrivate");
|
|
|
|
var webpref = chrome.i18n.getMessage("webPreface");
|
|
|
|
var webprefpriv = chrome.i18n.getMessage("webPrefacePrivate");
|
|
|
|
var routerpref = chrome.i18n.getMessage("routerPreface");
|
|
|
|
var routerprefpriv = chrome.i18n.getMessage("routerPrefacePrivate");
|
|
|
|
var mailpref = chrome.i18n.getMessage("mailPreface");
|
|
|
|
var mailprefpriv = chrome.i18n.getMessage("mailPrefacePrivate");
|
|
|
|
var torrentpref = chrome.i18n.getMessage("torrentPreface");
|
|
|
|
var torrentprefpriv = chrome.i18n.getMessage("torrentPrefacePrivate");
|
|
|
|
var tunnelpref = chrome.i18n.getMessage("i2ptunnelPreface");
|
|
|
|
var tunnelprefpriv = chrome.i18n.getMessage("i2ptunnelPrefacePrivate");
|
|
|
|
var ircpref = chrome.i18n.getMessage("ircPreface");
|
|
|
|
var ircprefpriv = chrome.i18n.getMessage("ircPrefacePrivate");
|
|
|
|
var extensionpref = chrome.i18n.getMessage("extensionPreface");
|
|
|
|
var muwirepref = chrome.i18n.getMessage("muwirePreface");
|
|
|
|
var muwireprefpriv = chrome.i18n.getMessage("muwirePrefacePrivate");
|
|
|
|
var botepref = chrome.i18n.getMessage("botePreface");
|
|
|
|
var blogpref = chrome.i18n.getMessage("blogPreface");
|
|
|
|
var blogprefpriv = chrome.i18n.getMessage("blogPrefacePrivate");
|
|
|
|
var torpref = chrome.i18n.getMessage("torPreface");
|
|
|
|
var torprefpriv = chrome.i18n.getMessage("torPrefacePrivate");
|
2019-11-11 23:31:37 -05:00
|
|
|
|
2020-12-09 20:51:08 -05:00
|
|
|
function onError(err) {
|
2022-10-24 19:57:51 -04:00
|
|
|
console.log("(background)", err);
|
2020-12-09 20:45:49 -05:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
function logContexts(contexts) {
|
|
|
|
if (contexts) {
|
2022-10-24 19:57:51 -04:00
|
|
|
console.log(contexts);
|
|
|
|
}
|
2020-12-09 20:45:49 -05:00
|
|
|
}
|
|
|
|
|
2019-11-24 17:14:43 -05:00
|
|
|
function onContextsGot(contexts) {
|
2023-06-03 19:43:41 +00:00
|
|
|
const prefList = [
|
2023-06-10 23:39:06 -04:00
|
|
|
{ name: titlepref, color: "orange", icon: "fingerprint" },
|
|
|
|
{ name: blogpref, color: "pink", icon: "pet" },
|
|
|
|
{ name: webpref, color: "red", icon: "circle" },
|
|
|
|
{ name: routerpref, color: "blue", icon: "briefcase" },
|
|
|
|
{ name: tunnelpref, color: "green", icon: "tree" },
|
|
|
|
{ name: mailpref, color: "yellow", icon: "briefcase" },
|
|
|
|
{ name: torrentpref, color: "purple", icon: "chill" },
|
|
|
|
{ name: ircpref, color: "red", icon: "vacation" },
|
|
|
|
{ name: torpref, color: "purple", icon: "circle" },
|
|
|
|
{ name: muwirepref, color: "turquoise", icon: "gift" },
|
|
|
|
{ name: botepref, color: "blue", icon: "fence" },
|
2023-06-03 19:43:41 +00:00
|
|
|
];
|
2023-06-10 23:39:06 -04:00
|
|
|
const ids = contexts.map((context) => context.name);
|
2022-10-24 19:57:51 -04:00
|
|
|
console.log("Checking new contexts");
|
2023-06-10 23:39:06 -04:00
|
|
|
prefList.forEach((pref) => {
|
2023-06-03 19:43:41 +00:00
|
|
|
if (ids.indexOf(pref.name) === -1) {
|
|
|
|
browser.contextualIdentities.create(pref).then(onCreated, onNotCreated);
|
|
|
|
}
|
|
|
|
});
|
2019-07-10 00:47:30 -04:00
|
|
|
}
|
2019-07-13 22:07:24 -04:00
|
|
|
|
2025-02-07 14:58:03 -05:00
|
|
|
browser.windows.onCreated.addListener(onContextsGot);
|
|
|
|
|
2025-02-07 14:57:17 -05:00
|
|
|
// every time a window opens, call onContextsGot
|
|
|
|
browser.tabs.onCreated.addListener(onContextsGot);
|
|
|
|
|
2020-01-02 13:59:21 -05:00
|
|
|
function onContextsError() {
|
2022-10-24 19:57:51 -04:00
|
|
|
console.log("Error finding contextual identities, is the API enabled?");
|
2020-01-02 13:59:21 -05:00
|
|
|
}
|
|
|
|
|
2019-10-28 01:11:16 -04:00
|
|
|
function onCreated(context) {
|
2022-10-24 19:57:51 -04:00
|
|
|
console.log(" ID:", context.cookieStoreId, "created.");
|
2019-10-28 01:11:16 -04:00
|
|
|
}
|
|
|
|
|
2020-01-02 13:59:21 -05:00
|
|
|
function onNotCreated(context) {
|
2022-10-24 19:57:51 -04:00
|
|
|
console.log("ID:", context.cookieStoreId, "not created.");
|
2020-01-02 13:59:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
browser.contextualIdentities.query({}).then(onContextsGot, onContextsError);
|
2019-06-17 19:17:11 -04:00
|
|
|
|
2019-11-24 04:13:12 -05:00
|
|
|
var gettingInfo = browser.runtime.getPlatformInfo();
|
2023-06-10 23:39:06 -04:00
|
|
|
gettingInfo.then((got) => {
|
2022-10-24 19:57:51 -04:00
|
|
|
if (got.os != "android") {
|
|
|
|
browser.windows.onCreated.addListener(themeWindow);
|
|
|
|
browser.windows.onFocusChanged.addListener(themeWindow);
|
|
|
|
browser.windows.onRemoved.addListener(themeWindow);
|
2023-06-03 19:43:41 +00:00
|
|
|
browser.tabs.onUpdated.addListener(themeWindow);
|
|
|
|
browser.tabs.onActivated.addListener(themeWindow);
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2019-11-24 04:13:12 -05:00
|
|
|
});
|
2019-02-05 10:53:26 -05:00
|
|
|
|
|
|
|
function themeWindow(window) {
|
2022-10-24 19:57:51 -04:00
|
|
|
function dynamicTheme() {
|
2023-06-03 19:43:41 +00:00
|
|
|
console.log("(theme)Active in I2P App window");
|
|
|
|
let ctheme = browser.theme.getCurrent();
|
|
|
|
ctheme.then(setDynamicTheme);
|
|
|
|
function setDynamicTheme(oldtheme) {
|
|
|
|
if (window.incognito) {
|
|
|
|
browser.theme.update(window.id, dtheme);
|
|
|
|
} else {
|
|
|
|
browser.theme.update(window.id, dtheme);
|
|
|
|
}
|
2020-12-05 23:53:24 -05:00
|
|
|
}
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2019-10-06 15:18:10 -04:00
|
|
|
|
2022-10-24 19:57:51 -04:00
|
|
|
function browserTheme() {
|
|
|
|
console.log("(theme)Active in I2P window");
|
2023-06-03 19:43:41 +00:00
|
|
|
let ctheme = browser.theme.getCurrent();
|
|
|
|
ctheme.then(setBrowserTheme);
|
|
|
|
function setBrowserTheme(oldtheme) {
|
|
|
|
btheme.images = oldtheme.images;
|
|
|
|
if (window.incognito) {
|
|
|
|
browser.theme.update(window.id, btheme);
|
|
|
|
} else {
|
|
|
|
browser.theme.update(window.id, btheme);
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2022-02-15 21:15:47 -05:00
|
|
|
}
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2022-02-15 21:15:47 -05:00
|
|
|
|
2022-10-24 19:57:51 -04:00
|
|
|
function logTabs(tabInfo) {
|
2023-06-03 19:43:41 +00:00
|
|
|
for (const tab of tabInfo) {
|
|
|
|
console.debug("(theme) logging tab", tab);
|
|
|
|
}
|
2022-10-24 19:57:51 -04:00
|
|
|
function onContextGotTheme(context) {
|
2023-06-03 19:43:41 +00:00
|
|
|
console.debug("(theme) deciding theme for context", context);
|
2022-10-24 19:57:51 -04:00
|
|
|
if (context.name == titlepref) {
|
|
|
|
browserTheme();
|
|
|
|
browser.pageAction.show(tabInfo[0].id);
|
|
|
|
} else if (context.name == routerpref) {
|
|
|
|
console.log("(theme) Active in Router Console window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else if (context.name == tunnelpref) {
|
|
|
|
console.log("(theme) Active in Hidden Services Manager window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else if (context.name == mailpref) {
|
|
|
|
console.log("(theme) Active in Web Mail window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else if (context.name == torrentpref) {
|
|
|
|
console.log("(theme) Active in Bittorrent window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else if (context.name == botepref) {
|
|
|
|
console.log("(theme) Active in Bote window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else if (context.name == ircpref) {
|
|
|
|
console.log("(theme) Active in IRC window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else if (context.name == torpref) {
|
|
|
|
console.log("(theme) Active in Tor Manager window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else if (context.name == blogpref) {
|
|
|
|
console.log("(theme) (theme) Active in Blog window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else if (context.name == muwirepref) {
|
|
|
|
console.log("(theme) Active in MuWire window");
|
|
|
|
dynamicTheme();
|
|
|
|
} else {
|
|
|
|
console.log("(theme) Not active in I2P Window");
|
|
|
|
unsetTheme();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
tabInfo[0].cookieStoreId != "firefox-default" &&
|
|
|
|
tabInfo[0].cookieStoreId != "firefox-private"
|
|
|
|
) {
|
|
|
|
browser.contextualIdentities
|
|
|
|
.get(tabInfo[0].cookieStoreId)
|
|
|
|
.then(onContextGotTheme, onThemeError);
|
|
|
|
} else {
|
2023-06-03 19:43:41 +00:00
|
|
|
console.log("(theme) default context identified");
|
2022-10-24 19:57:51 -04:00
|
|
|
unsetTheme();
|
2022-10-07 19:32:52 -04:00
|
|
|
}
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2022-02-15 21:15:47 -05:00
|
|
|
|
2022-10-24 19:57:51 -04:00
|
|
|
var querying = browser.tabs.query({
|
|
|
|
currentWindow: true,
|
2023-06-10 23:39:06 -04:00
|
|
|
active: true,
|
2022-10-24 19:57:51 -04:00
|
|
|
});
|
|
|
|
querying.then(logTabs, onThemeError);
|
2022-10-07 19:32:52 -04:00
|
|
|
}
|
2022-02-15 21:15:47 -05:00
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
function queryTitle(window) {
|
2022-10-24 19:57:51 -04:00
|
|
|
// Check if the window is in private browsing
|
|
|
|
function onContextError() {
|
2023-06-03 19:43:41 +00:00
|
|
|
console.log("(theme) Error finding context");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTitle(title, privTitle) {
|
2023-06-10 23:39:06 -04:00
|
|
|
const titlePreface = window.incognito ? privTitle : title;
|
2023-06-03 19:43:41 +00:00
|
|
|
browser.windows.update(window.id, { titlePreface });
|
|
|
|
}
|
|
|
|
|
|
|
|
function onContextGotTitle(context) {
|
|
|
|
const titleMap = {
|
|
|
|
titlepref: "Active in I2P window",
|
|
|
|
muwirepref: "Active in MuWire window",
|
|
|
|
routerpref: "Active in Router Console window",
|
|
|
|
botepref: "Active in Bote window",
|
|
|
|
tunnelpref: "Active in Hidden Services Manager window",
|
|
|
|
mailpref: "Active in I2P Web Mail window",
|
|
|
|
blogpref: "Active in I2P Blog window",
|
|
|
|
torrentpref: "Active in I2P Torrent window",
|
|
|
|
ircpref: "Active in IRC window",
|
2023-06-10 23:39:06 -04:00
|
|
|
torpref: "Active in Tor Manager window",
|
2023-06-03 19:43:41 +00:00
|
|
|
};
|
|
|
|
const { name } = context;
|
|
|
|
console.log(titleMap[name]);
|
|
|
|
setTitle(name, `${name}priv`);
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function logTabs(tabInfo) {
|
2023-06-03 19:43:41 +00:00
|
|
|
const { cookieStoreId } = tabInfo[0];
|
2023-06-10 23:39:06 -04:00
|
|
|
if (
|
|
|
|
cookieStoreId === "firefox-default" ||
|
|
|
|
cookieStoreId === "firefox-private"
|
|
|
|
) {
|
2023-06-03 19:43:41 +00:00
|
|
|
setTitle("", "");
|
|
|
|
} else {
|
2022-10-24 19:57:51 -04:00
|
|
|
browser.contextualIdentities
|
2023-06-03 19:43:41 +00:00
|
|
|
.get(cookieStoreId)
|
2022-10-24 19:57:51 -04:00
|
|
|
.then(onContextGotTitle, onContextError);
|
2022-10-16 22:58:48 -04:00
|
|
|
}
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2022-10-16 22:58:48 -04:00
|
|
|
|
2023-06-10 23:39:06 -04:00
|
|
|
const querying = browser.tabs.query({ currentWindow: true, active: true });
|
2022-10-24 19:57:51 -04:00
|
|
|
querying.then(logTabs, onContextError);
|
2019-02-19 11:43:44 -05:00
|
|
|
}
|
2019-02-07 20:14:57 -05:00
|
|
|
|
2020-01-02 13:59:21 -05:00
|
|
|
var gettingListenerInfo = browser.runtime.getPlatformInfo();
|
2023-06-10 23:39:06 -04:00
|
|
|
gettingListenerInfo.then((got) => {
|
2022-10-24 19:57:51 -04:00
|
|
|
function onPlatformError() {
|
|
|
|
console.log("Error finding platform info");
|
|
|
|
}
|
|
|
|
if (got.os != "android") {
|
|
|
|
browser.tabs.onCreated.addListener(() => {
|
|
|
|
var getting = browser.windows.getCurrent({
|
2023-06-10 23:39:06 -04:00
|
|
|
populate: true,
|
2022-10-24 19:57:51 -04:00
|
|
|
});
|
2023-06-03 19:43:41 +00:00
|
|
|
getting.then(queryTitle, onPlatformError);
|
2022-10-24 19:57:51 -04:00
|
|
|
});
|
|
|
|
browser.tabs.onActivated.addListener(() => {
|
|
|
|
var getting = browser.windows.getCurrent({
|
2023-06-10 23:39:06 -04:00
|
|
|
populate: true,
|
2022-10-24 19:57:51 -04:00
|
|
|
});
|
2023-06-03 19:43:41 +00:00
|
|
|
getting.then(queryTitle, onPlatformError);
|
2022-10-24 19:57:51 -04:00
|
|
|
});
|
|
|
|
}
|
2019-02-07 20:14:57 -05:00
|
|
|
});
|
2020-01-13 15:22:02 -05:00
|
|
|
|
2020-02-26 13:00:51 -05:00
|
|
|
function handleClick() {
|
2022-10-24 19:57:51 -04:00
|
|
|
console.log("Opening page action");
|
|
|
|
browser.pageAction.openPopup();
|
2020-02-26 13:00:51 -05:00
|
|
|
}
|
|
|
|
browser.pageAction.onClicked.addListener(handleClick);
|
2020-03-05 23:25:08 -05:00
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
async function checkCertificate(details) {
|
|
|
|
if (!details.url.startsWith("https") || !details.url.includes(".i2p")) {
|
2022-10-24 19:57:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
const activeTabs = await browser.tabs.query({ active: true });
|
2022-10-24 19:57:51 -04:00
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
if (!activeTabs) {
|
2022-10-24 19:57:51 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
for (const tab of activeTabs) {
|
|
|
|
if (details.url !== tab.url) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const securityInfo = await browser.webRequest.getSecurityInfo(
|
|
|
|
details.requestId,
|
|
|
|
{ certificateChain: true }
|
|
|
|
);
|
|
|
|
console.log("(cert) state is complete", securityInfo);
|
|
|
|
console.log("(cert) certificates", securityInfo.certificates);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 23:25:08 -05:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
/* Listen for onHeaderReceived for the target page.
|
|
|
|
Set "blocking" and "responseHeaders". */
|
2020-03-05 23:25:08 -05:00
|
|
|
browser.webRequest.onHeadersReceived.addListener(
|
2023-06-03 19:43:41 +00:00
|
|
|
checkCertificate,
|
2022-10-24 19:57:51 -04:00
|
|
|
{ urls: ["<all_urls>"] },
|
2023-06-10 23:39:06 -04:00
|
|
|
["blocking", "responseHeaders"]
|
2020-03-05 23:25:08 -05:00
|
|
|
);
|
2020-12-01 18:07:25 -05:00
|
|
|
|
|
|
|
function onClosedWindowCheck() {
|
2023-06-10 23:39:06 -04:00
|
|
|
const contextQuery = browser.contextualIdentities.query({
|
|
|
|
name: "titlepref",
|
|
|
|
});
|
2022-10-24 19:57:51 -04:00
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
function checkTabs(context) {
|
|
|
|
for (let ctx of context) {
|
|
|
|
function deleteIfEmpty(tabs) {
|
|
|
|
if (tabs.length === 0) {
|
|
|
|
browser.contextualIdentities.remove(ctx.cookieStoreId);
|
2021-05-24 13:57:37 -04:00
|
|
|
}
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2023-06-03 19:43:41 +00:00
|
|
|
const tabs = browser.tabs.query({ cookieStoreId: ctx.cookieStoreId });
|
|
|
|
tabs.then(deleteIfEmpty, onError);
|
2020-12-01 18:07:25 -05:00
|
|
|
}
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2023-06-03 19:43:41 +00:00
|
|
|
|
|
|
|
contextQuery.then(checkTabs, onError);
|
2020-12-01 18:07:25 -05:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:43:41 +00:00
|
|
|
async function onOpenedWindowCheck() {
|
2023-06-10 23:39:06 -04:00
|
|
|
const contexts = await browser.contextualIdentities.query({
|
|
|
|
name: titlepref,
|
|
|
|
});
|
2023-06-03 19:43:41 +00:00
|
|
|
|
|
|
|
function deleteContextIfNoTabs(tabs, context) {
|
|
|
|
if (tabs.length == 0 && context != 0) {
|
|
|
|
browser.contextualIdentities.remove(context.cookieStoreId);
|
2021-05-24 13:57:37 -04:00
|
|
|
}
|
2025-02-20 14:50:55 -05:00
|
|
|
// once we're done, re-create all the contexts
|
|
|
|
browser.contextualIdentities.query({}).then(onContextsGot, onContextsError);
|
2022-10-24 19:57:51 -04:00
|
|
|
}
|
2023-06-03 19:43:41 +00:00
|
|
|
|
|
|
|
async function checkTabs(context) {
|
2023-06-10 23:39:06 -04:00
|
|
|
const tabs = await browser.tabs.query({
|
|
|
|
cookieStoreId: context.cookieStoreId,
|
|
|
|
});
|
2023-06-03 19:43:41 +00:00
|
|
|
await deleteContextIfNoTabs(tabs, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const context of contexts) {
|
|
|
|
await checkTabs(context);
|
|
|
|
}
|
2021-05-24 13:57:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
onOpenedWindowCheck();
|
2025-02-07 14:57:17 -05:00
|
|
|
onContextsGot();
|
2021-05-24 13:57:37 -04:00
|
|
|
|
2020-12-01 18:07:25 -05:00
|
|
|
browser.tabs.onRemoved.addListener(onClosedWindowCheck);
|
2021-09-30 16:46:20 -04:00
|
|
|
|
2025-02-07 14:57:17 -05:00
|
|
|
if (browser.windows === undefined) {
|
2022-10-24 19:57:51 -04:00
|
|
|
console.log("windows unavailable on android", browser.runtime.PlatformOs);
|
|
|
|
browser.windows.onRemoved.addListener(onClosedWindowCheck);
|
|
|
|
browser.windows.onCreated.addListener(onOpenedWindowCheck);
|
2022-10-24 16:04:35 -04:00
|
|
|
}
|
2023-06-03 19:43:41 +00:00
|
|
|
|
|
|
|
function putCurrentThemeInLocalStorage() {
|
|
|
|
let mine = isMyTheme();
|
|
|
|
mine.then(processTheme, themeStoreError);
|
|
|
|
function processTheme(mineval) {
|
|
|
|
console.debug("(theme) validating the current theme:", !mineval);
|
|
|
|
if (mineval) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// get the current theme:
|
|
|
|
const currentTheme = browser.theme.getCurrent();
|
|
|
|
currentTheme.then(storeTheme, themeStoreError);
|
|
|
|
function storeTheme(theme) {
|
|
|
|
console.debug("(theme) stored the current theme:", theme);
|
2023-06-10 23:39:06 -04:00
|
|
|
browser.storage.local.set({ name: "theme", theme });
|
2023-06-03 19:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function themeStoreError(err) {
|
|
|
|
console.warn("(theme) theme storage error", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function putLatestThemeIDInLocalStorage(extensionInfo) {
|
|
|
|
let mine = isMyTheme();
|
|
|
|
mine.then(processTheme, themeStoreError);
|
|
|
|
function processTheme(mineval) {
|
|
|
|
console.debug("(theme) validating the current constant theme:", !mineval);
|
|
|
|
if (mineval) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.debug("(theme) storing theme by ID", extensionInfo.id);
|
|
|
|
if (extensionInfo.type === "theme") {
|
|
|
|
let themeID = extensionInfo.id;
|
2023-06-10 23:39:06 -04:00
|
|
|
browser.storage.local.set({ themeID });
|
2023-06-03 19:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function themeStoreError(err) {
|
|
|
|
console.warn("(theme) theme ID storage error", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function restoreLatestThemeIDInLocalStorage() {
|
|
|
|
const storedTheme = browser.storage.local.get("themeID");
|
|
|
|
storedTheme.then(restoreTheme, restoreThemeError);
|
|
|
|
function restoreTheme(theme) {
|
|
|
|
if (theme.themeID) {
|
|
|
|
let dis = browser.management.setEnabled(theme.themeID, false);
|
2023-06-10 23:39:06 -04:00
|
|
|
dis.then(function () {
|
2023-06-03 19:43:41 +00:00
|
|
|
console.debug("(theme) theme restored by ID", theme.themeID);
|
|
|
|
browser.management.setEnabled(theme.themeID, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function restoreThemeError(err) {
|
|
|
|
console.warn("(theme) theme restore error", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
browser.management.onEnabled.addListener(putLatestThemeIDInLocalStorage);
|
|
|
|
|
|
|
|
function onThemeError(err) {
|
|
|
|
console.warn("(theme) theme error", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
browser.theme.onUpdated.addListener(putCurrentThemeInLocalStorage);
|
2023-06-10 23:50:40 -04:00
|
|
|
browser.windows.onCreated.addListener(putCurrentThemeInLocalStorage);
|
|
|
|
browser.tabs.onCreated.addListener(putCurrentThemeInLocalStorage);
|
2023-06-03 19:43:41 +00:00
|
|
|
|
|
|
|
function unsetTheme() {
|
|
|
|
const storedTheme = browser.storage.local.get("theme");
|
|
|
|
storedTheme.then(restoreTheme, restoreThemeError);
|
2023-06-11 14:03:27 -04:00
|
|
|
async function restoreTheme(theme) {
|
|
|
|
let mt = await isMyTheme();
|
|
|
|
if (mt) {
|
|
|
|
if (theme.theme) {
|
|
|
|
console.debug("(theme) analyzing theme", theme.theme);
|
|
|
|
if (theme.theme.colors) {
|
|
|
|
theme.theme.images = {};
|
|
|
|
console.warn(
|
|
|
|
"(theme) There's not a way to restore theme images yet."
|
|
|
|
);
|
|
|
|
browser.theme.update(theme.theme);
|
|
|
|
console.log("(theme) restored the stored theme", theme);
|
|
|
|
} else {
|
|
|
|
browser.theme.reset();
|
|
|
|
}
|
2023-06-03 19:43:41 +00:00
|
|
|
}
|
2023-06-11 14:03:27 -04:00
|
|
|
restoreLatestThemeIDInLocalStorage();
|
2023-06-03 19:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function restoreThemeError(err) {
|
|
|
|
console.debug("(theme) theme restore error", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let btheme = {
|
|
|
|
colors: {
|
|
|
|
frame: "#363A68",
|
|
|
|
toolbar: "#363A68",
|
2023-06-10 23:39:06 -04:00
|
|
|
tab_text: "#ECF3FF",
|
|
|
|
},
|
2023-06-03 19:43:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let dtheme = {
|
|
|
|
colors: {
|
|
|
|
frame: "#4456B7",
|
|
|
|
toolbar: "#4456B7",
|
2023-06-10 23:39:06 -04:00
|
|
|
tab_text: "#ECF3FF",
|
|
|
|
},
|
2023-06-03 19:43:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function isMyTheme() {
|
|
|
|
function hasMatchingColors(theme, targetColors) {
|
|
|
|
console.debug("(theme) comparison", theme.colors, targetColors.colors);
|
|
|
|
if (theme.colors === null || targetColors.colors === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-10 23:39:06 -04:00
|
|
|
return (
|
|
|
|
theme.colors.frame == targetColors.colors.frame &&
|
|
|
|
theme.colors.toolbar == targetColors.colors.toolbar &&
|
|
|
|
theme.colors.tab_text == targetColors.colors.tab_text
|
|
|
|
);
|
2023-06-03 19:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const currentTheme = await browser.theme.getCurrent();
|
|
|
|
const isLightTheme = hasMatchingColors(currentTheme, dtheme);
|
|
|
|
const isDarkTheme = hasMatchingColors(currentTheme, btheme);
|
|
|
|
|
|
|
|
console.debug(`Is current theme a dynamic theme? ${isLightTheme}`);
|
|
|
|
console.debug(`Is current theme a browser theme? ${isDarkTheme}`);
|
|
|
|
|
|
|
|
return isLightTheme || isDarkTheme;
|
|
|
|
}
|