1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-04 16:47:15 +02:00

Add trusted-set-cookie scriptlet

This new scriptlet is only valid when used in a trusted lists.

Implementation follows:
https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/trusted-set-cookie.js
This commit is contained in:
Raymond Hill 2023-06-15 19:57:10 -04:00
parent f04f13e855
commit eaea26b5e9
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -1258,6 +1258,10 @@ function noFetchIf(
builtinScriptlets.push({ builtinScriptlets.push({
name: 'refresh-defuser.js', name: 'refresh-defuser.js',
fn: refreshDefuser, fn: refreshDefuser,
world: 'ISOLATED',
dependencies: [
'run-at.fn',
],
}); });
// https://www.reddit.com/r/uBlockOrigin/comments/q0frv0/while_reading_a_sports_article_i_was_redirected/hf7wo9v/ // https://www.reddit.com/r/uBlockOrigin/comments/q0frv0/while_reading_a_sports_article_i_was_redirected/hf7wo9v/
function refreshDefuser( function refreshDefuser(
@ -1273,11 +1277,9 @@ function refreshDefuser(
const ms = Math.max(parseFloat(s) || 0, 0) * 1000; const ms = Math.max(parseFloat(s) || 0, 0) * 1000;
setTimeout(( ) => { window.stop(); }, ms); setTimeout(( ) => { window.stop(); }, ms);
}; };
if ( document.readyState === 'loading' ) { runAt(( ) => {
document.addEventListener('DOMContentLoaded', defuse, { once: true });
} else {
defuse(); defuse();
} }, 'interactive');
} }
/******************************************************************************/ /******************************************************************************/
@ -1286,6 +1288,9 @@ builtinScriptlets.push({
name: 'remove-attr.js', name: 'remove-attr.js',
aliases: [ 'ra.js' ], aliases: [ 'ra.js' ],
fn: removeAttr, fn: removeAttr,
dependencies: [
'run-at.fn',
],
}); });
function removeAttr( function removeAttr(
token = '', token = '',
@ -1338,13 +1343,9 @@ function removeAttr(
subtree: true, subtree: true,
}); });
}; };
if ( document.readyState !== 'complete' && /\bcomplete\b/.test(behavior) ) { runAt(( ) => {
self.addEventListener('load', start, { once: true });
} else if ( document.readyState !== 'loading' || /\basap\b/.test(behavior) ) {
start(); start();
} else { }, /\bcomplete\b/.test(behavior) ? 'idle' : 'interactive');
self.addEventListener('DOMContentLoaded', start, { once: true });
}
} }
/******************************************************************************/ /******************************************************************************/
@ -1353,6 +1354,9 @@ builtinScriptlets.push({
name: 'remove-class.js', name: 'remove-class.js',
aliases: [ 'rc.js' ], aliases: [ 'rc.js' ],
fn: removeClass, fn: removeClass,
dependencies: [
'run-at.fn',
],
}); });
function removeClass( function removeClass(
token = '', token = '',
@ -1403,13 +1407,9 @@ function removeClass(
subtree: true, subtree: true,
}); });
}; };
if ( document.readyState !== 'complete' && /\bcomplete\b/.test(behavior) ) { runAt(( ) => {
self.addEventListener('load', start, { once: true });
} else if ( document.readyState === 'loading' ) {
self.addEventListener('DOMContentLoaded', start, { once: true });
} else {
start(); start();
} }, /\bcomplete\b/.test(behavior) ? 'idle' : 'interactive');
} }
/******************************************************************************/ /******************************************************************************/
@ -2550,6 +2550,52 @@ function removeNodeText(
replaceNodeTextCore(nodeName, '', '', 'condition', condition || '', ...extraArgs); replaceNodeTextCore(nodeName, '', '', 'condition', condition || '', ...extraArgs);
} }
/*******************************************************************************
*
* set-cookie.js
*
* Set specified cookie to a specific value.
*
* Reference:
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js
*
**/
builtinScriptlets.push({
name: 'set-cookie.js',
fn: setCookie,
world: 'ISOLATED',
});
function setCookie(
name = '',
value = '',
path = '/'
) {
if ( name === '' ) { return; }
const validValues = new Set([
'true', 'True',
'false', 'False',
'yes', 'Yes', 'y', 'Y',
'no', 'No', 'n', 'N',
'ok', 'OK',
]);
if ( validValues.has(value) === false ) {
if ( /^\d+$/.test(value) === false ) { return; }
const n = parseInt(value, 10);
if ( n > 15 ) { return; }
}
const validPaths = [ '/', 'none' ];
if ( validPaths.includes(path) === false ) { return; }
const cookieParts = [
encodeURIComponent(name), '=',
encodeURIComponent(value),
];
if ( path !== 'none' ) {
cookieParts.push('; path=/');
}
document.cookie = cookieParts.join('');
}
/******************************************************************************* /*******************************************************************************
* *
* Scriplets below this section are only available for filter lists from * Scriplets below this section are only available for filter lists from
@ -2631,48 +2677,51 @@ function trustedSetConstant(
/******************************************************************************* /*******************************************************************************
* *
* set-cookie.js * trusted-set-cookie.js
* *
* Set specified cookie to a specific value. * Set specified cookie to an arbitrary value.
* *
* Reference: * Reference:
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js * https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/trusted-set-cookie.js#L23
* *
**/ **/
builtinScriptlets.push({ builtinScriptlets.push({
name: 'set-cookie.js', name: 'trusted-set-cookie.js',
requiresTrust: true, requiresTrust: true,
fn: setCookie, fn: trustedSetCookie,
world: 'ISOLATED', world: 'ISOLATED',
}); });
function setCookie( function trustedSetCookie(
name = '', name = '',
value = '', value = '',
offsetExpiresSec = '',
path = '/' path = '/'
) { ) {
if ( name === '' ) { return; } if ( name === '' ) { return; }
const validValues = new Set([ const time = new Date();
'true', 'True', if ( value === '$now$' ) {
'false', 'False', value = Date.now();
'yes', 'Yes', 'y', 'Y', } else if ( value === '$currentDate$' ) {
'no', 'No', 'n', 'N', value = time.toUTCString();
'ok', 'OK', }
]); const validPaths = [ '/', 'none' ];
if ( validValues.has(value) === false ) { if ( validPaths.includes(path) === false ) { return; }
if ( /^\d+$/.test(value) === false ) { return; } const cookieParts = [ name, '=', value ];
const n = parseInt(value, 10); if ( offsetExpiresSec !== '' ) {
if ( n < 0 || n > 15 ) { return; } if ( offsetExpiresSec === '1day' ) {
time.setDate(time.getDate() + 1);
} else if ( offsetExpiresSec === '1year' ) {
time.setFullYear(time.getFullYear() + 1);
} else {
if ( /^\d+$/.test(offsetExpiresSec) === false ) { return; }
time.setSeconds(time.getSeconds() + parseInt(offsetExpiresSec, 10));
}
cookieParts.push('; expires=', time.toUTCString());
} }
const validPaths = new Set([ '/', 'none' ]);
if ( validPaths.has(path) === false ) { return; }
const cookieParts = [
encodeURIComponent(name), '=',
encodeURIComponent(value),
];
if ( path !== 'none' ) { if ( path !== 'none' ) {
cookieParts.push('; path=/'); cookieParts.push('; path=/');
} }
document.cookie = cookieParts.join(''); document.cookie = cookieParts.join('');
} }