Skip to content

Commit

Permalink
filter_cookies() is now a promise ; Fix #44
Browse files Browse the repository at this point in the history
=> allows independence of get_all_cookies and getCookiesFromSelectedDomain
  • Loading branch information
ysard committed Nov 11, 2018
1 parent cc9b51a commit cd0a672
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 58 deletions.
91 changes: 47 additions & 44 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,55 +114,62 @@ vAPI.parse_search_query = function(search_query) {
vAPI.query_values = values;
}

vAPI.filter_cookies = function(cookies, names, values) {
/* Filter cookies on their names and values
vAPI.filter_cookies = function(promise) {
/* Promise to filter cookies on their names and values
* Return a cookie list satisfying the search conditions
*
* This promise is used with get_all_cookies()
* and getCookiesFromSelectedDomain()
*
* This promise uses vAPI.query_names and vAPI.query_values set by vAPI.parse_search_query
*
* Multiple name filters are linked by OR operator.
* Multiple value filters are linked by OR operator.
* Groups of name filters are linked with groups of value filters by a AND operator.
*
* Ex: ("name1" OR "name2") AND ("value1", "value2")
*
* TODO: Transform this function in a promise that encapsulates get_all_cookies()
* and getCookiesFromSelectedDomain()
* => Move the filtering code in a separate place
*/

// No filter => return the list of cookies unchanged
if (!names.length && !values.length)
return cookies;

//console.log("filter_cookies: cookie to filter", cookies.length);

let filtered_cookies = [];
let name_found = false;
let value_found = false;
for (let cookie of cookies) {

for (let name of names)
if (cookie.name.indexOf(name) !== -1)
// name is found => keep the cookie
name_found = true;

for (let value of values)
if (cookie.value.indexOf(value) !== -1)
// value is found => keep the cookie
value_found = true;

if ((value_found && name_found) || ( // value and name found in the same cookie
(value_found && !names.length) || // value found with no queried name
(name_found && !values.length) // name found with no queried value
)
) {
//console.log("filter_cookies: kept:", cookie.domain, cookie.name, cookie.value);
filtered_cookies.push(cookie);
}
if (!vAPI.query_names.length && !vAPI.query_values.length)
return promise;

name_found = false;
value_found = false;
}
return filtered_cookies;
return new Promise((resolve, reject) => {
promise.then((cookies) => {

//console.log("filter_cookies: cookie to filter", cookies.length);

let filtered_cookies = [];
let name_found = false;
let value_found = false;
for (let cookie of cookies) {

for (let name of vAPI.query_names)
if (cookie.name.indexOf(name) !== -1)
// name is found => keep the cookie
name_found = true;

for (let value of vAPI.query_values)
if (cookie.value.indexOf(value) !== -1)
// value is found => keep the cookie
value_found = true;

if ((value_found && name_found) || ( // value and name found in the same cookie
(value_found && !vAPI.query_names.length) || // value found with no queried name
(name_found && !vAPI.query_values.length) // name found with no queried value
)
) {
//console.log("filter_cookies: kept:", cookie.domain, cookie.name, cookie.value);
filtered_cookies.push(cookie);
}

name_found = false;
value_found = false;
}
resolve(filtered_cookies);
})
.catch(err => console.error(err));
});
}

