Skip to content

Commit

Permalink
Update to version 1.7.0
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrie committed Jan 6, 2018
1 parent e6a477a commit 7d30e7a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 43 deletions.
81 changes: 53 additions & 28 deletions cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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 + "'"
Expand Down Expand Up @@ -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 + "'"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 + "'"
Expand All @@ -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 + "'"
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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]
}
}
}

Expand All @@ -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) {
Expand All @@ -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)
}

Expand Down Expand Up @@ -454,7 +479,7 @@ async function clearCookiesOnUpdate (tabId, changeInfo, tab) {
}
}

let clearCookiesOnLeave = (tabId, moveInfo) => {
function clearCookiesOnLeave (tabId, moveInfo) {
clearCookiesWrapper('tab close/window close', useChrome)
}

Expand Down
32 changes: 18 additions & 14 deletions flagCookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
}

Expand All @@ -24,35 +24,35 @@ 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.')
}
})
}

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.')
}
})
}

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.')
}
})
}
Expand All @@ -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.')
}
})
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<all_urls>", "storage", "unlimitedStorage"],
"background": {
"scripts": [
Expand Down

0 comments on commit 7d30e7a

Please sign in to comment.