Skip to content

Commit

Permalink
AG-31636 Improve 'prevent-xhr' — modify response. #415
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 92b7982
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 16:15:38 2024 +0300

    remove duplicate in compatibility-table

commit 33f2e9d
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 16:08:36 2024 +0300

    update compatibility-table

commit 019c9ab
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 16:02:53 2024 +0300

    update changelog

commit 600895a
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 15:56:03 2024 +0300

    Revert "Update changelog"

    This reverts commit 94e7d30.

commit 0790360
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 15:48:27 2024 +0300

    Revert "revert wiki .md files, update compatibility-table.json"

    This reverts commit 1e5458d.

commit 1e5458d
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 15:39:10 2024 +0300

    revert wiki .md files, update compatibility-table.json

commit 94e7d30
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 15:18:40 2024 +0300

    Update changelog

commit 4bf9071
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 15:13:31 2024 +0300

    update compatibility-table.json

commit 5a6f20e
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 15:03:33 2024 +0300

    update tests

commit 3cc23c1
Merge: 076ea89 756c918
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 14:55:13 2024 +0300

    resolve conflicts

commit 076ea89
Author: jellizaveta <[email protected]>
Date:   Mon Oct 7 14:46:43 2024 +0300

    AG-31636 Improve  — modify response
  • Loading branch information
jellizaveta committed Oct 7, 2024
1 parent 756c918 commit d4cc07f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic

- `prevent-canvas` scriptlet [#451]

### Changed

- improve `prevent-xhr` — modify response [#415]

[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.12.1...HEAD
[#451]: https://github.com/AdguardTeam/Scriptlets/issues/451
[#415]: https://github.com/AdguardTeam/Scriptlets/issues/415

## [v1.12.1] - 2024-09-20

Expand Down
13 changes: 11 additions & 2 deletions scripts/compatibility-table.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
},
{
"adg": "prevent-xhr",
"ubo": "no-xhr-if.js (prevent-xhr.js)"
"ubo": "prevent-xhr.js (no-xhr-if.js)"
},
{
"adg": "prevent-popads-net"
Expand Down Expand Up @@ -384,6 +384,15 @@
},
{
"ubo": "trusted-suppress-native-method.js"
},
{
"ubo": "trusted-prevent-xhr.js"
},
{
"ubo": "trusted-prevent-dom-bypass.js"
},
{
"ubo": "trusted-override-element-method.js"
}
],
"redirects": [
Expand Down Expand Up @@ -587,4 +596,4 @@
"ubo": "noop-0.5s.mp3"
}
]
}
}
7 changes: 4 additions & 3 deletions src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ import {
* - `value` is string or regular expression for matching the value of the option
* passed to `XMLHttpRequest.open()` call
* - `randomize` — defaults to `false` for empty responseText,
* optional argument to randomize responseText of matched XMLHttpRequest's response; possible values:
* - `true` to randomize responseText, random alphanumeric string of 10 symbols
* - colon-separated pair `name:value` string value to customize responseText data where
* optional argument to randomize responseText and response of matched XMLHttpRequest's response; possible values:
* - `true` to randomize responseText and response, random alphanumeric string of 10 symbols
* - colon-separated pair `name:value` string value to customize responseText and response data where
* - `name` — only `length` supported for now
* - `value` — range on numbers, for example `100-300`, limited to 500000 characters
*
Expand Down Expand Up @@ -169,6 +169,7 @@ export function preventXHR(source, propsToMatch, customResponseText) {
if (customResponseText) {
const randomText = generateRandomResponse(customResponseText);
if (randomText) {
modifiedResponse = randomText;
modifiedResponseText = randomText;
} else {
logMessage(source, `Invalid randomize parameter: '${customResponseText}'`);
Expand Down
44 changes: 44 additions & 0 deletions tests/scriptlets/prevent-xhr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,50 @@ if (isSupported) {
assert.strictEqual(xhr.getAllResponseHeaders(), expectedAllHeaders, 'getAllResponseHeaders() is mocked');
});

test('Args, method matched, randomize response (length:25000-30000)', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['method:GET', 'length:25000-30000'];

runScriptlet(name, MATCH_DATA);

const done = assert.async();

const xhr = new XMLHttpRequest();
xhr.open(METHOD, URL);
xhr.onload = () => {
assert.strictEqual(xhr.readyState, 4, 'Response done');
assert.strictEqual(typeof xhr.response, 'string', 'Response mocked');
assert.ok(
xhr.response.length > 20000,
`Response randomized, response length: ${xhr.response.length}`,
);
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
done();
};
xhr.send();
});

test('Empty arg, prevent all, randomize response', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['', 'true'];

runScriptlet(name, MATCH_DATA);

const done = assert.async();

const xhr = new XMLHttpRequest();
xhr.open(METHOD, URL);
xhr.onload = () => {
assert.strictEqual(typeof xhr.response, 'string', 'Response mocked');
assert.ok(xhr.response.length > 0, 'Response randomized');
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
done();
};
xhr.send();
});

test('Empty arg, prevent all, randomize response text', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
Expand Down

0 comments on commit d4cc07f

Please sign in to comment.