From ef311ddbec9e50e032cfce062bd6c69aed5ae977 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 12 Nov 2023 10:35:28 -0500 Subject: [PATCH] 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 --- assets/resources/scriptlets.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index e162932e7c6a1..319ef5625af66 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -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; @@ -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; @@ -2738,6 +2746,11 @@ function cookieRemover( } }); }; + if ( extraArgs.when === 'scroll' ) { + document.addEventListener('scroll', ( ) => { + throttle(removeCookie); + }, { passive: true }); + } removeCookie(); window.addEventListener('beforeunload', removeCookie); }