Skip to content

Commit

Permalink
Add ability to trigger cookie removal on specific events
Browse files Browse the repository at this point in the history
As discussed with filter list volunteers.

Related discussion:
uBlockOrigin/uBlock-discussions#834
  • Loading branch information
gorhill committed Nov 12, 2023
1 parent bcf8096 commit ef311dd
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2703,17 +2703,25 @@ function cookieRemover(
if ( typeof needle !== 'string' ) { return; }
const safe = safeSelf();
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 => {
let pos = cookieStr.indexOf('=');
const pos = cookieStr.indexOf('=');
if ( pos === -1 ) { return; }
let cookieName = cookieStr.slice(0, pos).trim();
if ( !reName.test(cookieName) ) { return; }
let part1 = cookieName + '=';
let part2a = '; domain=' + document.location.hostname;
let part2b = '; domain=.' + document.location.hostname;
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;
let part2c, part2d;
let domain = document.domain;
const domain = document.domain;
if ( domain ) {
if ( domain !== document.location.hostname ) {
part2c = '; domain=.' + domain;
Expand All @@ -2722,8 +2730,8 @@ function cookieRemover(
part2d = '; domain=' + domain.replace('www', '');
}
}
let part3 = '; path=/';
let part4 = '; Max-Age=-1000; expires=Thu, 01 Jan 1970 00:00:00 GMT';
const part3 = '; path=/';
const part4 = '; Max-Age=-1000; expires=Thu, 01 Jan 1970 00:00:00 GMT';
document.cookie = part1 + part4;
document.cookie = part1 + part2a + part4;
document.cookie = part1 + part2b + part4;
Expand All @@ -2738,6 +2746,11 @@ function cookieRemover(
}
});
};
if ( extraArgs.when === 'scroll' ) {
document.addEventListener('scroll', ( ) => {
throttle(removeCookie);
}, { passive: true });
}
removeCookie();
window.addEventListener('beforeunload', removeCookie);
}
Expand Down

0 comments on commit ef311dd

Please sign in to comment.