Skip to content

Commit

Permalink
Update for v2.0.0
Browse files Browse the repository at this point in the history
- Fixed, auto-flag feature not setting all known domains cookies to be flagged on change
- Fixed, count of deleted cookies not reported twice
- Improved support for Firefox-Multi-Account containers using the identity context name
  • Loading branch information
jrie committed Jan 7, 2018
1 parent 018420d commit ebeb04f
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 52 deletions.
20 changes: 16 additions & 4 deletions cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ async function clearCookiesAction (action, data, cookies, domainURL, activeCooki
}

let details = { url: urlString, name: cookie.name, storeId: activeCookieStore }
if ((await browser.cookies.remove(details)) !== null) {
if (await browser.cookies.remove(details) !== null) {
if (data[contextName][domainURL][cookie.name] === true) {
let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'"
addToLogData(msg)
Expand Down Expand Up @@ -305,7 +305,6 @@ async function clearCookiesAction (action, data, cookies, domainURL, activeCooki
}

let details = { url: urlString, name: cookie.name, storeId: activeCookieStore }

if (await browser.cookies.remove(details) !== null) {
if (data[contextName] !== undefined && data[contextName][domainURL] !== undefined && data[contextName][domainURL][cookie.name] !== undefined && data[contextName][domainURL][cookie.name] === true) {
let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'"
Expand All @@ -318,7 +317,7 @@ async function clearCookiesAction (action, data, cookies, domainURL, activeCooki
}
}
} else {
if (data[contextName] === undefined || data[contextName][domainURL] === undefined) {
if (data[contextName] === undefined || data[contextName][domainURL] === undefined || Object.keys(data[contextName][domainURL]) === 0) {
return
}

Expand Down Expand Up @@ -351,7 +350,7 @@ async function clearCookiesAction (action, data, cookies, domainURL, activeCooki
}

let details = { url: urlString, name: cookie.name, storeId: activeCookieStore }
if ((await browser.cookies.remove(details)) != null) {
if (await browser.cookies.remove(details) != null) {
let msg = "Deleted on '" + action + "', cookie: '" + cookie.name + "' for '" + domainURL + "'"
addToLogData(msg)
}
Expand Down Expand Up @@ -442,17 +441,30 @@ async function clearCookiesOnNavigate (details) {
}

async function clearCookiesOnUpdate (tabId, changeInfo, tab) {
if (useChrome) chrome.browserAction.disable(tabId)
else browser.browserAction.disable(tabId)

if (changeInfo.status && changeInfo.status === 'loading') {
clearCookiesWrapper('tab reload/load', useChrome, tab)
} else if (changeInfo.status && changeInfo.status === 'complete') {
if (useChrome) chrome.browserAction.enable(tabId)
else browser.browserAction.enable(tabId)
if (logData[contextName] !== undefined) {
let urlMatch = tab.url.replace(/\/www\./, '/').match(/(http|https):\/\/[a-zA-Z0-9öäüÖÄÜ.-]*\//)
if (urlMatch) {
let tabDomain = urlMatch[0]

let count = 0
let foundCookies = []
for (let entry of logData[contextName]) {
if (entry.indexOf(tabDomain) !== -1 && entry.toLowerCase().indexOf('deleted') !== -1) {
let cookieMatch = entry.match(/cookie: '[^']*/)
if (cookieMatch !== null) {
let cookieName = cookieMatch[0]
cookieName = cookieName.substr(cookieName.indexOf("'") + 1, cookieName.length)
if (foundCookies.indexOf(cookieName) !== -1) continue
foundCookies.push(cookieName)
}
count++
}
}
Expand Down
145 changes: 97 additions & 48 deletions flagCookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
let useChrome = typeof (browser) === 'undefined'
let hasConsole = typeof (console) !== 'undefined'
let contextName = 'default'
let domainURL = ''

// --------------------------------------------------------------------------------------------------------------------------------
// Chrome helpers
function checkChromeHadNoErrors () {
if (chrome.runtime.lastError) {
Expand Down Expand Up @@ -84,8 +86,30 @@ function chromeGetStorageAndCookiesForFunc (data, cookies, func) {
func(data, cookies)
}

function chromeGetStorageAndCookiesForFunc1 (data, cookies, func, par1) {
if (!checkChromeHadNoErrors()) return

if (data === null) {
chrome.storage.local.get(null, function (data) { chromeGetStorageAndCookiesForFunc(data, null, func, par1) })
return
} else if (cookies === null) {
chrome.runtime.sendMessage({'getCookies': domainURL, 'storeId': 'default'}, function (response) { checkChromeHadNoErrors(); chromeGetStorageAndCookiesForFunc(data, response['cookies'], func, par1) })
return
}

func(data, cookies, par1)
}

// --------------------------------------------------------------------------------------------------------------------------------
let domainURL = ''
// Firefox
async function getActiveTabFirefox () {
let activeTabs = await browser.tabs.query({currentWindow: true, active: true})
if (activeTabs.length !== 0) {
return activeTabs.pop()
}

return null
}

function firefoxOnGetContextSuccess (context) {
contextName = context.name
Expand All @@ -98,6 +122,8 @@ function firefoxOnGetContextError (e) {
}
}

// --------------------------------------------------------------------------------------------------------------------------------

async function initDomainURLandProceed (tabs) {
let tab = tabs.pop()

Expand Down Expand Up @@ -151,7 +177,7 @@ function updateUIData (data, cookies) {

if (cookies.length === 0) {
let infoDisplay = document.getElementById('infoDisplay')
let contentText = 'No active cookies for domain.'
let contentText = 'No active cookies for domain, you might need to reload the tab.'
infoDisplay.children[0].textContent = contentText
infoDisplay.removeAttribute('class')
} else {
Expand Down Expand Up @@ -360,41 +386,24 @@ async function flaggedCookieSwitchNeutral (data, event) {
for (let child of domainCookieList.children) {
if (child.children[0].dataset['name'] === cookieName) {
if (hasAutoFlag) {
data[contextName][domainURL][cookieName] = 'af'
child.children[0].className = 'checkmark auto-flagged'
} else if (hasGlobal) {
delete data[contextName][domainURL][cookieName]

if (Object.keys(data[contextName][domainURL]).length === 0) delete data[contextName][domainURL]
if (Object.keys(data[contextName]).length === 0) {
if (useChrome) chrome.storage.local.remove(contextName, function () { checkChromeHadNoErrors() })
else await browser.storage.local.remove(contextName)
delete data[contextName]
}

child.children[0].className = 'checkmark auto-flagged'
} else {
delete data[contextName][domainURL][cookieName]

if (Object.keys(data[contextName][domainURL]).length === 0) delete data[contextName][domainURL]
if (Object.keys(data[contextName]).length === 0) {
if (useChrome) chrome.storage.local.remove(contextName, function () { checkChromeHadNoErrors() })
else await browser.storage.local.remove(contextName)
delete data[contextName]
}

child.children[0].className = 'checkmark'
}

if (useChrome) {
setChromeStorage(data)
if (!checkChromeHadNoErrors()) return
} else await browser.storage.local.set(data)
if (useChrome) setChromeStorage(data)
else await browser.storage.local.set(data)

break
}
}

if (hasAutoFlag) reFlagDomainCookies('af')
else if (hasGlobal) reFlagDomainCookies(null)
else reFlagDomainCookies(null)

let parent = event.target.parentNode.parentNode

parent.removeChild(event.target.parentNode)
Expand All @@ -407,6 +416,59 @@ async function flaggedCookieSwitchNeutral (data, event) {
}
}

async function reFlagDomainCookies (newStatus) {
if (useChrome) {
chromeGetStorageAndCookiesForFunc1(null, null, reFlagDomainCookiesNeutral, newStatus)
return
}

// Get storage and cookies Firefox
let activeCookieStore = 'default'
let currentTab = await getActiveTabFirefox()
if (currentTab.cookieStoreId !== undefined) {
activeCookieStore = currentTab.cookieStoreId
}

let data = await browser.storage.local.get()
let cookieData = await browser.runtime.sendMessage({'getCookies': domainURL, 'storeId': activeCookieStore})
let cookies = cookieData['cookies']
reFlagDomainCookiesNeutral(data, cookies, newStatus)
}

async function reFlagDomainCookiesNeutral (data, cookies, newStatus) {
if (data[contextName] === undefined) data[contextName] = {}
if (data[contextName][domainURL] === undefined) data[contextName][domainURL] = {}

for (let cookie of cookies) {
if (newStatus === null) {
if (data[contextName][domainURL][cookie.name] === undefined || data[contextName][domainURL][cookie.name] === false || data[contextName][domainURL][cookie.name] === true) {
continue
}

delete data[contextName][domainURL][cookie.name]
continue
}

if (data[contextName][domainURL][cookie.name] === undefined || (data[contextName][domainURL][cookie.name] !== false && data[contextName][domainURL][cookie.name] !== true)) {
data[contextName][domainURL][cookie.name] = newStatus
}
}

if (newStatus === null) {
if (Object.keys(data[contextName][domainURL]).length === 0) delete data[contextName][domainURL]
if (Object.keys(data[contextName]).length === 0) {
if (useChrome) chrome.storage.local.remove(contextName, function () { checkChromeHadNoErrors() })
else await browser.storage.local.remove(contextName)
delete data[contextName]
}

return
}

if (useChrome) setChromeStorage(data)
else await browser.storage.local.set(data)
}

// Permitted view flag switch
// Chrome + Firefox
async function permittedCookieSwitch (event) {
Expand Down Expand Up @@ -457,10 +519,8 @@ async function permittedCookieSwitchNeutral (data, event) {
child.children[0].className = 'checkmark'
}

if (useChrome) {
setChromeStorage(data)
if (!checkChromeHadNoErrors()) return
} else await browser.storage.local.set(data)
if (useChrome) setChromeStorage(data)
else await browser.storage.local.set(data)
break
}
}
Expand Down Expand Up @@ -585,10 +645,8 @@ async function cookieLockSwitchNeutral (data, event) {
if (Object.keys(data['flagCookies_logged'][contextName][domainURL]).length === 0) delete data['flagCookies_logged'][contextName][domainURL]
if (Object.keys(data['flagCookies_logged'][contextName]).length === 0) delete data['flagCookies_logged'][contextName]

if (useChrome) {
setChromeStorage(data)
if (!checkChromeHadNoErrors) return
} else await browser.storage.local.set(data)
if (useChrome) setChromeStorage(data)
else await browser.storage.local.set(data)

let loggedInCookieList = document.getElementById('loggedInCookies')
removeCookieOfProfileList(loggedInCookieList, cookieName, 'flagCookies_logged')
Expand All @@ -601,10 +659,8 @@ async function cookieLockSwitchNeutral (data, event) {
} else {
data['flagCookies_logged'][contextName][domainURL][cookieName] = true

if (useChrome) {
setChromeStorage(data)
if (!checkChromeHadNoErrors) return
} else await browser.storage.local.set(data)
if (useChrome) setChromeStorage(data)
else await browser.storage.local.set(data)

let loggedInCookieList = document.getElementById('loggedInCookies')
addCookieToProfileList(loggedInCookieList, cookieName, 'flagCookies_logged')
Expand Down Expand Up @@ -1089,11 +1145,8 @@ async function dumpProfileCookieNeutral (data, event) {
delete data[cookieSrc][contextName]
}

if (useChrome) {
setChromeStorage(data)

if (!checkChromeHadNoErrors) return
} else await browser.storage.local.set(data)
if (useChrome) setChromeStorage(data)
else await browser.storage.local.set(data)

let cookieList = document.getElementById('cookie-list')
for (let child of cookieList.children) {
Expand Down Expand Up @@ -1138,13 +1191,9 @@ async function accountModeSwitchNeutral (data, event) {
else await browser.storage.local.remove('flagCookies_accountMode')
}

if (useChrome) {
setChromeStorage(data)
if (checkChromeHadNoErrors) event.target.removeAttribute('class')
return
}
if (useChrome) setChromeStorage(data)
else await browser.storage.local.set(data)

await browser.storage.local.set(data)
event.target.removeAttribute('class')
return
}
Expand Down

0 comments on commit ebeb04f

Please sign in to comment.