diff --git a/src/api.js b/src/api.js index dc84ad5..ccef393 100644 --- a/src/api.js +++ b/src/api.js @@ -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) { @@ -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"); @@ -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"); diff --git a/src/cookies.js b/src/cookies.js index be9ec50..62aac19 100644 --- a/src/cookies.js +++ b/src/cookies.js @@ -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 @@ -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 @@ -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) @@ -505,7 +505,7 @@ $('#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"); } @@ -513,7 +513,7 @@ $('#domain-list').contextMenu({ "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 @@ -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 @@ -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 @@ -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, @@ -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); @@ -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) => { diff --git a/src/export.js b/src/export.js index 7e30da8..b12bb60 100644 --- a/src/export.js +++ b/src/export.js @@ -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 @@ -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); }); @@ -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); });