Files
I2P_in_Private_Browsing_Mod…/background.js

495 lines
14 KiB
JavaScript
Raw Normal View History

2021-01-31 01:07:14 -05: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");
function onError(err) {
2021-01-31 01:07:14 -05:00
console.log("(background)", err);
}
function onContextGotLog(contexts) {
if (contexts != null) {
console.log(contexts);
}
}
2019-11-24 17:14:43 -05:00
function onContextsGot(contexts) {
2019-10-06 15:18:10 -04:00
var ids = [];
for (let context of contexts) {
2020-01-14 14:49:48 -05:00
console.log(`Name : ${context.name}`);
2019-10-06 15:18:10 -04:00
ids.push(context.name);
}
2021-01-31 01:07:14 -05:00
console.log("Checking new contexts");
if (ids.indexOf(titlepref) == -1) {
2019-12-24 16:51:38 -05:00
browser.contextualIdentities
.create({
name: titlepref,
2021-01-31 01:07:14 -05:00
color: "orange",
icon: "fingerprint",
2019-12-24 16:51:38 -05:00
})
.then(onCreated, onNotCreated);
2019-10-06 15:18:10 -04:00
}
if (ids.indexOf(webpref) == -1) {
2019-12-24 16:51:38 -05:00
browser.contextualIdentities
.create({
name: webpref,
2021-01-31 01:07:14 -05:00
color: "red",
icon: "circle",
2019-12-24 16:51:38 -05:00
})
.then(onCreated, onNotCreated);
}
if (ids.indexOf(routerpref) == -1) {
2019-12-24 16:51:38 -05:00
browser.contextualIdentities
.create({
name: routerpref,
2021-01-31 01:07:14 -05:00
color: "blue",
icon: "briefcase",
2019-12-24 16:51:38 -05:00
})
.then(onCreated, onNotCreated);
2019-10-06 22:11:23 -04:00
}
if (ids.indexOf(tunnelpref) == -1) {
2019-12-24 16:51:38 -05:00
browser.contextualIdentities
.create({
name: tunnelpref,
2021-01-31 01:07:14 -05:00
color: "green",
icon: "tree",
2019-12-24 16:51:38 -05:00
})
.then(onCreated, onNotCreated);
2019-11-11 16:34:41 -05:00
}
if (ids.indexOf(mailpref) == -1) {
2019-12-24 16:51:38 -05:00
browser.contextualIdentities
.create({
name: mailpref,
2021-01-31 01:07:14 -05:00
color: "yellow",
icon: "briefcase",
2019-12-24 16:51:38 -05:00
})
.then(onCreated, onNotCreated);
2019-11-11 16:34:41 -05:00
}
if (ids.indexOf(torrentpref) == -1) {
2019-12-24 16:51:38 -05:00
browser.contextualIdentities
.create({
name: torrentpref,
2021-01-31 01:07:14 -05:00
color: "purple",
icon: "chill",
2019-12-24 16:51:38 -05:00
})
.then(onCreated, onNotCreated);
2019-11-11 16:34:41 -05:00
}
2020-11-10 20:59:04 -05:00
if (ids.indexOf(ircpref) == -1) {
browser.contextualIdentities
.create({
2020-11-10 20:59:04 -05:00
name: ircpref,
2021-01-31 01:07:14 -05:00
color: "red",
icon: "vacation",
})
.then(onCreated, onNotCreated);
}
2020-11-10 20:59:04 -05:00
if (ids.indexOf(muwirepref) == -1) {
browser.contextualIdentities
.create({
name: muwirepref,
2021-01-31 01:07:14 -05:00
color: "turquoise",
icon: "gift",
})
.then(onCreated, onNotCreated);
}
if (ids.indexOf(botepref) == -1) {
browser.contextualIdentities
.create({
name: botepref,
2021-01-31 01:07:14 -05:00
color: "blue",
icon: "fence",
})
.then(onCreated, onNotCreated);
}
}
function onContextsError() {
2021-01-31 01:07:14 -05:00
console.log("Error finding contextual identities, is the API enabled?");
}
2019-10-28 01:11:16 -04:00
function onCreated(context) {
2021-01-31 01:07:14 -05:00
console.log(" ID:", context.cookieStoreId, "created.");
2019-10-28 01:11:16 -04:00
}
function onNotCreated(context) {
2021-01-31 01:07:14 -05:00
console.log("ID:", context.cookieStoreId, "not created.");
}
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();
2020-11-10 21:48:08 -05:00
gettingInfo.then((got) => {
2021-01-31 01:07:14 -05:00
if (got.os != "android") {
2019-11-24 04:13:12 -05:00
browser.windows.onCreated.addListener(themeWindow);
browser.windows.onFocusChanged.addListener(themeWindow);
browser.windows.onRemoved.addListener(themeWindow);
browser.tabs.onUpdated.addListener(themeWindowByTab);
browser.tabs.onActivated.addListener(themeWindowByTab);
}
});
2019-02-05 10:53:26 -05:00
2019-10-28 01:11:16 -04:00
function themeWindowByTab(tabId) {
function tabWindow(tab) {
var gettingPlatformInfo = browser.runtime.getPlatformInfo();
2020-11-10 21:48:08 -05:00
gettingPlatformInfo.then((got) => {
2021-01-31 01:07:14 -05:00
if (got.os == "android") {
let getwindow = browser.tabs.get(tab.tabId);
2019-11-24 04:13:12 -05:00
getwindow.then(themeWindow);
} else {
let getwindow = browser.windows.get(tab.windowId);
2019-11-24 04:13:12 -05:00
getwindow.then(themeWindow);
}
});
2019-10-28 01:11:16 -04:00
}
2021-01-31 01:07:14 -05:00
if (typeof tabId === "number") {
let tab = browser.tabs.get(tabId);
2019-10-28 01:11:16 -04:00
tab.then(tabWindow);
} else {
tabWindow(tabId);
}
2019-10-07 22:51:53 -04:00
}
function isEmpty(obj) {
if (obj === undefined || obj === null) {
return true;
}
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
2021-01-31 01:07:14 -05:00
let btheme = {
colors: {
frame: "#363A68",
toolbar: "#363A68",
},
};
let dtheme = {
colors: {
frame: "#4456B7",
toolbar: "#4456B7",
},
};
2019-02-05 10:53:26 -05:00
function themeWindow(window) {
2019-10-06 15:18:10 -04:00
// Check if the window is in private browsing
function onThemeError() {
2021-01-31 01:07:14 -05:00
console.log("theme color set error");
}
2021-01-31 01:07:14 -05:00
function dynamicTheme() {
if (window.incognito) {
2021-01-31 01:07:14 -05:00
browser.theme.update(window.id, dtheme);
} else {
2021-01-31 01:07:14 -05:00
browser.theme.update(window.id, dtheme);
}
}
2021-01-31 01:07:14 -05:00
2020-12-05 23:53:24 -05:00
function browserTheme() {
2021-01-31 01:07:14 -05:00
console.log("Active in I2P window");
2020-12-05 23:53:24 -05:00
if (window.incognito) {
2021-01-31 01:07:14 -05:00
browser.theme.update(window.id, btheme);
2020-12-05 23:53:24 -05:00
} else {
2021-01-31 01:07:14 -05:00
browser.theme.update(window.id, btheme);
2020-12-05 23:53:24 -05:00
}
}
2019-10-06 15:18:10 -04:00
function logTabs(tabInfo) {
2019-11-24 17:14:43 -05:00
function onContextGotTheme(context) {
if (context.name == titlepref) {
2020-12-05 23:53:24 -05:00
browserTheme();
2021-01-31 01:07:14 -05:00
if (tabInfo[0].url.startsWith("https://")) {
browser.pageAction.setPopup({
tabId: tabInfo[0].id,
2021-01-31 01:07:14 -05:00
popup: "security.html",
});
browser.pageAction.setIcon({
path: "icons/toopies.png",
tabId: tabInfo[0].id,
});
//console.log("(background) tabinfo", tabInfo[0].id)
browser.pageAction.show(tabInfo[0].id);
} else {
//browser.pageAction.hide(tabInfo[0].id);
}
} else if (context.name == routerpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in Router Console window");
dynamicTheme();
} else if (context.name == tunnelpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in Hidden Services Manager window");
dynamicTheme();
} else if (context.name == mailpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in Web Mail window");
dynamicTheme();
} else if (context.name == torrentpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in Bittorrent window");
dynamicTheme();
} else if (context.name == botepref) {
2021-01-31 01:07:14 -05:00
console.log("Active in Bote window");
dynamicTheme();
} else if (context.name == ircpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in IRC window");
dynamicTheme();
} else if (context.name == muwirepref) {
2021-01-31 01:07:14 -05:00
console.log("Active in MuWire window");
dynamicTheme();
2019-10-06 15:18:10 -04:00
}
2019-07-10 02:29:38 -04:00
}
if (
2021-01-31 01:07:14 -05:00
tabInfo[0].cookieStoreId != "firefox-default" &&
tabInfo[0].cookieStoreId != "firefox-private"
) {
2019-12-24 16:51:38 -05:00
browser.contextualIdentities
.get(tabInfo[0].cookieStoreId)
.then(onContextGotTheme, onThemeError);
2020-11-10 21:48:08 -05:00
} else {
2021-01-31 01:07:14 -05:00
console.log("Not active in I2P window");
2020-09-22 00:59:38 -04:00
function unSetTheme(them) {
2021-01-31 01:07:14 -05:00
console.log("unsetting theme", them);
2020-09-22 00:59:38 -04:00
if (Object.keys(them).length > 0) {
2021-01-31 01:07:14 -05:00
try {
browser.theme.update(window.id, them.originalTheme);
2021-02-05 12:41:26 -05:00
} catch (e) {
console.log(
"Original theme set error. Your theme must have an SVG in it.",
e
);
2021-01-31 01:07:14 -05:00
}
2020-11-10 21:48:08 -05:00
} else {
2020-09-22 01:51:55 -04:00
browser.theme.update(window.id, { colors: null });
2020-09-22 00:59:38 -04:00
}
}
2021-01-31 01:07:14 -05:00
browser.storage.local.get("originalTheme").then(unSetTheme, onError);
2019-10-07 22:51:53 -04:00
}
2019-10-06 15:18:10 -04:00
}
var querying = browser.tabs.query({
currentWindow: true,
2020-11-10 21:48:08 -05:00
active: true,
2019-10-06 15:18:10 -04:00
});
querying.then(logTabs, onThemeError);
2019-02-05 10:53:26 -05:00
}
2019-02-07 20:14:57 -05:00
function setTitle(window) {
// Check if the window is in private browsing
function onContextError() {
2021-01-31 01:07:14 -05:00
console.log("Context Error");
}
function setTitle(title, privtitle) {
if (window.incognito) {
browser.windows.update(window.id, {
2021-01-31 01:07:14 -05:00
titlePreface: privtitle + ": ",
});
} else {
browser.windows.update(window.id, {
2021-01-31 01:07:14 -05:00
titlePreface: title + ": ",
});
}
}
2019-10-06 15:18:10 -04:00
function logTabs(tabInfo) {
2019-11-24 17:14:43 -05:00
function onContextGotTitle(context) {
if (context.name == titlepref) {
2021-01-31 01:07:14 -05:00
console.log("Active in I2P window");
setTitle(titlepref, titleprefpriv);
} else if (context.name == muwirepref) {
2021-01-31 01:07:14 -05:00
console.log("Active in MuWire window");
setTitle(muwirepref, muwireprefpriv);
} else if (context.name == routerpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in Router Console window");
setTitle(routerpref, routerprefpriv);
} else if (context.name == botepref) {
2021-01-31 01:07:14 -05:00
console.log("Active in Bote window");
setTitle(botepref, boteprefpriv);
} else if (context.name == tunnelpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in Hidden Services Manager window");
setTitle(tunnelpref, tunnelprefpriv);
} else if (context.name == mailpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in I2P Web Mail window");
setTitle(mailpref, mailprefpriv);
} else if (context.name == torrentpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in I2P Torrent window");
setTitle(torrentpref, torrentprefpriv);
2020-11-10 20:59:04 -05:00
} else if (context.name == ircpref) {
2021-01-31 01:07:14 -05:00
console.log("Active in IRC window");
setTitle(ircpref, ircprefpriv);
2019-10-06 15:18:10 -04:00
}
2019-07-10 02:29:38 -04:00
}
if (
2021-01-31 01:07:14 -05:00
tabInfo[0].cookieStoreId != "firefox-default" &&
tabInfo[0].cookieStoreId != "firefox-private"
) {
2019-12-24 16:51:38 -05:00
browser.contextualIdentities
.get(tabInfo[0].cookieStoreId)
.then(onContextGotTitle, onContextError);
2019-12-24 11:21:07 -05:00
} else if (window.incognito) {
2019-12-24 16:51:38 -05:00
browser.windows.update(window.id, {
2021-01-31 01:07:14 -05:00
titlePreface: "",
2019-12-24 16:51:38 -05:00
});
} else {
browser.windows.update(window.id, {
2021-01-31 01:07:14 -05:00
titlePreface: "",
2019-12-24 16:51:38 -05:00
});
}
2019-10-06 15:18:10 -04:00
}
var querying = browser.tabs.query({
currentWindow: true,
2020-11-10 21:48:08 -05:00
active: true,
2019-10-06 15:18:10 -04:00
});
querying.then(logTabs, onContextError);
}
2019-02-07 20:14:57 -05:00
var gettingListenerInfo = browser.runtime.getPlatformInfo();
2020-11-10 21:48:08 -05:00
gettingListenerInfo.then((got) => {
function onPlatformError() {
2021-01-31 01:07:14 -05:00
console.log("Error finding platform info");
}
2021-01-31 01:07:14 -05:00
if (got.os != "android") {
browser.tabs.onCreated.addListener(() => {
var getting = browser.windows.getCurrent({
2020-11-10 21:48:08 -05:00
populate: true,
});
getting.then(setTitle, onPlatformError);
});
browser.tabs.onActivated.addListener(() => {
var getting = browser.windows.getCurrent({
2020-11-10 21:48:08 -05:00
populate: true,
});
getting.then(setTitle, onPlatformError);
});
2019-11-24 04:32:35 -05:00
}
2019-02-07 20:14:57 -05:00
});
2020-01-13 15:22:02 -05:00
function handleUpdated(updateInfo) {
2020-11-10 21:48:08 -05:00
function maybeSet(them) {
console.log("original theme found:", them, Object.keys(them).length);
try {
2021-02-05 12:41:26 -05:00
console.log(
"testing theme",
updateInfo.theme.colors.toolbar,
"!=",
btheme.colors.toolbar
);
console.log(
"testing theme",
updateInfo.theme.colors.toolbar,
"!=",
dtheme.colors.toolbar
);
if (
updateInfo.theme.colors.toolbar != dtheme.colors.toolbar &&
updateInfo.theme.colors.toolbar != btheme.colors.toolbar
) {
function onSet() {
console.log("stored theme:", updateInfo.theme);
}
/*if (
2021-01-31 01:07:14 -05:00
updateInfo.theme.colors != null &&
updateInfo.theme.images != null &&
updateInfo.theme.properties != null
) {*/
2021-02-05 12:41:26 -05:00
console.log("storing theme:", updateInfo.theme);
browser.storage.local
.set({ originalTheme: updateInfo.theme })
.then(onSet, onError);
//}
}
2020-12-09 17:49:36 -05:00
} catch (e) {
console.log("theme storage error", e);
2020-09-22 01:51:55 -04:00
}
}
2021-01-31 01:07:14 -05:00
console.log("Handling theme", updateInfo);
2020-09-22 00:59:38 -04:00
browser.storage.local.get("originalTheme").then(maybeSet, onError);
}
browser.theme.onUpdated.addListener(handleUpdated);
2020-02-26 13:00:51 -05:00
function handleClick() {
console.log("Opening page action");
browser.pageAction.openPopup();
}
browser.pageAction.onClicked.addListener(handleClick);
2020-03-05 23:25:08 -05:00
async function certCheck(details) {
if (details.url.startsWith("https")) {
console.log("(cert) https site", details.url);
} else {
return;
}
if (!details.url.includes(".i2p")) {
return;
}
var tabs = await browser.tabs.query({ active: true });
if (tabs == null) {
return;
}
console.log("(cert) checking cert", tabs);
for (tab in tabs) {
if (details.url == tabs[tab].url) {
console.log("(cert) right tab", tabs[tab].id);
try {
let 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);
}
}
}
}
// Listen for onHeaderReceived for the target page.
// Set "blocking" and "responseHeaders".
browser.webRequest.onHeadersReceived.addListener(
certCheck,
{ urls: ["<all_urls>"] },
["blocking", "responseHeaders"]
);
function onClosedWindowCheck() {
var getContext = browser.contextualIdentities.query({ name: titlepref });
function checkTabs(ctx) {
function conditionallyDelete(tabs) {
if (tabs.length == 0) {
browser.contextualIdentities.remove(ctx[0].cookieStoreId);
browser.contextualIdentities
.query({})
.then(onContextsGot, onContextsError);
}
}
var tabs = browser.tabs.query({ cookieStoreId: ctx[0].cookieStoreId });
tabs.then(conditionallyDelete, onError);
}
getContext.then(checkTabs, onError);
}
browser.tabs.onRemoved.addListener(onClosedWindowCheck);
browser.windows.onRemoved.addListener(onClosedWindowCheck);
browser.windows.onCreated.addListener(onClosedWindowCheck);