Skip to content

Commit

Permalink
Logic improvements
Browse files Browse the repository at this point in the history
Various improvements to logic, and added comments.
  • Loading branch information
aghorler committed Jun 3, 2017
1 parent e3fc2e5 commit 28f23f6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
10 changes: 5 additions & 5 deletions html/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
<body>
<p id="conf-warning" style="color: red; text-align: center;">Startup Cookie Manager will not function until you apply these settings.</p>

<h3>Cookie whitelist (include subdomains)</h3>
<h3 title="example.com will preserve example.com, and *.example.com cookies.">Cookie whitelist (include subdomains)</h3>

<p>
<p title="example.com will preserve example.com, and *.example.com cookies.">
<textarea id="subdomain-whitelist" rows="4" cols="50"></textarea>
</p>

<h3>Cookie whitelist (domains only)</h3>
<h3 title="example.com will preserve example.com, .example.com, and www.example.com cookies.">Cookie whitelist (root domains only)</h3>

<p>
<p title="example.com will preserve example.com, .example.com, and www.example.com cookies.">
<textarea id="domain-only-whitelist" rows="4" cols="50"></textarea>
</p>

<h3>Additional settings</h3>
<h3 title="Additional tasks to perform whenever cookies and site data are cleared.">Additional settings</h3>

<p>
<label>
Expand Down
44 changes: 31 additions & 13 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
function removeCookie(cookie){
/* Function to check whitelist status of cookie, and delete accordingly. */
function checkCookie(cookie){
chrome.storage.local.get({
subdomainWhitelist: [],
domainOnlyWhitelist: []
subDomainWhitelist: [],
rootDomainWhitelist: []
}, function(array){
var subdomainCheck = 0;
for(var i = 0; i < array.subdomainWhitelist.length; i++){
if(cookie.domain.endsWith(array.subdomainWhitelist[i])){
subdomainCheck++;

/* Check if cookie matches element of subdomain whitelist. (example.com or *.example.com) */
var preserveCookieSubdomain = false;
for(var i = 0; i < array.subDomainWhitelist.length; i++){
if(cookie.domain == array.subDomainWhitelist[i] || cookie.domain.endsWith("." + array.subDomainWhitelist[i])){
preserveCookieSubdomain = true;
break;
}
}

var domainCheck = 0;
for(var i = 0; i < array.domainOnlyWhitelist.length; i++){
if(cookie.domain == array.domainOnlyWhitelist[i] || cookie.domain == "." + array.domainOnlyWhitelist[i] || cookie.domain == "www." + array.domainOnlyWhitelist[i]){
domainCheck++;
/* Check if cookie matches element of root domain whitelist. (example.com or .example.com or www.example.com) */
var preserveCookieRoot = false;
for(var i = 0; i < array.rootDomainWhitelist.length; i++){
if(cookie.domain == array.rootDomainWhitelist[i] || cookie.domain == "." + array.rootDomainWhitelist[i] || cookie.domain == "www." + array.rootDomainWhitelist[i]){
preserveCookieRoot = true;
break;
}
}

if(subdomainCheck == 0 && domainCheck == 0){
/* Delete cookie if preservation flag not set. */
if(preserveCookieSubdomain == false && preserveCookieRoot == false){
var url = "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain + cookie.path;
chrome.cookies.remove({"url": url, "name": cookie.name});
console.log("Removed: " + cookie.domain + " " + url);
}
});
}

/* Function to delete cookies using the checkCookie function, and indiscriminately delete all other relevant site data. */
function clearSiteData(){
chrome.storage.local.get('configured', function(check){

/* Check if extension has been configured. This prevents the extension from removing all cookies on installation. */
if(check.configured){

/* Pass all cookies, one-by-one, to the checkCookie function. */
chrome.cookies.getAll({}, function(allCookies){
for(var i = 0; i < allCookies.length; i++){
removeCookie(allCookies[i]);
checkCookie(allCookies[i]);
}
});

/* Delete various other forms of site data, along with history and cache depending on user preference, indiscriminately. */
chrome.browsingData.remove({
"originTypes": {
"unprotectedWeb": true,
"protectedWeb": true
}
}, {
"appcache": true,
"fileSystems": true,
"indexedDB": true,
"localStorage": true,
Expand Down Expand Up @@ -91,14 +105,18 @@ function clearSiteData(){
});
}

/* Open extension options page on installation. */
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install"){
chrome.runtime.openOptionsPage();
}
});


/* Run clearSiteData function on extension toolbar icon click. */
chrome.browserAction.onClicked.addListener(function(){
clearSiteData();
});

/* Run clearSiteData function on extension load (on boot). */
clearSiteData();
20 changes: 10 additions & 10 deletions js/options.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function saveOptions(){
var subdomainArray = document.getElementById('subdomain-whitelist').value.split('\n');
subdomainArray = subdomainArray.filter(Boolean);
var domainOnlyArray = document.getElementById('domain-only-whitelist').value.split('\n');
domainOnlyArray = domainOnlyArray.filter(Boolean);
var subDomainArray = document.getElementById('subdomain-whitelist').value.split('\n');
subDomainArray = subDomainArray.filter(Boolean);
var rootDomainArray = document.getElementById('domain-only-whitelist').value.split('\n');
rootDomainArray = rootDomainArray.filter(Boolean);
var cacheOption = document.getElementById('cache_option').checked;
var historyOption = document.getElementById('history_option').checked;

chrome.storage.local.set({
subdomainWhitelist: subdomainArray,
domainOnlyWhitelist: domainOnlyArray,
subDomainWhitelist: subDomainArray,
rootDomainWhitelist: rootDomainArray,
clearCache: cacheOption,
clearHistory: historyOption,
configured: true
Expand All @@ -24,14 +24,14 @@ function saveOptions(){

function restoreOptions(){
chrome.storage.local.get({
subdomainWhitelist: [],
domainOnlyWhitelist: [],
subDomainWhitelist: [],
rootDomainWhitelist: [],
clearCache: false,
clearHistory: false,
configured: false
}, function(items){
document.getElementById('subdomain-whitelist').value = items.subdomainWhitelist.join("\n");
document.getElementById('domain-only-whitelist').value = items.domainOnlyWhitelist.join("\n");
document.getElementById('subdomain-whitelist').value = items.subDomainWhitelist.join("\n");
document.getElementById('domain-only-whitelist').value = items.rootDomainWhitelist.join("\n");
document.getElementById('cache_option').checked = items.clearCache;
document.getElementById('history_option').checked = items.clearHistory;

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Startup Cookie Destroyer",
"version": "1.0.2",
"version": "1.0.3",
"manifest_version": 2,
"description": "Remove unwanted cookies and site data on startup, and manually from the toolbar.",
"background": {
Expand Down

0 comments on commit 28f23f6

Please sign in to comment.