Skip to content

Commit

Permalink
Merge branch 'master' into fix/AG-33558
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWr committed Jul 8, 2024
2 parents 4822423 + 15a2d8f commit 2ca4790
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
### Added

- new values to `set-cookie` and `set-cookie-reload` scriptlets: `hide`, `hidden` [#433]
- new values to `set-local-storage-item` and `set-session-storage-item` scriptlets:
`accept`, `accepted`, `reject`, `rejected` [#429]
- ability to log original and modified content in `trusted-replace-node-text`, `xml-prune`, `m3u-prune`,
`trusted-replace-fetch-response` and `trusted-replace-xhr-response` scriptlets [#411]

### Changed

- Log message format [CoreLibs#180]

[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.11.1...HEAD
[#433]: https://github.com/AdguardTeam/Scriptlets/issues/433
[#429]: https://github.com/AdguardTeam/Scriptlets/issues/429
[#411]: https://github.com/AdguardTeam/Scriptlets/issues/411
[CoreLibs#180]: https://github.com/AdguardTeam/CoreLibs/issues/180

## [v1.11.1] - 2024-06-13

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adguard/scriptlets",
"version": "1.11.2",
"version": "1.11.4",
"description": "AdGuard's JavaScript library of Scriptlets and Redirect resources",
"scripts": {
"build": "babel-node -x .js,.ts scripts/build.js",
Expand Down
25 changes: 16 additions & 9 deletions src/helpers/hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,32 @@ declare global {
* for logging scriptlets
*/
export const hit = (source: Source) => {
if (source.verbose !== true) {
const ADGUARD_PREFIX = '[AdGuard]';
if (!source.verbose) {
return;
}

try {
const log = console.log.bind(console);
const trace = console.trace.bind(console);

let prefix = '';
if (source.domainName) {
prefix += `${source.domainName}`;
let label = `${ADGUARD_PREFIX} `;
if (source.engine === 'corelibs') {
// rule text will be available for corelibs
label += source.ruleText;
} else {
if (source.domainName) {
label += `${source.domainName}`;
}
if (source.args) {
label += `#%#//scriptlet('${source.name}', '${source.args.join("', '")}')`;
} else {
label += `#%#//scriptlet('${source.name}')`;
}
}
prefix += `#%#//scriptlet('${source.name}', '${source.args.join(', ')}')`;

log(`${prefix} trace start`);
if (trace) {
trace();
trace(label);
}
log(`${prefix} trace end`);
} catch (e) {
// try catch for Edge 15
// In according to this issue https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/14495220/
Expand Down
12 changes: 9 additions & 3 deletions src/helpers/storage-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ export const setStorageItem = (source: Source, storage: Storage, key: string, va
*/
export const removeStorageItem = (source: Source, storage: Storage, key: string): void => {
try {
if (key.startsWith('/')
&& (key.endsWith('/') || key.endsWith('/i'))
&& isValidStrPattern(key)) {
if (
key.startsWith('/')
&& (key.endsWith('/') || key.endsWith('/i'))
&& isValidStrPattern(key)
) {
const regExpKey = toRegExp(key);
const storageKeys = Object.keys(storage);
storageKeys.forEach((storageKey) => {
Expand Down Expand Up @@ -70,6 +72,10 @@ export const getLimitedStorageItemValue = (value: string): StorageItemValue | nu
'no',
'on',
'off',
'accept',
'accepted',
'reject',
'rejected',
]);

let validValue;
Expand Down
4 changes: 4 additions & 0 deletions src/scriptlets/set-local-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import {
* - `no`
* - `on`
* - `off`
* - `accept`
* - `accepted`
* - `reject`
* - `rejected`
* - `$remove$` — remove specific item from localStorage
*
* ### Examples
Expand Down
4 changes: 4 additions & 0 deletions src/scriptlets/set-session-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import {
* - `no`
* - `on`
* - `off`
* - `accept`
* - `accepted`
* - `reject`
* - `rejected`
* - `$remove$` — remove specific item from sessionStorage
*
* ### Examples
Expand Down
28 changes: 28 additions & 0 deletions tests/scriptlets/set-local-storage-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ if (isSafariBrowser()) {
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'off', 'localStorage item has been set');
clearStorageItem(iName);

iName = '__test-item_accept';
iValue = 'accept';
runScriptlet(name, [iName, iValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'accept', 'localStorage item has been set');
clearStorageItem(iName);

iName = '__test-item_accepted';
iValue = 'accepted';
runScriptlet(name, [iName, iValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'accepted', 'localStorage item has been set');
clearStorageItem(iName);

iName = '__test-item_reject';
iValue = 'reject';
runScriptlet(name, [iName, iValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'reject', 'localStorage item has been set');
clearStorageItem(iName);

iName = '__test-item_rejected';
iValue = 'rejected';
runScriptlet(name, [iName, iValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.localStorage.getItem(iName), 'rejected', 'localStorage item has been set');
clearStorageItem(iName);
});

test('Set localStorage key with invalid value', (assert) => {
Expand Down
28 changes: 28 additions & 0 deletions tests/scriptlets/set-session-storage-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ if (isSafariBrowser()) {
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'off', 'sessionStorage item has been set');
clearStorageItem(cName);

cName = '__test-item_accept';
cValue = 'accept';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'accept', 'sessionStorage item has been set');
clearStorageItem(cName);

cName = '__test-item_accepted';
cValue = 'accepted';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'accepted', 'sessionStorage item has been set');
clearStorageItem(cName);

cName = '__test-item_reject';
cValue = 'reject';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'reject', 'sessionStorage item has been set');
clearStorageItem(cName);

cName = '__test-item_rejected';
cValue = 'rejected';
runScriptlet(name, [cName, cValue]);
assert.strictEqual(window.hit, 'FIRED', 'Hit was fired');
assert.strictEqual(window.sessionStorage.getItem(cName), 'rejected', 'sessionStorage item has been set');
clearStorageItem(cName);
});

test('Set sessionStorage key with invalid value', (assert) => {
Expand Down

0 comments on commit 2ca4790

Please sign in to comment.