From 7d30e7af0ce5027f3086c9129caac943a3c74bde Mon Sep 17 00:00:00 2001 From: "jan@dwrox.net" Date: Sat, 6 Jan 2018 13:12:40 +0100 Subject: [PATCH] Update to version 1.7.0 Improved support for Firefox-Multi-Account containers using the approperiate cookie storeId for the cookie display in the UI and also to some extend for the removal and temporary storage options. Changed some general Opera/Chrome error display message to "Browser" instead of "Chrome", which would not be correct. --- cookies.js | 81 +++++++++++++++++++++++++++++++++----------------- flagCookies.js | 32 +++++++++++--------- manifest.json | 2 +- 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/cookies.js b/cookies.js index f9c5128..d7a77b9 100755 --- a/cookies.js +++ b/cookies.js @@ -44,7 +44,7 @@ function chromeGetStorageAndClearCookies (action, data, cookies, domainURL, doLo return } - clearCookiesAction(action, data, cookies, domainURL) + clearCookiesAction(action, data, cookies, domainURL, 'default') } async function getDomainURLFirefox () { @@ -106,25 +106,27 @@ async function clearCookiesWrapper (action, doChromeLoad) { let domain = getDomainURL(domainURL) let data = await browser.storage.local.get() let cookies + let cookiesURL = [] if (currentTab.cookieStoreId !== undefined) { cookies = await browser.cookies.getAll({domain: domain, storeId: currentTab.cookieStoreId}) cookiesURL = await browser.cookies.getAll({url: domainURL.replace(/\/www./, '/'), storeId: currentTab.cookieStoreId}) } else { cookies = await browser.cookies.getAll({domain: domain}) - cookiesURL = await browser.cookies.getAll({url: domainURL.replace(/\/www./, '/'), storeId: currentTab.cookieStoreId}) + cookiesURL = await browser.cookies.getAll({url: domainURL.replace(/\/www./, '/')}) } - for (cookie of cookiesURL) { + for (let cookie of cookiesURL) { cookies.push(cookie) } - clearCookiesAction(action, data, cookies, domainURL) + if (currentTab.cookieStoreId !== undefined) clearCookiesAction(action, data, cookies, domainURL, currentTab.cookieStoreId) + else clearCookiesAction(action, data, cookies, domainURL, 'default') } function handleMessage (request, sender, sendResponse) { if (request.getCookies !== undefined) { if (cookieData[request.getCookies] !== undefined) { - sendResponse({'cookies': cookieData[request.getCookies]}) + sendResponse({'cookies': cookieData[request.getCookies][request.storeId]}) } else { let found = false for (let entry of Object.keys(cookieData)) { @@ -142,7 +144,7 @@ function handleMessage (request, sender, sendResponse) { } // Clear the cookies which are enabled for the domain in browser storage -async function clearCookiesAction (action, data, cookies, domainURL) { +async function clearCookiesAction (action, data, cookies, domainURL, activeCookieStore) { if (domainURL === '' || cookies === undefined) return let useWWW = false @@ -157,21 +159,27 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } } - if (cookieData[domainURL] === undefined) cookieData[domainURL] = [] + if (cookieData[domainURL] === undefined) { + cookieData[domainURL] = {} + } + + if (cookieData[domainURL][activeCookieStore] === undefined) { + cookieData[domainURL][activeCookieStore] = [] + } for (let cookie of cookies) { let foundCookie = false let index = 0 - for (let cookieEntry of cookieData[domainURL]) { + for (let cookieEntry of cookieData[domainURL][activeCookieStore]) { if (cookieEntry.name === cookie.name) { - cookieData[domainURL][index] = cookie + cookieData[domainURL][activeCookieStore][index] = cookie foundCookie = true break } index++ } - if (!foundCookie) cookieData[domainURL].push(cookie) + if (!foundCookie) cookieData[domainURL][activeCookieStore].push(cookie) } if (data[domainURL] === undefined && data['flagCookies_flagGlobal'] === undefined) { @@ -185,7 +193,7 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } if (data['flagCookies_autoFlag'] && data['flagCookies_autoFlag'][domainURL]) { - for (let cookie of cookieData[domainURL]) { + for (let cookie of cookieData[domainURL][activeCookieStore]) { if (hasProfile && hasLogged && data['flagCookies_logged'][domainURL][cookie.name] !== undefined) { let msg = "Allowed profile cookie on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" addToLogData(msg) @@ -201,9 +209,8 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } for (let urlString of urls) { - let details = { url: urlString, name: cookie.name } - if (useChrome) { + let details = { url: urlString, name: cookie.name } if (chrome.cookies.remove(details) !== null) { if (data[domainURL][cookie.name] === true) { let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" @@ -231,6 +238,7 @@ async function clearCookiesAction (action, data, cookies, domainURL) { continue } + let details = { url: urlString, name: cookie.name, storeId: activeCookieStore } if ((await browser.cookies.remove(details)) !== null) { if (data[domainURL][cookie.name] === true) { let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" @@ -246,7 +254,7 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } } else if (data['flagCookies_flagGlobal'] !== undefined && data['flagCookies_flagGlobal'] === true) { if (data[domainURL] === undefined) { - for (let cookie of cookieData[domainURL]) { + for (let cookie of cookieData[domainURL][activeCookieStore]) { if (hasProfile && hasLogged && data['flagCookies_logged'][domainURL][cookie.name] !== undefined) { let msg = "Allowed profile cookie on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" addToLogData(msg) @@ -255,8 +263,8 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } for (let urlString of urls) { - let details = { url: urlString, name: cookie.name } if (useChrome) { + let details = { url: urlString, name: cookie.name } if (chrome.cookies.remove(details) !== null) { let msg = "Global-flag deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" addToLogData(msg) @@ -266,6 +274,7 @@ async function clearCookiesAction (action, data, cookies, domainURL) { continue } + let details = { url: urlString, name: cookie.name, storeId: activeCookieStore } if ((await browser.cookies.remove(details)) !== null) { let msg = "Global-flag deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" addToLogData(msg) @@ -274,7 +283,7 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } } } else { - for (let cookie of cookieData[domainURL]) { + for (let cookie of cookieData[domainURL][activeCookieStore]) { if (hasProfile && hasLogged && data['flagCookies_logged'][domainURL][cookie.name] !== undefined) { let msg = "Allowed profile cookie on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" addToLogData(msg) @@ -290,8 +299,8 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } for (let urlString of urls) { - let details = { url: urlString, name: cookie.name } if (useChrome) { + let details = { url: urlString, name: cookie.name } if (chrome.cookies.remove(details) !== null) { if (data[domainURL] !== undefined && data[domainURL][cookie.name] !== undefined && data[domainURL][cookie.name] === true) { let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" @@ -307,6 +316,7 @@ async function clearCookiesAction (action, data, cookies, domainURL) { continue } + let details = { url: urlString, name: cookie.name, storeId: activeCookieStore } if ((await browser.cookies.remove(details)) !== null) { if (data[domainURL] !== undefined && data[domainURL][cookie.name] !== undefined && data[domainURL][cookie.name] === true) { let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" @@ -326,7 +336,7 @@ async function clearCookiesAction (action, data, cookies, domainURL) { return } - for (let cookie of cookieData[domainURL]) { + for (let cookie of cookieData[domainURL][activeCookieStore]) { if (data[domainURL][cookie.name] === undefined) { continue } @@ -346,9 +356,8 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } for (let urlString of urls) { - let details = { url: urlString, name: cookie.name } - if (useChrome) { + let details = { url: urlString, name: cookie.name } if (chrome.cookies.remove(details) !== null) { let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" addToLogData(msg) @@ -358,6 +367,7 @@ async function clearCookiesAction (action, data, cookies, domainURL) { continue } + let details = { url: urlString, name: cookie.name, storeId: activeCookieStore } if ((await browser.cookies.remove(details)) != null) { let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'" addToLogData(msg) @@ -367,8 +377,12 @@ async function clearCookiesAction (action, data, cookies, domainURL) { } } - if (action.toLowerCase().indexOf('close') !== -1 && cookieData[domainURL] !== undefined) { - delete cookieData[domainURL] + if (action.toLowerCase().indexOf('close') !== -1 && cookieData[domainURL][activeCookieStore] !== undefined) { + delete cookieData[domainURL][activeCookieStore] + + if (Object.keys(cookieData[domainURL]).length === 0) { + delete cookieData[domainURL] + } } } @@ -390,15 +404,23 @@ function chromeUpdateLogData (data, writeData) { if (data['flagCookies'] === undefined) data['flagCookies'] = {} data['flagCookies']['logData'] = logData - chrome.storage.local.set(data, updatedData); + chrome.storage.local.set(data, updatedData) } else { - chrome.storage.local.get(null, function(data) { checkChromeHadNoErrors(); chromeUpdateLogData(data, true) }) + chrome.storage.local.get(null, function (data) { checkChromeHadNoErrors(); chromeUpdateLogData(data, true) }) } } -function clearCookiesOnNavigate (details) { +async function clearCookiesOnNavigate (details) { if (details.parentFrameId === undefined || details.parentFrameId !== -1 || details.url === undefined) return + let activeCookieStore = 'default' + if (!useChrome) { + let currentTab = await getActiveTabFirefox() + if (currentTab.cookieStoreId !== undefined) { + activeCookieStore = currentTab.cookieStoreId + } + } + let domainURL let urlMatch = details.url.replace(/\/www\./, '/').match(/(http|https):\/\/[a-zA-Z0-9öäüÖÄÜ.-]*\//) if (urlMatch) { @@ -408,9 +430,12 @@ function clearCookiesOnNavigate (details) { return } - if (cookieData[domainURL] !== undefined) delete cookieData[domainURL] - clearDomainLog(domainURL) + if (cookieData[domainURL][activeCookieStore] !== undefined) { + delete cookieData[domainURL][activeCookieStore] + if (Object.keys(cookieData[domainURL]).length === 0) delete cookieData[domainURL] + } + clearDomainLog(domainURL, activeCookieStore) clearCookiesWrapper('tab navigate', useChrome) } @@ -454,7 +479,7 @@ async function clearCookiesOnUpdate (tabId, changeInfo, tab) { } } -let clearCookiesOnLeave = (tabId, moveInfo) => { +function clearCookiesOnLeave (tabId, moveInfo) { clearCookiesWrapper('tab close/window close', useChrome) } diff --git a/flagCookies.js b/flagCookies.js index 703c16a..25e56b6 100644 --- a/flagCookies.js +++ b/flagCookies.js @@ -7,9 +7,9 @@ function checkChromeHadNoErrors () { if (chrome.runtime.lastError) { if (hasConsole) { if (chrome.runtime.lastError.message !== undefined) { - console.log('Chrome had an error, with mesage: ' + chrome.runtime.lastError.message) + console.log('Browser had an error, with mesage: ' + chrome.runtime.lastError.message) } else { - console.log('Chrome had an error.') + console.log('Browser had an error.') } } @@ -24,11 +24,11 @@ function checkChromeHadNoErrors () { function getChromeStorageForFunc (func) { chrome.storage.local.get(null, function (data) { if (checkChromeHadNoErrors()) { - if (hasConsole) console.log('Chrome retrieved storage data.') + if (hasConsole) console.log('Browser retrieved storage data.') func(data) } else if (hasConsole) { - console.log('Chrome storage retrieval error.') + console.log('Browser storage retrieval error.') } }) } @@ -36,11 +36,11 @@ function getChromeStorageForFunc (func) { function getChromeStorageForFunc1 (func, par1) { chrome.storage.local.get(null, function (data) { if (checkChromeHadNoErrors()) { - if (hasConsole) console.log('Chrome retrieved storage data.') + if (hasConsole) console.log('Browser retrieved storage data.') func(data, par1) } else if (hasConsole) { - console.log('Chrome storage retrieval error.') + console.log('Browser storage retrieval error.') } }) } @@ -48,11 +48,11 @@ function getChromeStorageForFunc1 (func, par1) { function getChromeStorageForFunc2 (func, par1, par2) { chrome.storage.local.get(null, function (data) { if (checkChromeHadNoErrors()) { - if (hasConsole) console.log('Chrome retrieved storage data.') + if (hasConsole) console.log('Browser retrieved storage data.') func(data, par1, par2) } else if (hasConsole) { - console.log('Chrome storage retrieval error.') + console.log('Browser storage retrieval error.') } }) } @@ -61,10 +61,10 @@ function setChromeStorage (data) { chrome.storage.local.set(data, function () { if (checkChromeHadNoErrors()) { if (hasConsole) { - console.log('Chrome updated the storage data.') + console.log('Browser updated the storage data.') } } else if (hasConsole) { - console.log('Chrome updating storage error.') + console.log('Browser updating storage error.') } }) } @@ -76,7 +76,7 @@ function chromeGetStorageAndCookiesForFunc (data, cookies, func) { chrome.storage.local.get(null, function (data) { chromeGetStorageAndCookiesForFunc(data, null, func) }) return } else if (cookies === null) { - chrome.runtime.sendMessage({'getCookies': domainURL}, function (response) { checkChromeHadNoErrors(); chromeGetStorageAndCookiesForFunc(data, response['cookies'], func) }) + chrome.runtime.sendMessage({'getCookies': domainURL, 'storeId': 'default'}, function (response) { checkChromeHadNoErrors(); chromeGetStorageAndCookiesForFunc(data, response['cookies'], func) }) return } @@ -116,10 +116,14 @@ async function initDomainURLandProceed (tabs) { // Get storage and cookies Firefox let data = await browser.storage.local.get() //let cookies = await browser.cookies.getAll({url: domainURL}) - let cookieData = await browser.runtime.sendMessage({'getCookies': domainURL}) - let cookies = cookieData['cookies'] + let activeCookieStore = 'default' + if (tab.cookieStoreId !== undefined) { + activeCookieStore = tab.cookieStoreId + } - updateUIData(data, cookies) + let cookieData = await browser.runtime.sendMessage({'getCookies': domainURL, 'storeId': activeCookieStore}) + let cookies = cookieData['cookies'] + updateUIData(data, cookies, activeCookieStore) } function updateUIData (data, cookies) { diff --git a/manifest.json b/manifest.json index 5c15d30..3e24bf3 100644 --- a/manifest.json +++ b/manifest.json @@ -31,7 +31,7 @@ "homepage_url": "https://github.com/jrie/flagCookies", "manifest_version": 2, "name": "Flag Cookies", - "version": "1.6.9", + "version": "1.7.0", "permissions": ["cookies", "tabs", "webNavigation", "", "storage", "unlimitedStorage"], "background": { "scripts": [