Skip to content

Commit

Permalink
AG-31713 Add possibility to validate redirects AdGuard compatibility …
Browse files Browse the repository at this point in the history
…without the full rule text #420

Merge in ADGUARD-FILTERS/scriptlets from fix/AG-31713 to master

Squashed commit of the following:

commit 7ac7c43
Author: scripthunter7 <d.tota@adguard.com>
Date:   Fri Apr 5 14:12:42 2024 +0200

    update types

commit 52148d5
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Fri Apr 5 15:10:57 2024 +0300

    CHANGELOG.md edited online with Bitbucket

commit ae49977
Author: scripthunter7 <d.tota@adguard.com>
Date:   Thu Apr 4 15:15:38 2024 +0200

    upd wiki

commit 9bf3723
Author: scripthunter7 <d.tota@adguard.com>
Date:   Thu Apr 4 15:07:28 2024 +0200

    implement isRedirectResourceCompatibleWithAdg
  • Loading branch information
scripthunter7 committed Apr 5, 2024
1 parent 8475b36 commit 89c6db9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
<!-- TODO: change `@added unknown` tag due to the actual version -->
<!-- during new scriptlets or redirects releasing -->

## Unreleased

### Added

- `isRedirectResourceCompatibleWithAdg()` method to check compatibility of redirect resources with AdGuard
without needing the full rule text [#420]

[#420]: https://github.com/AdguardTeam/Scriptlets/issues/420


## [v1.10.25] - 2024-03-28

### Added
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ AdGuard's Scriptlets and Redirect resources library which provides extended capa
- [`getCode()`](#redirects-api--getCode)
- [`isAdgRedirectRule()`](#redirects-api--isAdgRedirectRule)
- [`isValidAdgRedirectRule()`](#redirects-api--isValidAdgRedirectRule)
- [`isRedirectResourceCompatibleWithAdg()`](#redirects-api--isRedirectResourceCompatibleWithAdg)
- [`isAdgRedirectCompatibleWithUbo()`](#redirects-api--isAdgRedirectCompatibleWithUbo)
- [`isUboRedirectCompatibleWithAdg()`](#redirects-api--isUboRedirectCompatibleWithAdg)
- [`isAbpRedirectCompatibleWithAdg()`](#redirects-api--isAbpRedirectCompatibleWithAdg)
Expand Down Expand Up @@ -609,6 +610,18 @@ redirects.isAdgRedirectRule(rule)
redirects.isValidAdgRedirectRule(rule);
```
##### <a name="redirects-api--isRedirectResourceCompatibleWithAdg"></a> `isRedirectResourceCompatibleWithAdg()`
```javascript
/**
* Checks if the specified redirect resource is compatible with AdGuard
*
* @param redirectName - Redirect resource name to check
* @returns true if the redirect resource is compatible with AdGuard
*/
redirects.isRedirectResourceCompatibleWithAdg(redirectName);
```
##### <a name="redirects-api--isAdgRedirectCompatibleWithUbo"></a> `isAdgRedirectCompatibleWithUbo()`
```javascript
Expand Down
5 changes: 4 additions & 1 deletion scripts/compatibility-table.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@
},
{
"ubo": "trusted-replace-argument.js"
},
{
"ubo": "trusted-replace-outbound-text.js"
}
],
"redirects": [
Expand Down Expand Up @@ -597,4 +600,4 @@
"ubo": "noop-0.5s.mp3"
}
]
}
}
11 changes: 11 additions & 0 deletions src/helpers/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ const isValidAdgRedirectRule = (rule: string): boolean => {
return isRedirectRuleByType(rule, RedirectRuleType.ValidAdg);
};

/**
* Checks if the specified redirect resource is compatible with AdGuard
*
* @param redirectName - Redirect resource name to check
* @returns - true if the redirect resource is compatible with AdGuard
*/
export const isRedirectResourceCompatibleWithAdg = (redirectName: string): boolean => {
return Object.keys(validAdgCompatibility).includes(redirectName);
};

/**
* Checks if the AdGuard redirect `rule` has Ubo analog. Needed for Adg->Ubo conversion
*
Expand Down Expand Up @@ -519,6 +529,7 @@ const validator = {
ABSENT_SOURCE_TYPE_REPLACEMENT,
isAdgRedirectRule,
isValidAdgRedirectRule,
isRedirectResourceCompatibleWithAdg,
isAdgRedirectCompatibleWithUbo,
isUboRedirectCompatibleWithAdg,
isAbpRedirectCompatibleWithAdg,
Expand Down
1 change: 1 addition & 0 deletions src/redirects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const redirects = {
getCode: getRedirectCode,
isAdgRedirectRule: validator.isAdgRedirectRule,
isValidAdgRedirectRule: validator.isValidAdgRedirectRule,
isRedirectResourceCompatibleWithAdg: validator.isRedirectResourceCompatibleWithAdg,
isAdgRedirectCompatibleWithUbo: validator.isAdgRedirectCompatibleWithUbo,
isUboRedirectCompatibleWithAdg: validator.isUboRedirectCompatibleWithAdg,
isAbpRedirectCompatibleWithAdg: validator.isAbpRedirectCompatibleWithAdg,
Expand Down
18 changes: 18 additions & 0 deletions tests/api/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,24 @@ describe('Test redirects api methods', () => {
});
});

describe('isRedirectResourceCompatibleWithAdg()', () => {
const validRedirectNames = [
'noopvast-4.0', // adg only
'empty', // adg/ubo
];
test.each(validRedirectNames)('%s', (name) => {
expect(validator.isRedirectResourceCompatibleWithAdg(name)).toBeTruthy();
});

const invalidRedirectNames = [
'outbrain-widget.js', // ubo only
'blank-css', // name abp-only
];
test.each(invalidRedirectNames)('%s', (name) => {
expect(validator.isRedirectResourceCompatibleWithAdg(name)).toBeFalsy();
});
});

describe('isValidAdgRedirectRule()', () => {
const validRules = [
'||example.org$xmlhttprequest,redirect=noopvast-2.0',
Expand Down
8 changes: 8 additions & 0 deletions types/scriptlets.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ declare module '@adguard/scriptlets' {
*/
isAdgRedirectRule(rule: string): boolean;

/**
* Checks if the specified redirect resource is compatible with AdGuard
*
* @param redirectName - Redirect resource name to check
* @returns - true if the redirect resource is compatible with AdGuard
*/
isRedirectResourceCompatibleWithAdg(redirectName: string): boolean;

/**
* Checks if the `rule` is **valid** AdGuard redirect resource rule
*
Expand Down
1 change: 1 addition & 0 deletions wiki/compatibility-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
| | trusted-replace-node-text.js (trusted-rpnt.js, replace-node-text.js, rpnt.js) | |
| | remove-cache-storage-item.js | |
| | trusted-replace-argument.js | |
| | trusted-replace-outbound-text.js | |


## <a id="redirects"></a> Redirects compatibility table
Expand Down

0 comments on commit 89c6db9

Please sign in to comment.