mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +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 = '',
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user