mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
Fine tune new set-cookie
/trusted-set-cookie
scriptlet
Added support for extra parameter `reload, 1`. If present, the scriplet will force a reload of the webpage if the cookie being set was not already set. As per feedback from filter list maintainers.
This commit is contained in:
parent
9f2bfecd27
commit
112e41b9d5
@ -709,6 +709,45 @@ function objectPrune(
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
builtinScriptlets.push({
|
||||||
|
name: 'set-cookie-helper.fn',
|
||||||
|
fn: setCookieHelper,
|
||||||
|
});
|
||||||
|
function setCookieHelper(
|
||||||
|
name = '',
|
||||||
|
value = '',
|
||||||
|
expires = '',
|
||||||
|
path = '',
|
||||||
|
options = {},
|
||||||
|
) {
|
||||||
|
const cookieExists = (name, value) => {
|
||||||
|
return document.cookie.split(/\s*;\s*/).some(s => {
|
||||||
|
const pos = s.indexOf('=');
|
||||||
|
if ( pos === -1 ) { return false; }
|
||||||
|
if ( s.slice(0, pos) !== name ) { return false; }
|
||||||
|
if ( s.slice(pos+1) !== value ) { return false; }
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if ( options.reload && cookieExists(name, value) ) { return; }
|
||||||
|
|
||||||
|
const cookieParts = [ name, '=', value ];
|
||||||
|
if ( expires !== '' ) {
|
||||||
|
cookieParts.push('; expires=', expires);
|
||||||
|
}
|
||||||
|
if ( path !== '' ) {
|
||||||
|
cookieParts.push('; path=/');
|
||||||
|
}
|
||||||
|
document.cookie = cookieParts.join('');
|
||||||
|
|
||||||
|
if ( options.reload && cookieExists(name, value) ) {
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
|
|
||||||
Injectable scriptlets
|
Injectable scriptlets
|
||||||
@ -2565,13 +2604,19 @@ builtinScriptlets.push({
|
|||||||
name: 'set-cookie.js',
|
name: 'set-cookie.js',
|
||||||
fn: setCookie,
|
fn: setCookie,
|
||||||
world: 'ISOLATED',
|
world: 'ISOLATED',
|
||||||
|
dependencies: [
|
||||||
|
'get-extra-args.fn',
|
||||||
|
'set-cookie-helper.fn',
|
||||||
|
],
|
||||||
});
|
});
|
||||||
function setCookie(
|
function setCookie(
|
||||||
name = '',
|
name = '',
|
||||||
value = '',
|
value = '',
|
||||||
path = '/'
|
path = ''
|
||||||
) {
|
) {
|
||||||
if ( name === '' ) { return; }
|
if ( name === '' ) { return; }
|
||||||
|
name = encodeURIComponent(name);
|
||||||
|
|
||||||
const validValues = new Set([
|
const validValues = new Set([
|
||||||
'true', 'True',
|
'true', 'True',
|
||||||
'false', 'False',
|
'false', 'False',
|
||||||
@ -2584,16 +2629,19 @@ function setCookie(
|
|||||||
const n = parseInt(value, 10);
|
const n = parseInt(value, 10);
|
||||||
if ( n > 15 ) { return; }
|
if ( n > 15 ) { return; }
|
||||||
}
|
}
|
||||||
const validPaths = [ '/', 'none' ];
|
value = encodeURIComponent(value);
|
||||||
|
|
||||||
|
const validPaths = [ '', '/', 'none' ];
|
||||||
if ( validPaths.includes(path) === false ) { return; }
|
if ( validPaths.includes(path) === false ) { return; }
|
||||||
const cookieParts = [
|
if ( path === 'none' ) { path = ''; }
|
||||||
encodeURIComponent(name), '=',
|
|
||||||
encodeURIComponent(value),
|
setCookieHelper(
|
||||||
];
|
name,
|
||||||
if ( path !== 'none' ) {
|
value,
|
||||||
cookieParts.push('; path=/');
|
'',
|
||||||
}
|
path,
|
||||||
document.cookie = cookieParts.join('');
|
getExtraArgs(Array.from(arguments), 3)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
@ -2691,23 +2739,28 @@ builtinScriptlets.push({
|
|||||||
requiresTrust: true,
|
requiresTrust: true,
|
||||||
fn: trustedSetCookie,
|
fn: trustedSetCookie,
|
||||||
world: 'ISOLATED',
|
world: 'ISOLATED',
|
||||||
|
dependencies: [
|
||||||
|
'get-extra-args.fn',
|
||||||
|
'set-cookie-helper.fn',
|
||||||
|
],
|
||||||
});
|
});
|
||||||
function trustedSetCookie(
|
function trustedSetCookie(
|
||||||
name = '',
|
name = '',
|
||||||
value = '',
|
value = '',
|
||||||
offsetExpiresSec = '',
|
offsetExpiresSec = '',
|
||||||
path = '/'
|
path = ''
|
||||||
) {
|
) {
|
||||||
if ( name === '' ) { return; }
|
if ( name === '' ) { return; }
|
||||||
|
|
||||||
const time = new Date();
|
const time = new Date();
|
||||||
|
|
||||||
if ( value === '$now$' ) {
|
if ( value === '$now$' ) {
|
||||||
value = Date.now();
|
value = Date.now();
|
||||||
} else if ( value === '$currentDate$' ) {
|
} else if ( value === '$currentDate$' ) {
|
||||||
value = time.toUTCString();
|
value = time.toUTCString();
|
||||||
}
|
}
|
||||||
const validPaths = [ '/', 'none' ];
|
|
||||||
if ( validPaths.includes(path) === false ) { return; }
|
let expires = '';
|
||||||
const cookieParts = [ name, '=', value ];
|
|
||||||
if ( offsetExpiresSec !== '' ) {
|
if ( offsetExpiresSec !== '' ) {
|
||||||
if ( offsetExpiresSec === '1day' ) {
|
if ( offsetExpiresSec === '1day' ) {
|
||||||
time.setDate(time.getDate() + 1);
|
time.setDate(time.getDate() + 1);
|
||||||
@ -2717,12 +2770,20 @@ function trustedSetCookie(
|
|||||||
if ( /^\d+$/.test(offsetExpiresSec) === false ) { return; }
|
if ( /^\d+$/.test(offsetExpiresSec) === false ) { return; }
|
||||||
time.setSeconds(time.getSeconds() + parseInt(offsetExpiresSec, 10));
|
time.setSeconds(time.getSeconds() + parseInt(offsetExpiresSec, 10));
|
||||||
}
|
}
|
||||||
cookieParts.push('; expires=', time.toUTCString());
|
expires = time.toUTCString();
|
||||||
}
|
}
|
||||||
if ( path !== 'none' ) {
|
|
||||||
cookieParts.push('; path=/');
|
const validPaths = [ '', '/', 'none' ];
|
||||||
}
|
if ( validPaths.includes(path) === false ) { return; }
|
||||||
document.cookie = cookieParts.join('');
|
if ( path === 'none' ) { path = ''; }
|
||||||
|
|
||||||
|
setCookieHelper(
|
||||||
|
name,
|
||||||
|
value,
|
||||||
|
expires,
|
||||||
|
path,
|
||||||
|
getExtraArgs(Array.from(arguments), 4)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
Loading…
Reference in New Issue
Block a user