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:
parent
009c572cb0
commit
607bba6eaf
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user