Improve remove-cookie scriptlet

Related discussion:
afc6ff52ff (commitcomment-160172503)
This commit is contained in:
Raymond Hill 2025-06-16 15:19:01 -04:00
parent 33b92f91cb
commit 0a8ea58bb7
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -137,8 +137,9 @@ export function setCookieFn(
if ( options.domain ) {
let domain = options.domain;
if ( /^\/.+\//.test(domain) ) {
const baseURL = new URL(document.baseURI);
const reDomain = new RegExp(domain.slice(1, -1));
const match = reDomain.exec(document.location.hostname);
const match = reDomain.exec(baseURL.hostname);
domain = match ? match[0] : undefined;
}
if ( domain ) {
@ -369,6 +370,13 @@ export function removeCookie(
fn();
}, ms);
};
const baseURL = new URL(document.baseURI);
let targetDomain = extraArgs.domain;
if ( targetDomain && /^\/.+\//.test(targetDomain) ) {
const reDomain = new RegExp(targetDomain.slice(1, -1));
const match = reDomain.exec(baseURL.hostname);
targetDomain = match ? match[0] : undefined;
}
const remove = ( ) => {
safe.String_split.call(document.cookie, ';').forEach(cookieStr => {
const pos = cookieStr.indexOf('=');
@ -376,16 +384,19 @@ export function removeCookie(
const cookieName = cookieStr.slice(0, pos).trim();
if ( reName.test(cookieName) === false ) { return; }
const part1 = cookieName + '=';
const part2a = '; domain=' + document.location.hostname;
const part2b = '; domain=.' + document.location.hostname;
const part2a = `; domain=${baseURL.hostname}`;
const part2b = `; domain=.${baseURL.hostname}`;
let part2c, part2d;
const domain = document.domain;
if ( domain ) {
if ( domain !== document.location.hostname ) {
part2c = '; domain=.' + domain;
if ( targetDomain ) {
part2c = `; domain=${targetDomain}`;
part2d = `; domain=.${targetDomain}`;
} else if ( document.domain ) {
const domain = document.domain;
if ( domain !== baseURL.hostname ) {
part2c = `; domain=.${domain}`;
}
if ( domain.startsWith('www.') ) {
part2d = '; domain=' + domain.replace('www', '');
part2d = `; domain=${domain.replace('www', '')}`;
}
}
const part3 = '; path=/';