Skip to content

Commit eaea26b

Browse files
committed
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
1 parent f04f13e commit eaea26b

File tree

1 file changed

+89
-40
lines changed

1 file changed

+89
-40
lines changed

assets/resources/scriptlets.js

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,10 @@ function noFetchIf(
12581258
builtinScriptlets.push({
12591259
name: 'refresh-defuser.js',
12601260
fn: refreshDefuser,
1261+
world: 'ISOLATED',
1262+
dependencies: [
1263+
'run-at.fn',
1264+
],
12611265
});
12621266
// https://www.reddit.com/r/uBlockOrigin/comments/q0frv0/while_reading_a_sports_article_i_was_redirected/hf7wo9v/
12631267
function refreshDefuser(
@@ -1273,11 +1277,9 @@ function refreshDefuser(
12731277
const ms = Math.max(parseFloat(s) || 0, 0) * 1000;
12741278
setTimeout(( ) => { window.stop(); }, ms);
12751279
};
1276-
if ( document.readyState === 'loading' ) {
1277-
document.addEventListener('DOMContentLoaded', defuse, { once: true });
1278-
} else {
1280+
runAt(( ) => {
12791281
defuse();
1280-
}
1282+
}, 'interactive');
12811283
}
12821284

12831285
/******************************************************************************/
@@ -1286,6 +1288,9 @@ builtinScriptlets.push({
12861288
name: 'remove-attr.js',
12871289
aliases: [ 'ra.js' ],
12881290
fn: removeAttr,
1291+
dependencies: [
1292+
'run-at.fn',
1293+
],
12891294
});
12901295
function removeAttr(
12911296
token = '',
@@ -1338,13 +1343,9 @@ function removeAttr(
13381343
subtree: true,
13391344
});
13401345
};
1341-
if ( document.readyState !== 'complete' && /\bcomplete\b/.test(behavior) ) {
1342-
self.addEventListener('load', start, { once: true });
1343-
} else if ( document.readyState !== 'loading' || /\basap\b/.test(behavior) ) {
1346+
runAt(( ) => {
13441347
start();
1345-
} else {
1346-
self.addEventListener('DOMContentLoaded', start, { once: true });
1347-
}
1348+
}, /\bcomplete\b/.test(behavior) ? 'idle' : 'interactive');
13481349
}
13491350

13501351
/******************************************************************************/
@@ -1353,6 +1354,9 @@ builtinScriptlets.push({
13531354
name: 'remove-class.js',
13541355
aliases: [ 'rc.js' ],
13551356
fn: removeClass,
1357+
dependencies: [
1358+
'run-at.fn',
1359+
],
13561360
});
13571361
function removeClass(
13581362
token = '',
@@ -1403,13 +1407,9 @@ function removeClass(
14031407
subtree: true,
14041408
});
14051409
};
1406-
if ( document.readyState !== 'complete' && /\bcomplete\b/.test(behavior) ) {
1407-
self.addEventListener('load', start, { once: true });
1408-
} else if ( document.readyState === 'loading' ) {
1409-
self.addEventListener('DOMContentLoaded', start, { once: true });
1410-
} else {
1410+
runAt(( ) => {
14111411
start();
1412-
}
1412+
}, /\bcomplete\b/.test(behavior) ? 'idle' : 'interactive');
14131413
}
14141414

14151415
/******************************************************************************/
@@ -2550,6 +2550,52 @@ function removeNodeText(
25502550
replaceNodeTextCore(nodeName, '', '', 'condition', condition || '', ...extraArgs);
25512551
}
25522552