vAPI.get_all_cookies = function(storeIds) {
Expand Down Expand Up @@ -223,9 +230,7 @@ vAPI.get_all_cookies = function(storeIds) {
filtered_cookies.push(cookie);
}
}
// Filtering on names and values
filtered_cookies = vAPI.filter_cookies(filtered_cookies, vAPI.query_names, vAPI.query_values);
//console.log("get_all_cookies: filtering:", filtered_cookies.length);
//console.log("get_all_cookies: nb:", filtered_cookies.length);
resolve(filtered_cookies);
} else
reject("all_cookies-NoCookies");
Expand Down Expand Up @@ -551,9 +556,7 @@ vAPI.getCookiesFromSelectedDomain = function() {
filtered_cookies.push(cookie);
}
}
// Filtering on names and values
filtered_cookies = vAPI.filter_cookies(filtered_cookies, vAPI.query_names, vAPI.query_values);
//console.log("getCookiesFromSelectedDomain: filtering", filtered_cookies.length);
//console.log("getCookiesFromSelectedDomain: nb", filtered_cookies.length);
resolve(filtered_cookies);
} else {
reject("SelectedDomain-NoCookies");
Expand Down
20 changes: 10 additions & 10 deletions src/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ $('#button_optimal_size').click(function() {

$("#protect_all_button").click(function() {
// Get all cookies for this store and protect them
let promise = vAPI.get_all_cookies([$('#search_store').val()]);
let promise = vAPI.filter_cookies(vAPI.get_all_cookies([$('#search_store').val()]));
promise.then((cookies) => {
vAPI.set_cookie_protection(cookies, true).then(() => {
// Update the UI
Expand All @@ -403,7 +403,7 @@ $("#protect_all_button").click(function() {

$("#unprotect_all_button").click(function() {
// Get all cookies for this store and unprotect them
let promise = vAPI.get_all_cookies([$('#search_store').val()]);
let promise = vAPI.filter_cookies(vAPI.get_all_cookies([$('#search_store').val()]));
promise.then((cookies) => {
vAPI.set_cookie_protection(cookies, false).then(() => {
// Update the UI
Expand Down Expand Up @@ -494,7 +494,7 @@ $('#domain-list').contextMenu({
text: $(this).text()
});*/

let promise = vAPI.getCookiesFromSelectedDomain();
let promise = vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain());
vAPI.copy_cookies_to_store(promise, options).then((ret) => {
// Simulate click on the same domain with recalculation of badges
// (because almost 1 new cookie is added, with maybe a new container)
Expand All @@ -505,15 +505,15 @@ $('#domain-list').contextMenu({
"copy": {name: browser.i18n.getMessage("contextMenu_domain_copy2Clipboard"), icon: "copy",
callback: function(itemKey, opt, rootMenu, originalEvent) {
// Export to clipboard all cookies in the selected domain
let promise = vAPI.getCookiesFromSelectedDomain();
let promise = vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain());
window.display_json_in_clipboard_area(promise);
$('#modal_clipboard').modal("show");
}
},
"save": {name: browser.i18n.getMessage("contextMenu_domain_copy2File"), icon: "save",
callback: function(itemKey, opt, rootMenu, originalEvent) {
// Export to file all cookies in the selected domain
let promise = vAPI.getCookiesFromSelectedDomain();
let promise = vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain());
promise.then((cookies) => {
// Make 1 json for each cookie and store it
// Merge and display templates
Expand All @@ -527,7 +527,7 @@ $('#domain-list').contextMenu({
"protect": {name: browser.i18n.getMessage("contextMenu_domain_protect"), icon: "lock",
callback: function(itemKey, opt, rootMenu, originalEvent) {
// Protect all cookies in the selected domain
let promise = vAPI.getCookiesFromSelectedDomain();
let promise = vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain());
promise.then((cookies) => {
vAPI.set_cookie_protection(cookies, true).then(() => {
// Update the UI
Expand All @@ -539,7 +539,7 @@ $('#domain-list').contextMenu({
"unprotect": {name: browser.i18n.getMessage("contextMenu_domain_unprotect"), icon: "unlock",
callback: function(itemKey, opt, rootMenu, originalEvent) {
// Unprotect all cookies in the selected domain
let promise = vAPI.getCookiesFromSelectedDomain();
let promise = vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain());
promise.then((cookies) => {
vAPI.set_cookie_protection(cookies, false).then(() => {
// Update the UI
Expand All @@ -552,7 +552,7 @@ $('#domain-list').contextMenu({
callback: function(itemKey, opt, rootMenu, originalEvent) {
// Remove all cookies in the selected domain
// TODO #delete_domain_button n'existe plus
delete_cookies(vAPI.getCookiesFromSelectedDomain(), "#delete_domain_button span");
delete_cookies(vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain()), "#delete_domain_button span");
}
},
"contexts_selector": context_menu_elements,
Expand Down Expand Up @@ -1068,7 +1068,7 @@ function showDomains(storeIds) {
if (searched_store != 'all')
storeIds = [searched_store];

vAPI.get_all_cookies(storeIds).then((cookies) => {
vAPI.filter_cookies(vAPI.get_all_cookies(storeIds)).then((cookies) => {

// Get dict of domains with number of cookies + cookieStore ids
var domains = uniqueDomains(cookies);
Expand Down Expand Up @@ -1165,7 +1165,7 @@ function showCookiesList(event, refresh_domain_badges) {
// Get 1 promise for each cookie store
// Each promise stores all associated cookies
// NOTE: On FF62- and FF59+=, the promise simply returns the content of vAPI.get_all_cookies(storeIds)
let promise = vAPI.getCookiesFromSelectedDomain();
let promise = vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain());
// Merge all promises
promise.then((cookies) => {

Expand Down
8 changes: 4 additions & 4 deletions src/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $("#file_cookie_export").click(function() {

$("#file_domain_export").click(function() {
// Build 1 json template for each cookie for the selected domain
let promise = vAPI.getCookiesFromSelectedDomain();
let promise = vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain());
promise.then((cookies) => {
// Make 1 json for each cookie and store it
// Merge and display templates
Expand All @@ -63,7 +63,7 @@ $("#file_domain_export").click(function() {

$("#file_all_export").click(function() {
// Build 1 json template for each cookie in all stores
let promise = vAPI.get_all_cookies([$('#search_store').val()]);
let promise = vAPI.filter_cookies(vAPI.get_all_cookies([$('#search_store').val()]));
promise.then((cookies) => {
export_content_to_file_wrapper(cookies);
});
Expand All @@ -79,13 +79,13 @@ $("#clipboard_cookie_export").click(function() {

$("#clipboard_domain_export").click(function() {
// Build 1 json template for each cookie for the selected domain
let promise = vAPI.getCookiesFromSelectedDomain();
let promise = vAPI.filter_cookies(vAPI.getCookiesFromSelectedDomain());
display_json_in_clipboard_area(promise);
});

$("#clipboard_all_export").click(function() {
// Build 1 json template for each cookie in all stores
let promise = vAPI.get_all_cookies([$('#search_store').val()]);
let promise = vAPI.filter_cookies(vAPI.get_all_cookies([$('#search_store').val()]));
display_json_in_clipboard_area(promise);
});

Expand Down

0 comments on commit cd0a672

Please sign in to comment.