Skip to content

Commit

Permalink
Add dontOverwrite vararg to (trusted-)set-cookie scriptlet
Browse files Browse the repository at this point in the history
When the vararg `, dontOverwrite, 1` is present, the scriptlet will
not modify the cookie if it already exists.

Related discussion:
uBlockOrigin/uAssets#19976 (comment)
  • Loading branch information
gorhill committed Oct 21, 2023
1 parent 009c572 commit 607bba6
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions assets/resources/scriptlets.js
Expand Up @@ -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 !== '' ) {
Expand All @@ -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();
}
}
Expand Down

2 comments on commit 607bba6

@peace2000
Copy link
Contributor

@peace2000 peace2000 commented on 607bba6 Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gorhill , so the usage will be like:

youtube.com##+js(trusted-set-cookie, SOCS, CAISNQgDEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjMwODI5LjA3X3AxGgJlbiADGgYIgJnPpwY, , , reload, 1, dontOverwrite, 1)

or:

youtube.com##+js(trusted-set-cookie, SOCS, CAISNQgDEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjMwODI5LjA3X3AxGgJlbiADGgYIgJnPpwY, , , , , dontOverwrite, 1)

?

@gorhill
Copy link
Owner Author

@gorhill gorhill commented on 607bba6 Oct 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trusted-set-cookie has 4 positional arguments (not counting the name of the scriptlet), so , dontOverwrite, 1 needs to come at 5th position at the earliest, so this should work:

youtube.com##+js(trusted-set-cookie, SOCS, CAISNQgDEitib3FfaWRlbnRpdHlmcm9udGVuZHVpc2VydmVyXzIwMjMwODI5LjA3X3AxGgJlbiADGgYIgJnPpwY, , , dontOverwrite, 1)

Varargs can be used in any order among themselves, as long as they start after the positional arguments. So , reload, 1, dontOverwrite, 1 or , dontOverwrite, 1, reload, 1 accomplish the same thing.

Please sign in to comment.