Add ability to trigger cookie removal on specific events

As discussed with filter list volunteers.

Related discussion:
https://github.com/uBlockOrigin/uBlock-discussions/discussions/834
This commit is contained in:
Raymond Hill 2023-11-12 10:35:28 -05:00
parent bcf809615c
commit ef311ddbec
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -2703,17 +2703,25 @@ function cookieRemover(
if ( typeof needle !== 'string' ) { return; } if ( typeof needle !== 'string' ) { return; }
const safe = safeSelf(); const safe = safeSelf();
const reName = safe.patternToRegex(needle); const reName = safe.patternToRegex(needle);
const removeCookie = function() { const extraArgs = safe.getExtraArgs(Array.from(arguments), 1);
const throttle = (fn, ms = 1000) => {
if ( throttle.timer !== undefined ) { return; }
throttle.timer = setTimeout(( ) => {
throttle.timer = undefined;
fn();
}, ms);
};
const removeCookie = ( ) => {
document.cookie.split(';').forEach(cookieStr => { document.cookie.split(';').forEach(cookieStr => {
let pos = cookieStr.indexOf('='); const pos = cookieStr.indexOf('=');
if ( pos === -1 ) { return; } if ( pos === -1 ) { return; }
let cookieName = cookieStr.slice(0, pos).trim(); const cookieName = cookieStr.slice(0, pos).trim();
if ( !reName.test(cookieName) ) { return; } if ( reName.test(cookieName) === false ) { return; }
let part1 = cookieName + '='; const part1 = cookieName + '=';
let part2a = '; domain=' + document.location.hostname; const part2a = '; domain=' + document.location.hostname;
let part2b = '; domain=.' + document.location.hostname; const part2b = '; domain=.' + document.location.hostname;
let part2c, part2d; let part2c, part2d;
let domain = document.domain; const domain = document.domain;
if ( domain ) { if ( domain ) {
if ( domain !== document.location.hostname ) { if ( domain !== document.location.hostname ) {
part2c = '; domain=.' + domain; part2c = '; domain=.' + domain;
@ -2722,8 +2730,8 @@ function cookieRemover(
part2d = '; domain=' + domain.replace('www', ''); part2d = '; domain=' + domain.replace('www', '');
} }
} }
let part3 = '; path=/'; const part3 = '; path=/';
let part4 = '; Max-Age=-1000; expires=Thu, 01 Jan 1970 00:00:00 GMT'; const part4 = '; Max-Age=-1000; expires=Thu, 01 Jan 1970 00:00:00 GMT';
document.cookie = part1 + part4; document.cookie = part1 + part4;
document.cookie = part1 + part2a + part4; document.cookie = part1 + part2a + part4;
document.cookie = part1 + part2b + part4; document.cookie = part1 + part2b + part4;
@ -2738,6 +2746,11 @@ function cookieRemover(
} }
}); });
}; };
if ( extraArgs.when === 'scroll' ) {
document.addEventListener('scroll', ( ) => {
throttle(removeCookie);
}, { passive: true });
}
removeCookie(); removeCookie();
window.addEventListener('beforeunload', removeCookie); window.addEventListener('beforeunload', removeCookie);
} }