2020-08-27 07:22:46 +02:00
|
|
|
"use strict";
|
2019-10-07 13:59:31 +02:00
|
|
|
|
2022-05-30 00:02:59 +02:00
|
|
|
import generalHelper from "../../assets/javascripts/general.js";
|
|
|
|
import utils from "../../assets/javascripts/utils.js";
|
2022-05-21 15:38:28 +02:00
|
|
|
|
2022-05-30 00:02:59 +02:00
|
|
|
import youtubeHelper from "../../assets/javascripts/youtube/youtube.js";
|
|
|
|
import youtubeMusicHelper from "../../assets/javascripts/youtubeMusic.js";
|
|
|
|
import twitterHelper from "../../assets/javascripts/twitter.js";
|
|
|
|
import instagramHelper from "../../assets/javascripts/instagram.js";
|
|
|
|
import redditHelper from "../../assets/javascripts/reddit.js";
|
|
|
|
import searchHelper from "../../assets/javascripts/search.js";
|
|
|
|
import translateHelper from "../../assets/javascripts/translate/translate.js";
|
|
|
|
import mapsHelper from "../../assets/javascripts/maps.js";
|
|
|
|
import wikipediaHelper from "../../assets/javascripts/wikipedia.js";
|
|
|
|
import mediumHelper from "../../assets/javascripts/medium.js";
|
|
|
|
import imgurHelper from "../../assets/javascripts/imgur.js";
|
|
|
|
import tiktokHelper from "../../assets/javascripts/tiktok.js";
|
|
|
|
import sendTargetsHelper from "../../assets/javascripts/sendTargets.js";
|
|
|
|
import peertubeHelper from "../../assets/javascripts/peertube.js";
|
|
|
|
import lbryHelper from "../../assets/javascripts/lbry.js";
|
2022-05-21 15:38:28 +02:00
|
|
|
|
2020-02-24 04:19:56 +01:00
|
|
|
window.browser = window.browser || window.chrome;
|
|
|
|
|
2022-05-19 18:21:39 +02:00
|
|
|
browser.runtime.onInstalled.addListener(
|
2022-06-07 18:40:03 +02:00
|
|
|
details => {
|
|
|
|
function initDefaults() {
|
2022-06-04 18:42:34 +02:00
|
|
|
fetch('/instances/blacklist.json').then(response => response.text()).then(async data => {
|
2022-06-05 14:54:13 +02:00
|
|
|
browser.storage.local.clear(
|
|
|
|
() => {
|
2022-06-07 18:40:03 +02:00
|
|
|
|
2022-06-05 14:54:13 +02:00
|
|
|
browser.storage.local.set({ cloudflareBlackList: JSON.parse(data).cloudflare },
|
|
|
|
() => {
|
|
|
|
browser.storage.local.set({ authenticateBlackList: JSON.parse(data).authenticate },
|
|
|
|
() => {
|
|
|
|
generalHelper.initDefaults();
|
|
|
|
youtubeHelper.initDefaults();
|
|
|
|
youtubeMusicHelper.initDefaults();
|
|
|
|
twitterHelper.initDefaults();
|
|
|
|
instagramHelper.initDefaults();
|
|
|
|
mapsHelper.initDefaults();
|
|
|
|
searchHelper.initDefaults();
|
|
|
|
translateHelper.initDefaults();
|
|
|
|
mediumHelper.initDefaults();
|
|
|
|
redditHelper.initDefaults();
|
|
|
|
wikipediaHelper.initDefaults();
|
|
|
|
imgurHelper.initDefaults();
|
|
|
|
tiktokHelper.initDefaults();
|
|
|
|
sendTargetsHelper.initDefaults();
|
|
|
|
peertubeHelper.initDefaults();
|
|
|
|
lbryHelper.initDefaults();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
2022-05-19 18:21:39 +02:00
|
|
|
})
|
2022-06-07 18:40:03 +02:00
|
|
|
};
|
|
|
|
// if (details.reason == 'install') {
|
|
|
|
if (details.reason == 'install' || (details.reason == "update" && details.previousVersion != browser.runtime.getManifest().version)) {
|
|
|
|
if (details.reason == "update")
|
|
|
|
browser.storage.local.get(null, r => {
|
|
|
|
if (r.theme) {
|
|
|
|
const old = encodeURIComponent(JSON.stringify(r))
|
|
|
|
browser.tabs.create({ url: browser.runtime.getURL(`/pages/background/reset_warning.html?data=${old}`) });
|
|
|
|
}
|
|
|
|
initDefaults();
|
|
|
|
})
|
|
|
|
else initDefaults();
|
2022-05-19 18:21:39 +02:00
|
|
|
}
|
2022-04-25 17:48:39 +02:00
|
|
|
}
|
2022-05-19 18:21:39 +02:00
|
|
|
)
|
2022-04-25 17:48:39 +02:00
|
|
|
|
2022-06-04 15:16:17 +02:00
|
|
|
youtubeHelper.pasteInvidiousCookies();
|
|
|
|
translateHelper.pasteSimplyTranslateCookies();
|
|
|
|
twitterHelper.pasteNitterCookies();
|
|
|
|
wikipediaHelper.pasteWikilessCookies();
|
|
|
|
searchHelper.pasteSearxCookies();
|
|
|
|
searchHelper.pasteSearxngCookies();
|
|
|
|
redditHelper.pasteLibredditCookies();
|
|
|
|
redditHelper.pasteTedditCookies();
|
|
|
|
tiktokHelper.pasteProxiTokCookies();
|
2022-06-01 23:13:08 +02:00
|
|
|
|
2022-04-25 17:48:39 +02:00
|
|
|
|
2022-04-30 11:34:38 +02:00
|
|
|
let BYPASSTABs = [];
|
2020-02-24 04:19:56 +01:00
|
|
|
browser.webRequest.onBeforeRequest.addListener(
|
2022-05-27 05:20:07 +02:00
|
|
|
details => {
|
2020-02-01 04:17:51 +01:00
|
|
|
const url = new URL(details.url);
|
2022-06-04 18:42:34 +02:00
|
|
|
if (new RegExp(/^chrome-extension:\/{2}.*\/instances\/.*.json$/).test(url.href) && details.type == 'xmlhttprequest') return;
|
2020-03-31 08:39:57 +02:00
|
|
|
let initiator;
|
2022-05-27 05:20:07 +02:00
|
|
|
try {
|
|
|
|
if (details.originUrl) initiator = new URL(details.originUrl);
|
|
|
|
else if (details.initiator) initiator = new URL(details.initiator);
|
|
|
|
}
|
|
|
|
catch { return null; }
|
|
|
|
|
|
|
|
let newUrl = youtubeMusicHelper.redirect(url, details.type)
|
|
|
|
if (!newUrl) newUrl = youtubeHelper.redirect(url, details, initiator)
|
|
|
|
if (!newUrl) newUrl = twitterHelper.redirect(url, initiator);
|
|
|
|
if (!newUrl) newUrl = instagramHelper.redirect(url, details.type, initiator);
|
|
|
|
if (!newUrl) newUrl = mapsHelper.redirect(url, initiator);
|
|
|
|
if (!newUrl) newUrl = redditHelper.redirect(url, details.type, initiator);
|
|
|
|
if (!newUrl) newUrl = mediumHelper.redirect(url, details.type, initiator);
|
|
|
|
if (!newUrl) newUrl = imgurHelper.redirect(url, details.type, initiator);
|
|
|
|
if (!newUrl) newUrl = tiktokHelper.redirect(url, details.type, initiator);
|
|
|
|
if (!newUrl) newUrl = sendTargetsHelper.redirect(url, details.type, initiator);
|
|
|
|
if (!newUrl) newUrl = peertubeHelper.redirect(url, details.type, initiator);
|
|
|
|
if (!newUrl) newUrl = lbryHelper.redirect(url, details.type, initiator);
|
|
|
|
if (!newUrl) newUrl = translateHelper.redirect(url);
|
|
|
|
if (!newUrl) newUrl = searchHelper.redirect(url)
|
|
|
|
if (!newUrl) newUrl = wikipediaHelper.redirect(url);
|
2022-01-29 19:01:49 +01:00
|
|
|
|
2022-04-03 12:55:54 +02:00
|
|
|
if (
|
|
|
|
details.frameAncestors && details.frameAncestors.length > 0 &&
|
2022-05-27 05:20:07 +02:00
|
|
|
generalHelper.isException(new URL(details.frameAncestors[0].url))
|
2022-04-03 12:55:54 +02:00
|
|
|
) newUrl = null;
|
|
|
|
|
2022-05-27 05:20:07 +02:00
|
|
|
if (generalHelper.isException(url)) newUrl = 'BYPASSTAB';
|
2022-03-01 20:42:48 +01:00
|
|
|
if (BYPASSTABs.includes(details.tabId)) newUrl = null;
|
2022-02-26 13:38:13 +01:00
|
|
|
|
2022-01-29 19:01:49 +01:00
|
|
|
if (newUrl) {
|
2022-05-03 15:45:14 +02:00
|
|
|
if (newUrl === 'CANCEL') {
|
2022-02-05 13:49:12 +01:00
|
|
|
console.log(`Canceled ${url}`);
|
|
|
|
return { cancel: true };
|
|
|
|
}
|
2022-05-27 05:20:07 +02:00
|
|
|
if (newUrl === 'BYPASSTAB') {
|
2022-05-21 15:38:28 +02:00
|
|
|
console.log(`Bypassed ${details.tabId} ${url}`);
|
2022-04-03 12:55:54 +02:00
|
|
|
if (!BYPASSTABs.includes(details.tabId)) BYPASSTABs.push(details.tabId);
|
2022-02-26 13:38:13 +01:00
|
|
|
return null;
|
|
|
|
}
|
2022-05-27 05:20:07 +02:00
|
|
|
console.info("Redirecting", url.href, "=>", newUrl);
|
|
|
|
return { redirectUrl: newUrl };
|
2020-01-14 10:48:37 +01:00
|
|
|
}
|
2022-01-30 21:36:07 +01:00
|
|
|
return null;
|
2019-09-20 12:45:58 +02:00
|
|
|
},
|
2022-01-30 21:36:07 +01:00
|
|
|
{ urls: ["<all_urls>"], },
|
2020-08-27 07:22:46 +02:00
|
|
|
["blocking"]
|
2019-09-20 12:45:58 +02:00
|
|
|
);
|
2020-04-09 11:37:56 +02:00
|
|
|
|
2022-06-01 23:13:08 +02:00
|
|
|
let incognitoList = [];
|
|
|
|
browser.tabs.onCreated.addListener(
|
|
|
|
tab => {
|
|
|
|
if (tab.incognito) {
|
|
|
|
incognitoList.push(tab.id);
|
2022-06-09 19:29:05 +02:00
|
|
|
if (incognitoList.length == 1) browser.tabs.create({ url: browser.runtime.getURL("/pages/background/incognito.html"), active: false });
|
2022-06-01 23:13:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-03-16 08:59:34 +01:00
|
|
|
browser.tabs.onRemoved.addListener(
|
|
|
|
tabId => {
|
2022-06-01 23:13:08 +02:00
|
|
|
incognitoList.pop(tabId)
|
|
|
|
|
2022-05-27 05:20:07 +02:00
|
|
|
const i = BYPASSTABs.indexOf(tabId);
|
2022-03-16 08:59:34 +01:00
|
|
|
if (i > -1) {
|
|
|
|
BYPASSTABs.splice(i, 1);
|
|
|
|
console.log("Removed BYPASSTABs", tabId);
|
|
|
|
}
|
2022-03-01 20:42:48 +01:00
|
|
|
}
|
2022-03-16 08:59:34 +01:00
|
|
|
);
|
2022-02-26 13:38:13 +01:00
|
|
|
|
2022-03-23 11:50:51 +01:00
|
|
|
browser.webRequest.onHeadersReceived.addListener(
|
2022-06-06 18:30:07 +02:00
|
|
|
e => {
|
|
|
|
let response = twitterHelper.removeXFrameOptions(e);
|
|
|
|
if (!response) response = youtubeHelper.removeXFrameOptions(e);
|
2022-05-19 02:46:38 +02:00
|
|
|
return response;
|
2022-03-23 11:50:51 +01:00
|
|
|
},
|
2022-06-11 12:03:46 +02:00
|
|
|
{ urls: ["<all_urls>"] },
|
2022-03-23 11:50:51 +01:00
|
|
|
["blocking", "responseHeaders"]
|
|
|
|
);
|
|
|
|
|
2022-05-04 12:59:37 +02:00
|
|
|
async function redirectOfflineInstance(url, tabId) {
|
2022-05-27 05:20:07 +02:00
|
|
|
let newUrl = await youtubeHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await twitterHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await instagramHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await redditHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await searchHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await translateHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await mediumHelper.switchInstance(url);
|
2022-06-10 20:03:38 +02:00
|
|
|
if (!newUrl) newUrl = await tiktokHelper.switchInstance(url);
|
2022-05-27 05:20:07 +02:00
|
|
|
if (!newUrl) newUrl = await imgurHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await wikipediaHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await peertubeHelper.switchInstance(url);
|
|
|
|
if (!newUrl) newUrl = await lbryHelper.switchInstance(url);
|
2022-03-16 08:59:34 +01:00
|
|
|
|
2022-04-18 07:06:05 +02:00
|
|
|
if (newUrl) {
|
|
|
|
if (counter >= 5) {
|
|
|
|
browser.tabs.update(tabId, { url: `/pages/errors/instance_offline.html?url=${encodeURIComponent(newUrl)}` });
|
|
|
|
counter = 0;
|
|
|
|
} else {
|
|
|
|
browser.tabs.update(tabId, { url: newUrl });
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 19:57:01 +01:00
|
|
|
}
|
2022-04-18 07:06:05 +02:00
|
|
|
let counter = 0;
|
2022-05-21 15:38:28 +02:00
|
|
|
|
|
|
|
function isAutoRedirect() {
|
2022-06-08 03:18:09 +02:00
|
|
|
return new Promise(resolve => browser.storage.local.get('autoRedirect', r => resolve(r.autoRedirect == true)))
|
2022-05-21 15:38:28 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 19:57:01 +01:00
|
|
|
browser.webRequest.onResponseStarted.addListener(
|
2022-05-21 15:38:28 +02:00
|
|
|
async details => {
|
|
|
|
if (!await isAutoRedirect()) return null;
|
2022-05-27 05:20:07 +02:00
|
|
|
if (details.type == 'main_frame' && details.statusCode >= 500) redirectOfflineInstance(new URL(details.url), details.tabId);
|
2022-03-09 20:52:58 +01:00
|
|
|
},
|
2022-06-11 12:03:46 +02:00
|
|
|
{ urls: ["<all_urls>"] }
|
2022-03-09 20:52:58 +01:00
|
|
|
)
|
|
|
|
|
2022-03-22 19:57:01 +01:00
|
|
|
browser.webRequest.onErrorOccurred.addListener(
|
2022-05-21 15:38:28 +02:00
|
|
|
async details => {
|
|
|
|
if (!await isAutoRedirect()) return;
|
2022-05-27 05:20:07 +02:00
|
|
|
if (details.type == 'main_frame') redirectOfflineInstance(new URL(details.url), details.tabId);
|
2022-03-22 19:57:01 +01:00
|
|
|
},
|
2022-06-11 12:03:46 +02:00
|
|
|
{ urls: ["<all_urls>"] }
|
2022-03-22 19:57:01 +01:00
|
|
|
)
|
|
|
|
|
2022-03-15 19:55:03 +01:00
|
|
|
browser.commands.onCommand.addListener(
|
2022-06-10 19:00:12 +02:00
|
|
|
command => {
|
2022-05-21 00:46:47 +02:00
|
|
|
if (command === 'switchInstance') utils.switchInstance();
|
|
|
|
else if (command == 'copyRaw') utils.copyRaw();
|
|
|
|
else if (command == 'unify') utils.unify();
|
2022-03-15 14:16:17 +01:00
|
|
|
}
|
2022-03-18 23:19:46 +01:00
|
|
|
)
|
|
|
|
|
2022-04-14 01:53:47 +02:00
|
|
|
browser.contextMenus.create({
|
2022-03-18 23:19:46 +01:00
|
|
|
id: "settings",
|
2022-04-02 17:23:43 +02:00
|
|
|
title: browser.i18n.getMessage("Settings"),
|
2022-03-18 23:19:46 +01:00
|
|
|
contexts: ["browser_action"]
|
|
|
|
});
|
|
|
|
|
2022-04-14 01:53:47 +02:00
|
|
|
browser.contextMenus.create({
|
2022-03-18 23:19:46 +01:00
|
|
|
id: "switchInstance",
|
2022-05-27 05:20:07 +02:00
|
|
|
title: browser.i18n.getMessage("switchInstance"),
|
2022-03-18 23:19:46 +01:00
|
|
|
contexts: ["browser_action"]
|
|
|
|
});
|
|
|
|
|
2022-05-21 00:46:47 +02:00
|
|
|
browser.contextMenus.create({
|
|
|
|
id: "copyRaw",
|
2022-06-08 03:18:09 +02:00
|
|
|
title: browser.i18n.getMessage("copyRaw"),
|
2022-05-21 00:46:47 +02:00
|
|
|
contexts: ["browser_action"]
|
|
|
|
});
|
|
|
|
|
|
|
|
browser.contextMenus.create({
|
|
|
|
id: "unify",
|
2022-06-08 03:18:09 +02:00
|
|
|
title: browser.i18n.getMessage("unifySettings"),
|
2022-05-21 00:46:47 +02:00
|
|
|
contexts: ["browser_action"]
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-05-19 18:21:39 +02:00
|
|
|
browser.contextMenus.onClicked.addListener(
|
2022-05-21 00:46:47 +02:00
|
|
|
(info) => {
|
|
|
|
if (info.menuItemId == 'switchInstance') utils.switchInstance();
|
|
|
|
else if (info.menuItemId == 'settings') browser.runtime.openOptionsPage()
|
|
|
|
else if (info.menuItemId == 'copyRaw') utils.copyRaw();
|
|
|
|
else if (info.menuItemId == 'unify') utils.unify();
|
2022-03-18 23:19:46 +01:00
|
|
|
}
|
2022-05-19 18:21:39 +02:00
|
|
|
);
|
2022-05-27 05:20:07 +02:00
|
|
|
|
2022-05-28 10:00:48 +02:00
|
|
|
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
|
|
if (message.function === 'unify') utils.unify(false).then(r => sendResponse({ response: r }))
|
|
|
|
return true;
|
|
|
|
});
|