2553+
/*******************************************************************************
2554+
*
2555+
* set-cookie.js
2556+
*
2557+
* Set specified cookie to a specific value.
2558+
*
2559+
* Reference:
2560+
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js
2561+
*
2562+
**/
2563+
2564+
builtinScriptlets.push({
2565+
name: 'set-cookie.js',
2566+
fn: setCookie,
2567+
world: 'ISOLATED',
2568+
});
2569+
function setCookie(
2570+
name = '',
2571+
value = '',
2572+
path = '/'
2573+
) {
2574+
if ( name === '' ) { return; }
2575+
const validValues = new Set([
2576+
'true', 'True',
2577+
'false', 'False',
2578+
'yes', 'Yes', 'y', 'Y',
2579+
'no', 'No', 'n', 'N',
2580+
'ok', 'OK',
2581+
]);
2582+
if ( validValues.has(value) === false ) {
2583+
if ( /^\d+$/.test(value) === false ) { return; }
2584+
const n = parseInt(value, 10);
2585+
if ( n > 15 ) { return; }
2586+
}
2587+
const validPaths = [ '/', 'none' ];
2588+
if ( validPaths.includes(path) === false ) { return; }
2589+
const cookieParts = [
2590+
encodeURIComponent(name), '=',
2591+
encodeURIComponent(value),
2592+
];
2593+
if ( path !== 'none' ) {
2594+
cookieParts.push('; path=/');
2595+
}
2596+
document.cookie = cookieParts.join('');
2597+
}
2598+
25532599
/*******************************************************************************
25542600
*
25552601
* Scriplets below this section are only available for filter lists from
@@ -2631,48 +2677,51 @@ function trustedSetConstant(
26312677

26322678
/*******************************************************************************
26332679
*
2634-
* set-cookie.js
2680+
* trusted-set-cookie.js
26352681
*
2636-
* Set specified cookie to a specific value.
2682+
* Set specified cookie to an arbitrary value.
26372683
*
26382684
* Reference:
2639-
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js
2685+
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/trusted-set-cookie.js#L23
26402686
*
26412687
**/
26422688

26432689
builtinScriptlets.push({
2644-
name: 'set-cookie.js',
2690+
name: 'trusted-set-cookie.js',
26452691
requiresTrust: true,
2646-
fn: setCookie,
2692+
fn: trustedSetCookie,
26472693
world: 'ISOLATED',
26482694
});
2649-
function setCookie(
2695+
function trustedSetCookie(
26502696
name = '',
26512697
value = '',
2698+
offsetExpiresSec = '',
26522699
path = '/'
26532700
) {
26542701
if ( name === '' ) { return; }
2655-
const validValues = new Set([
2656-
'true', 'True',
2657-
'false', 'False',
2658-
'yes', 'Yes', 'y', 'Y',
2659-
'no', 'No', 'n', 'N',
2660-
'ok', 'OK',
2661-
]);
2662-
if ( validValues.has(value) === false ) {
2663-
if ( /^\d+$/.test(value) === false ) { return; }
2664-
const n = parseInt(value, 10);
2665-
if ( n < 0 || n > 15 ) { return; }
2702+
const time = new Date();
2703+
if ( value === '$now$' ) {
2704+
value = Date.now();
2705+
} else if ( value === '$currentDate$' ) {
2706+
value = time.toUTCString();
2707+
}
2708+
const validPaths = [ '/', 'none' ];
2709+
if ( validPaths.includes(path) === false ) { return; }
2710+
const cookieParts = [ name, '=', value ];
2711+
if ( offsetExpiresSec !== '' ) {
2712+
if ( offsetExpiresSec === '1day' ) {
2713+
time.setDate(time.getDate() + 1);
2714+
} else if ( offsetExpiresSec === '1year' ) {
2715+
time.setFullYear(time.getFullYear() + 1);
2716+
} else {
2717+
if ( /^\d+$/.test(offsetExpiresSec) === false ) { return; }
2718+
time.setSeconds(time.getSeconds() + parseInt(offsetExpiresSec, 10));
2719+
}
2720+
cookieParts.push('; expires=', time.toUTCString());
26662721
}
2667-
const validPaths = new Set([ '/', 'none' ]);
2668-
if ( validPaths.has(path) === false ) { return; }
2669-
const cookieParts = [
2670-
encodeURIComponent(name), '=',
2671-
encodeURIComponent(value),
2672-
];
26732722
if ( path !== 'none' ) {
26742723
cookieParts.push('; path=/');
2675-
}
2724+
}
26762725
document.cookie = cookieParts.join('');
26772726
}
26782727

0 commit comments

Comments
 (0)