From 533a2963797e539edc1b91e324d1a79e677c1cc7 Mon Sep 17 00:00:00 2001 From: Sahil R Date: Sun, 24 Sep 2023 23:39:25 +0530 Subject: [PATCH] added x.com to twitter domains --- background.js | 127 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 51 deletions(-) diff --git a/background.js b/background.js index 42d938a..8aa09c3 100644 --- a/background.js +++ b/background.js @@ -1,9 +1,20 @@ - // Param values from https://developer.mozilla.org/Add-ons/WebExtensions/API/contextualIdentities/create +// Param values from https://developer.mozilla.org/Add-ons/WebExtensions/API/contextualIdentities/create const TWITTER_CONTAINER_NAME = "Twitter"; const TWITTER_CONTAINER_COLOR = "blue"; const TWITTER_CONTAINER_ICON = "briefcase"; -const TWITTER_DOMAINS = ["twitter.com", "www.twitter.com", "t.co", "twimg.com", "mobile.twitter.com", "m.twitter.com", - "api.twitter.com", "abs.twimg.com", "ton.twimg.com", "pbs.twimg.com", "tweetdeck.twitter.com"]; +const TWITTER_DOMAINS = [ + "x.com", + "www.x.com", + "t.co", + "twimg.com", + "mobile.x.com", + "m.x.com", + "api.x.com", + "abs.twimg.com", + "ton.twimg.com", + "pbs.twimg.com", + "tweetdeck.x.com", +]; const MAC_ADDON_ID = "@testpilot-containers"; @@ -14,7 +25,7 @@ let twitterCookiesCleared = false; const canceledRequests = {}; const twitterHostREs = []; -async function isMACAddonEnabled () { +async function isMACAddonEnabled() { try { const macAddonInfo = await browser.management.get(MAC_ADDON_ID); if (macAddonInfo.enabled) { @@ -26,34 +37,34 @@ async function isMACAddonEnabled () { return false; } -async function setupMACAddonManagementListeners () { - browser.management.onInstalled.addListener(info => { +async function setupMACAddonManagementListeners() { + browser.management.onInstalled.addListener((info) => { if (info.id === MAC_ADDON_ID) { macAddonEnabled = true; } }); - browser.management.onUninstalled.addListener(info => { + browser.management.onUninstalled.addListener((info) => { if (info.id === MAC_ADDON_ID) { macAddonEnabled = false; } - }) - browser.management.onEnabled.addListener(info => { + }); + browser.management.onEnabled.addListener((info) => { if (info.id === MAC_ADDON_ID) { macAddonEnabled = true; } - }) - browser.management.onDisabled.addListener(info => { + }); + browser.management.onDisabled.addListener((info) => { if (info.id === MAC_ADDON_ID) { macAddonEnabled = false; } - }) + }); } -async function getMACAssignment (url) { +async function getMACAssignment(url) { try { const assignment = await browser.runtime.sendMessage(MAC_ADDON_ID, { method: "getAssignment", - url + url, }); return assignment; } catch (e) { @@ -61,15 +72,15 @@ async function getMACAssignment (url) { } } -function cancelRequest (tab, options) { +function cancelRequest(tab, options) { // we decided to cancel the request at this point, register canceled request canceledRequests[tab.id] = { requestIds: { - [options.requestId]: true + [options.requestId]: true, }, urls: { - [options.url]: true - } + [options.url]: true, + }, }; // since webRequest onCompleted and onErrorOccurred are not 100% reliable @@ -82,14 +93,16 @@ function cancelRequest (tab, options) { }, 2000); } -function shouldCancelEarly (tab, options) { +function shouldCancelEarly(tab, options) { // we decided to cancel the request at this point if (!canceledRequests[tab.id]) { cancelRequest(tab, options); } else { let cancelEarly = false; - if (canceledRequests[tab.id].requestIds[options.requestId] || - canceledRequests[tab.id].urls[options.url]) { + if ( + canceledRequests[tab.id].requestIds[options.requestId] || + canceledRequests[tab.id].urls[options.url] + ) { // same requestId or url from the same tab // this is a redirect that we have to cancel early to prevent opening two tabs cancelEarly = true; @@ -104,60 +117,62 @@ function shouldCancelEarly (tab, options) { return false; } -function generateTwitterHostREs () { +function generateTwitterHostREs() { for (let twitterDomain of TWITTER_DOMAINS) { twitterHostREs.push(new RegExp(`^(.*\\.)?${twitterDomain}$`)); } } -async function clearTwitterCookies () { +async function clearTwitterCookies() { // Clear all twitter cookies const containers = await browser.contextualIdentities.query({}); containers.push({ - cookieStoreId: 'firefox-default' + cookieStoreId: "firefox-default", }); - containers.map(container => { + containers.map((container) => { const storeId = container.cookieStoreId; if (storeId === twitterCookieStoreId) { // Don't clear cookies in the Twitter Container return; } - TWITTER_DOMAINS.map(async twitterDomain => { + TWITTER_DOMAINS.map(async (twitterDomain) => { const twitterCookieUrl = `https://${twitterDomain}/`; const cookies = await browser.cookies.getAll({ domain: twitterDomain, - storeId + storeId, }); - cookies.map(cookie => { + cookies.map((cookie) => { browser.cookies.remove({ name: cookie.name, url: twitterCookieUrl, - storeId + storeId, }); }); }); }); } -async function setupContainer () { +async function setupContainer() { // Use existing Twitter container, or create one - const contexts = await browser.contextualIdentities.query({name: TWITTER_CONTAINER_NAME}) + const contexts = await browser.contextualIdentities.query({ + name: TWITTER_CONTAINER_NAME, + }); if (contexts.length > 0) { twitterCookieStoreId = contexts[0].cookieStoreId; } else { const context = await browser.contextualIdentities.create({ name: TWITTER_CONTAINER_NAME, color: TWITTER_CONTAINER_COLOR, - icon: TWITTER_CONTAINER_ICON - }) + icon: TWITTER_CONTAINER_ICON, + }); twitterCookieStoreId = context.cookieStoreId; } } -async function containTwitter (options) { +async function containTwitter(options) { // Listen to requests and open Twitter into its Container, // open other sites into the default tab context const requestUrl = new URL(options.url); @@ -188,32 +203,32 @@ async function containTwitter (options) { // Sometimes this add-on is installed but doesn't get a twitterCookieStoreId ? if (twitterCookieStoreId) { if (shouldCancelEarly(tab, options)) { - return {cancel: true}; + return { cancel: true }; } browser.tabs.create({ url: requestUrl.toString(), cookieStoreId: twitterCookieStoreId, active: tab.active, index: tab.index, - windowId: tab.windowId + windowId: tab.windowId, }); browser.tabs.remove(options.tabId); - return {cancel: true}; + return { cancel: true }; } } } else { if (tabCookieStoreId === twitterCookieStoreId) { if (shouldCancelEarly(tab, options)) { - return {cancel: true}; + return { cancel: true }; } browser.tabs.create({ url: requestUrl.toString(), active: tab.active, index: tab.index, - windowId: tab.windowId + windowId: tab.windowId, }); browser.tabs.remove(options.tabId); - return {cancel: true}; + return { cancel: true }; } } } @@ -227,17 +242,27 @@ async function containTwitter (options) { generateTwitterHostREs(); // Add the request listener - browser.webRequest.onBeforeRequest.addListener(containTwitter, {urls: [""], types: ["main_frame"]}, ["blocking"]); + browser.webRequest.onBeforeRequest.addListener( + containTwitter, + { urls: [""], types: ["main_frame"] }, + ["blocking"] + ); // Clean up canceled requests - browser.webRequest.onCompleted.addListener((options) => { - if (canceledRequests[options.tabId]) { - delete canceledRequests[options.tabId]; - } - },{urls: [""], types: ["main_frame"]}); - browser.webRequest.onErrorOccurred.addListener((options) => { - if (canceledRequests[options.tabId]) { - delete canceledRequests[options.tabId]; - } - },{urls: [""], types: ["main_frame"]}); + browser.webRequest.onCompleted.addListener( + (options) => { + if (canceledRequests[options.tabId]) { + delete canceledRequests[options.tabId]; + } + }, + { urls: [""], types: ["main_frame"] } + ); + browser.webRequest.onErrorOccurred.addListener( + (options) => { + if (canceledRequests[options.tabId]) { + delete canceledRequests[options.tabId]; + } + }, + { urls: [""], types: ["main_frame"] } + ); })();