1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-04 08:37:11 +02:00

Add dontOverwrite vararg to (trusted-)set-cookie scriptlet

When the vararg `, dontOverwrite, 1` is present, the scriptlet will
not modify the cookie if it already exists.

Related discussion:
https://github.com/uBlockOrigin/uAssets/issues/19976#issuecomment-1773466740
This commit is contained in:
Raymond Hill 2023-10-20 21:38:54 -04:00
parent 009c572cb0
commit 607bba6eaf
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -748,17 +748,18 @@ function setCookieHelper(
path = '', path = '',
options = {}, options = {},
) { ) {
const cookieExists = (name, value) => { const getCookieValue = name => {
return document.cookie.split(/\s*;\s*/).some(s => { for ( const s of document.cookie.split(/\s*;\s*/) ) {
const pos = s.indexOf('='); const pos = s.indexOf('=');
if ( pos === -1 ) { return false; } if ( pos === -1 ) { continue; }
if ( s.slice(0, pos) !== name ) { return false; } if ( s.slice(0, pos) !== name ) { continue; }
if ( s.slice(pos+1) !== value ) { return false; } return s.slice(pos+1);
return true; }
});
}; };
if ( options.reload && cookieExists(name, value) ) { return; } const cookieBefore = getCookieValue(name);
if ( cookieBefore !== undefined && options.dontOverwrite ) { return; }
if ( cookieBefore === value && options.reload ) { return; }
const cookieParts = [ name, '=', value ]; const cookieParts = [ name, '=', value ];
if ( expires !== '' ) { if ( expires !== '' ) {
@ -773,7 +774,7 @@ function setCookieHelper(
} }
document.cookie = cookieParts.join(''); document.cookie = cookieParts.join('');
if ( options.reload && cookieExists(name, value) ) { if ( options.reload && getCookieValue(name) === value ) {
window.location.reload(); window.location.reload();
} }
} }