1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +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 = '',
options = {},
) {
const cookieExists = (name, value) => {
return document.cookie.split(/\s*;\s*/).some(s => {
const getCookieValue = name => {
for ( const s of document.cookie.split(/\s*;\s*/) ) {
const pos = s.indexOf('=');
if ( pos === -1 ) { return false; }
if ( s.slice(0, pos) !== name ) { return false; }
if ( s.slice(pos+1) !== value ) { return false; }
return true;
});
if ( pos === -1 ) { continue; }
if ( s.slice(0, pos) !== name ) { continue; }
return s.slice(pos+1);
}
};
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 ];
if ( expires !== '' ) {
@ -773,7 +774,7 @@ function setCookieHelper(
}
document.cookie = cookieParts.join('');
if ( options.reload && cookieExists(name, value) ) {
if ( options.reload && getCookieValue(name) === value ) {
window.location.reload();
}
}