Skip to content

Commit 0a8ea58

Browse files
committed
Improve remove-cookie scriptlet
Related discussion: uBlockOrigin/uAssets@afc6ff5#commitcomment-160172503
1 parent 33b92f9 commit 0a8ea58

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/js/resources/cookie.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ export function setCookieFn(
137137
if ( options.domain ) {
138138
let domain = options.domain;
139139
if ( /^\/.+\//.test(domain) ) {
140+
const baseURL = new URL(document.baseURI);
140141
const reDomain = new RegExp(domain.slice(1, -1));
141-
const match = reDomain.exec(document.location.hostname);
142+
const match = reDomain.exec(baseURL.hostname);
142143
domain = match ? match[0] : undefined;
143144
}
144145
if ( domain ) {
@@ -369,23 +370,33 @@ export function removeCookie(
369370
fn();
370371
}, ms);
371372
};
373+
const baseURL = new URL(document.baseURI);
374+
let targetDomain = extraArgs.domain;
375+
if ( targetDomain && /^\/.+\//.test(targetDomain) ) {
376+
const reDomain = new RegExp(targetDomain.slice(1, -1));
377+
const match = reDomain.exec(baseURL.hostname);
378+
targetDomain = match ? match[0] : undefined;
379+
}
372380
const remove = ( ) => {
373381
safe.String_split.call(document.cookie, ';').forEach(cookieStr => {
374382
const pos = cookieStr.indexOf('=');
375383
if ( pos === -1 ) { return; }
376384
const cookieName = cookieStr.slice(0, pos).trim();
377385
if ( reName.test(cookieName) === false ) { return; }
378386
const part1 = cookieName + '=';
379-
const part2a = '; domain=' + document.location.hostname;
380-
const part2b = '; domain=.' + document.location.hostname;
387+
const part2a = `; domain=${baseURL.hostname}`;
388+
const part2b = `; domain=.${baseURL.hostname}`;
381389
let part2c, part2d;
382-
const domain = document.domain;
383-
if ( domain ) {
384-
if ( domain !== document.location.hostname ) {
385-
part2c = '; domain=.' + domain;
390+
if ( targetDomain ) {
391+
part2c = `; domain=${targetDomain}`;
392+
part2d = `; domain=.${targetDomain}`;
393+
} else if ( document.domain ) {
394+
const domain = document.domain;
395+
if ( domain !== baseURL.hostname ) {
396+
part2c = `; domain=.${domain}`;
386397
}
387398
if ( domain.startsWith('www.') ) {
388-
part2d = '; domain=' + domain.replace('www', '');
399+
part2d = `; domain=${domain.replace('www', '')}`;
389400
}
390401
}
391402
const part3 = '; path=/';

0 commit comments

Comments
 (0)