From ec56dfa99cf384d9f7372ed38944e0cc8e0b6988 Mon Sep 17 00:00:00 2001 From: magnolia1234 <7676006-magnolia1234@users.noreply.gitlab.com> Date: Wed, 17 Feb 2021 19:11:27 +0100 Subject: [PATCH] Fix private window (cookies/popup) --- background.js | 184 ++++++++++++++++++++++++++-------------------- changelog.txt | 1 + manifest.json | 2 +- optin/opt-in.js | 2 +- options.js | 9 ++- options_custom.js | 8 +- popup.js | 16 ++-- 7 files changed, 134 insertions(+), 88 deletions(-) diff --git a/background.js b/background.js index 2fe46e1..ee1dd99 100644 --- a/background.js +++ b/background.js @@ -386,29 +386,6 @@ function setDefaultOptions() { }); } -// copy storage.sync to storage.local (quota exceeded) -ext_api.storage.sync.get({ - sites: {}, - sites_custom: {}, - daily_users: {}, - optIn: {}, - optInShown: {}, - customShown: {} -}, function (items) { - if (Object.keys(items.sites).length > 0) { - ext_api.storage.local.set({ - sites: items.sites, - sites_custom: items.sites_custom, - daily_users: items.daily_users, - optIn: items.optIn, - optInShown: items.optInShown, - customShown: items.customShown - }, function () { - ext_api.storage.sync.remove(['sites', 'sites_custom']); - }); - } -}); - var grouped_sites = { '###_au_comm_media': au_comm_media_domains, '###_au_news_corp': au_news_corp_domains, @@ -1140,55 +1117,52 @@ function site_switch() { }); } -function popup_show_toggle_tab(callback) { - ext_api.tabs.query({ - active: true, - currentWindow: true - }, function (tabs) { - if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { - let currentUrl = tabs[0].url; - let domain; - let isExcludedSite = matchUrlDomain(excludedSites, currentUrl); - if (!isExcludedSite) { - let isDefaultSiteGrouped = matchUrlDomain(defaultSites_domains, currentUrl); - let isDefaultSite = matchUrlDomain(defaultSites_domains, currentUrl); - let isCustomSite = matchUrlDomain(Object.values(customSites_domains), currentUrl); - domain = isDefaultSiteGrouped || (!isDefaultSite && isCustomSite); - } - callback(domain); - } - }); -}; - // remove cookies after page load ext_api.webRequest.onCompleted.addListener(function (details) { - var domainVar = matchUrlDomain(remove_cookies, details.url); - if ((!['main_frame', 'xmlhttprequest', 'other'].includes(details.type)) || !domainVar || !enabledSites.includes(domainVar)) - return; - ext_api.cookies.getAll({ - domain: domainVar - }, function (cookies) { - for (let cookie of cookies) { - var rc_domain = cookie.domain.replace(/^(\.?www\.|\.)/, ''); - // hold specific cookie(s) from remove_cookies domains - if ((rc_domain in remove_cookies_select_hold) && remove_cookies_select_hold[rc_domain].includes(cookie.name)) { - continue; // don't remove specific cookie + ext_api.cookies.getAllCookieStores(function (cookieStores) { + ext_api.tabs.query({ + active: true, + currentWindow: true + }, function (tabs) { + if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + let tabId = tabs[0].id; + let storeId = 0; + for (let store of cookieStores) { + if (store.tabIds.includes(tabId)) + storeId = store.id; + } + var domainVar = matchUrlDomain(remove_cookies, details.url); + if ((!['main_frame', 'xmlhttprequest', 'other'].includes(details.type)) || !domainVar || !enabledSites.includes(domainVar)) + return; + ext_api.cookies.getAll({ + domain: domainVar, + storeId: storeId + }, function (cookies) { + for (let cookie of cookies) { + var rc_domain = cookie.domain.replace(/^(\.?www\.|\.)/, ''); + // hold specific cookie(s) from remove_cookies domains + if ((rc_domain in remove_cookies_select_hold) && remove_cookies_select_hold[rc_domain].includes(cookie.name)) { + continue; // don't remove specific cookie + } + // drop only specific cookie(s) from remove_cookies domains + if ((rc_domain in remove_cookies_select_drop) && !(remove_cookies_select_drop[rc_domain].includes(cookie.name))) { + continue; // only remove specific cookie + } + // hold on to consent-cookie + if (cookie.name.match(/(consent|^optanon)/i)) { + continue; + } + cookie.domain = cookie.domain.replace(/^\./, ''); + ext_api.cookies.remove({ + url: (cookie.secure ? "https://" : "http://") + cookie.domain + cookie.path, + name: cookie.name, + storeId: storeId + }); + } + }); } - // drop only specific cookie(s) from remove_cookies domains - if ((rc_domain in remove_cookies_select_drop) && !(remove_cookies_select_drop[rc_domain].includes(cookie.name))) { - continue; // only remove specific cookie - } - // hold on to consent-cookie - if (cookie.name.match(/(consent|^optanon)/i)) { - continue; - } - cookie.domain = cookie.domain.replace(/^\./, ''); - ext_api.cookies.remove({ - url: (cookie.secure ? "https://" : "http://") + cookie.domain + cookie.path, - name: cookie.name - }); - } - }); + }); + }) }, { urls: [""] }); @@ -1226,17 +1200,71 @@ ext_api.runtime.onMessage.addListener(function (message, sender) { }); }); } + if (message.request === 'clear_cookies') { + clear_cookies(); + } // clear cookies for domain if (message.domain) { - var domainVar = message.domain.replace('www.', ''); - ext_api.cookies.getAll({ - domain: domainVar - }, function (cookies) { - for (let cookie of cookies) { - cookie.domain = cookie.domain.replace(/^\./, ''); - ext_api.cookies.remove({ - url: (cookie.secure ? "https://" : "http://") + cookie.domain + cookie.path, - name: cookie.name + ext_api.cookies.getAllCookieStores(function (cookieStores) { + ext_api.tabs.query({ + active: true, + currentWindow: true + }, function (tabs) { + if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + let tabId = tabs[0].id; + let storeId = 0; + for (let store of cookieStores) { + if (store.tabIds.includes(tabId)) + storeId = store.id; + } + var domainVar = message.domain.replace('www.', ''); + ext_api.cookies.getAll({ + domain: domainVar, + storeId: storeId + }, function (cookies) { + for (let cookie of cookies) { + cookie.domain = cookie.domain.replace(/^\./, ''); + ext_api.cookies.remove({ + url: (cookie.secure ? "https://" : "http://") + cookie.domain + cookie.path, + name: cookie.name, + storeId: storeId + }); + } + }); + } + }); + }) + } + if (message.request === 'defaultSites_domains') { + ext_api.tabs.sendMessage( + sender.tab.id, { + "defaultSites_domains": defaultSites_domains + }); + } + if (message.request === 'site_switch') { + site_switch(); + } + if (message.request === 'popup_show_toggle') { + ext_api.tabs.query({ + active: true, + currentWindow: true + }, function (tabs) { + if (tabs.length > 0 && tabs[0].url && tabs[0].url.indexOf("http") !== -1) { + let currentUrl = tabs[0].url; + let domain; + let isExcludedSite = matchUrlDomain(excludedSites, currentUrl); + if (!isExcludedSite) { + let isDefaultSiteGrouped = matchUrlDomain(defaultSites_domains, currentUrl); + let isDefaultSite = matchUrlDomain(defaultSites_domains, currentUrl); + let isCustomSite = matchUrlDomain(Object.values(customSites_domains), currentUrl); + domain = isDefaultSiteGrouped || (!isDefaultSite && isCustomSite); + } + ext_api.runtime.sendMessage({ + msg: "popup_show_toggle", + data: { + domain: domain, + enabled: enabledSites.includes(domain) + } }); } }); diff --git a/changelog.txt b/changelog.txt index a0b1f86..80d2346 100644 --- a/changelog.txt +++ b/changelog.txt @@ -3,6 +3,7 @@ Changelog Bypass Paywalls Clean - Firefox Post-release Fix popup (update notification; GitLab CORS issue) +Fix private window (cookies/popup) Update limit contentScript (add image/sub_frame) * v2.0.8.0 (2021-02-14) diff --git a/manifest.json b/manifest.json index 2680cf3..d841bec 100644 --- a/manifest.json +++ b/manifest.json @@ -472,5 +472,5 @@ "*://*.userzoom.com/*", "*://*.wsj.net/*" ], - "version": "2.0.8.2" + "version": "2.0.8.3" } \ No newline at end of file diff --git a/optin/opt-in.js b/optin/opt-in.js index ab98f8a..b7aa42e 100644 --- a/optin/opt-in.js +++ b/optin/opt-in.js @@ -40,7 +40,7 @@ window.addEventListener("load", function () { "optInShown": true, "customShown": true }); - window.close(); + open(location).close(); }); var custom_enabled = document.getElementById('custom-enabled'); diff --git a/options.js b/options.js index 69e7c68..4220116 100644 --- a/options.js +++ b/options.js @@ -29,6 +29,9 @@ function save_options() { // Restores checkbox input states using the preferences stored in ext_api.storage. function renderOptions() { +ext_api.runtime.sendMessage({request: 'defaultSites_domains'}); +ext_api.runtime.onMessage.addListener(function (message, sender) { +if (message.defaultSites_domains) { var labelEl; ext_api.storage.local.get({ sites: {}, sites_custom: {}, sites_excluded: [] @@ -65,7 +68,7 @@ function renderOptions() { labelEl.appendChild(document.createTextNode('* Custom Sites')); sitesEl.appendChild(labelEl); var sites_custom = items.sites_custom; - var defaultSites_domains = ext_api.extension.getBackgroundPage().defaultSites_domains; + var defaultSites_domains = message.defaultSites_domains; for (var key in sites_custom) { var domain = sites_custom[key]['domain']; if (defaultSites.hasOwnProperty(key) || defaultSites_domains.includes(domain)) { @@ -96,6 +99,8 @@ function renderOptions() { save_options(); }); } +}); +} function selectAll() { var inputEls = Array.from(document.querySelectorAll('input')); @@ -118,7 +123,7 @@ function selectNone() { } function closeButton() { - window.close(); + open(location).close(); } document.addEventListener('DOMContentLoaded', renderOptions); diff --git a/options_custom.js b/options_custom.js index 1ed1605..4eb0388 100644 --- a/options_custom.js +++ b/options_custom.js @@ -186,6 +186,9 @@ function edit_options() { // Restores checkbox input states using the preferences stored in ext_api.storage. function renderOptions() { +ext_api.runtime.sendMessage({request: 'defaultSites_domains'}); +ext_api.runtime.onMessage.addListener(function (message, sender) { +if (message.defaultSites_domains) { ext_api.storage.local.get({ sites_custom: {} }, function (items) { @@ -253,7 +256,8 @@ function renderOptions() { for (var key in sites_custom) { optionEl = document.createElement('option'); let domain = sites_custom[key]['domain']; - let isDefaultSite = ext_api.extension.getBackgroundPage().defaultSites_domains.includes(domain); + let defaultSites_domains = message.defaultSites_domains; + let isDefaultSite = defaultSites_domains.includes(domain); optionEl.text = isDefaultSite ? '*' : ''; optionEl.text += key + ': ' + domain + (sites_custom[key]['googlebot']>0 ? ' | googlebot' : '') + @@ -279,6 +283,8 @@ function renderOptions() { } }); } +}); +} document.addEventListener('DOMContentLoaded', renderOptions); document.getElementById('save').addEventListener('click', save_options); diff --git a/popup.js b/popup.js index 3cf83a7..a182319 100644 --- a/popup.js +++ b/popup.js @@ -1,6 +1,6 @@ var ext_api = chrome || browser; -function popup_show_toggle(domain) { +function popup_show_toggle(domain, enabled) { if (domain) { var site_switch_span = document.getElementById('site_switch_span'); let labelEl = document.createElement('label'); @@ -8,7 +8,7 @@ function popup_show_toggle(domain) { let inputEl = document.createElement('input'); inputEl.setAttribute('id', 'site_switch'); inputEl.setAttribute('type', 'checkbox'); - if (ext_api.extension.getBackgroundPage().enabledSites.includes(domain)) + if (enabled) inputEl.setAttribute('checked', true); labelEl.appendChild(inputEl); let spanEl = document.createElement('span'); @@ -17,15 +17,21 @@ function popup_show_toggle(domain) { labelEl.appendChild(spanEl); site_switch_span.appendChild(labelEl); document.getElementById("site_switch").addEventListener('click', function () { - ext_api.extension.getBackgroundPage().site_switch(); + ext_api.runtime.sendMessage({request: 'site_switch'}); //open(location).close(); }); } }; -ext_api.extension.getBackgroundPage().popup_show_toggle_tab(popup_show_toggle); + +ext_api.runtime.sendMessage({request: 'popup_show_toggle'}); +ext_api.runtime.onMessage.addListener(function (message, sender) { + if (message.msg === 'popup_show_toggle' && message.data) { + popup_show_toggle(message.data.domain, message.data.enabled) + } +}); document.getElementById("clear_cookies").addEventListener('click', function () { - ext_api.extension.getBackgroundPage().clear_cookies(); + ext_api.runtime.sendMessage({request: 'clear_cookies'}); //open(location).close(); });