From d7c8e703956c8a8c6caa636770d272b0bb53335f Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 7 Jul 2024 01:09:58 +0000 Subject: [PATCH 1/6] Separate the cookie add/modify actions in dropdown. --- popup/config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/popup/config.js b/popup/config.js index 832f7fa..3bd1a2f 100644 --- a/popup/config.js +++ b/popup/config.js @@ -190,7 +190,8 @@ function appendLine(url_contains, action, header_name, header_value, comment, ap if (!useManifestV3) { // Not available in Manifest V3 html += ` - + + `; } html += ` From 42caea44215d0398e662ab2e3b2b517ec7aa74ae Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 7 Jul 2024 01:23:48 +0000 Subject: [PATCH 2/6] Separate add/modify cookie actions Actions are separated to allow users to have a choice to either: - only add a cookie to the header if it does not exist yet. - only modify a cookie in the header if it exists. - add a cookie if it doesn't exist (rule 1) and modify a cookie if it exists (rule 2). It allows more control over cookies. --- background.js | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/background.js b/background.js index 271a1f4..faed185 100644 --- a/background.js +++ b/background.js @@ -230,7 +230,7 @@ function rewriteRequestHeader(e) { 'Delete request header : name=' + to_modify.header_name.toLowerCase() + ' for url ' + e.url ); } - } else if (to_modify.action === 'cookie_add_or_modify') { + } else if (to_modify.action === 'cookie_add') { let header_cookie = e.requestHeaders.find((header) => header.name.toLowerCase() === 'cookie'); let new_cookie = cookie_keyvalues_set( header_cookie === undefined ? '' : header_cookie.value, @@ -241,16 +241,24 @@ function rewriteRequestHeader(e) { e.requestHeaders.push({name: 'Cookie', value: new_cookie}); if (config.debug_mode) log( - 'cookie_add_or_modify.req new_header : name=Cookie,value=' + + 'cookie_add.req new_header : name=Cookie,value=' + new_cookie + ' for url ' + e.url ); - } else { + } + } else if (to_modify.action === 'cookie_modify') { + let header_cookie = e.requestHeaders.find((header) => header.name.toLowerCase() === 'cookie'); + let new_cookie = cookie_keyvalues_set( + header_cookie === undefined ? '' : header_cookie.value, + to_modify.header_name, + to_modify.header_value + ); + if (header_cookie != undefined) { header_cookie.value = new_cookie; if (config.debug_mode) log( - 'cookie_add_or_modify.req modify_header : name=Cookie,value=' + + 'cookie_modify.req modify_header : name=Cookie,value=' + new_cookie + ' for url ' + e.url @@ -346,7 +354,7 @@ function rewriteResponseHeader(e) { e.url ); } - } else if (to_modify.action === 'cookie_add_or_modify') { + } else if (to_modify.action === 'cookie_add') { let header_cookie = e.responseHeaders.find( (header) => header.name.toLowerCase() === 'set-cookie' && @@ -362,21 +370,36 @@ function rewriteResponseHeader(e) { ); if (header_cookie === undefined) { log( - "SimpleModifyHeaders.Warning: you're using cookie_add_or_modify in Response. While adding new cookie in response, this plugin only generates `Set-Cookie: cookie-name=cookie-value `, without ANY additional attributes. Add a `Set-Cookie` header if you need them. " + "SimpleModifyHeaders.Warning: you're using cookie_add in Response. While adding new cookie in response, this plugin only generates `Set-Cookie: cookie-name=cookie-value `, without ANY additional attributes. Add a `Set-Cookie` header if you need them. " ); e.responseHeaders.push({name: 'Set-Cookie', value: new_header_value}); if (config.debug_mode) log( - 'cookie_add_or_modify.resp new_header : name=Cookie,value=' + + 'cookie_add.resp new_header : name=Cookie,value=' + new_header_value + ' for url ' + e.url ); - } else { + } + } else if (to_modify.action === 'cookie_modify') { + let header_cookie = e.responseHeaders.find( + (header) => + header.name.toLowerCase() === 'set-cookie' && + header.value + .toLowerCase() + .trim() + .startsWith(to_modify.header_name.toLowerCase() + '=') + ); + let new_header_value = set_cookie_modify_cookie_value( + header_cookie === undefined ? '' : header_cookie.value, + to_modify.header_name, + to_modify.header_value + ); + if (header_cookie != undefined) header_cookie.value = new_header_value; if (config.debug_mode) log( - 'cookie_add_or_modify.resp modify_header : name=Cookie,value=' + + 'cookie_modify.resp modify_header : name=Cookie,value=' + new_header_value + ' for url ' + e.url From e2c5966672bc404bb4d1feff242e9ce0b4f81028 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 7 Jul 2024 01:25:11 +0000 Subject: [PATCH 3/6] Update and rename manifestV3..json to manifestV3.json Seems like a filename mistake. --- manifestV3..json => manifestV3.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename manifestV3..json => manifestV3.json (95%) diff --git a/manifestV3..json b/manifestV3.json similarity index 95% rename from manifestV3..json rename to manifestV3.json index 65f94a9..3d95ecd 100644 --- a/manifestV3..json +++ b/manifestV3.json @@ -25,4 +25,4 @@ "background": { "service_worker": "service-worker.js" } -} \ No newline at end of file +} From 23b7bfa7d0cb840b26bbb30eda143ef09b009ab6 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 7 Jul 2024 01:34:24 +0000 Subject: [PATCH 4/6] Update config.js indent issue --- popup/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/popup/config.js b/popup/config.js index 3bd1a2f..303442c 100644 --- a/popup/config.js +++ b/popup/config.js @@ -191,7 +191,7 @@ function appendLine(url_contains, action, header_name, header_value, comment, ap // Not available in Manifest V3 html += ` - + `; } html += ` From 812a92698ad23040769765327bf13ccf306dcbd3 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 7 Jul 2024 01:59:49 +0000 Subject: [PATCH 5/6] F-up in background.js --- background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/background.js b/background.js index faed185..034e7be 100644 --- a/background.js +++ b/background.js @@ -395,7 +395,7 @@ function rewriteResponseHeader(e) { to_modify.header_name, to_modify.header_value ); - if (header_cookie != undefined) + if (header_cookie != undefined) { header_cookie.value = new_header_value; if (config.debug_mode) log( From 8e8afe8b5478a7bfb343d022e56e9256058c7350 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 7 Jul 2024 04:33:50 +0000 Subject: [PATCH 6/6] Add cookie key/value when Cookie header is present, but key is missing --- background.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/background.js b/background.js index 034e7be..73203cc 100644 --- a/background.js +++ b/background.js @@ -116,6 +116,12 @@ function storeInBrowserStorage(item, callback_function) { chrome.storage.local.set(item, callback_function); } +function hasCookieKey(cookie, key) { + let cookieArray = cookie.value.split(';').filter((e) => e.trim().length > 0); + let cookieKeyIndex = cookieArray.findIndex((kv) => kv.trim().startsWith(key + '=')); + return cookieKeyIndex !== -1; +} + /* * This function set a key-value pair in HTTP header "Cookie", * and returns the value of HTTP header after modification. @@ -246,6 +252,15 @@ function rewriteRequestHeader(e) { ' for url ' + e.url ); + } else if ( ! hasCookieKey(header_cookie, to_modify.header_name)) { + header_cookie.value = new_cookie; + if (config.debug_mode) + log( + 'cookie_modify.req modify_header : name=Cookie,value=' + + new_cookie + + ' for url ' + + e.url + ); } } else if (to_modify.action === 'cookie_modify') { let header_cookie = e.requestHeaders.find((header) => header.name.toLowerCase() === 'cookie');