-
Notifications
You must be signed in to change notification settings - Fork 279
/
sample-fetch-opts-filter.js
36 lines (32 loc) · 1.27 KB
/
sample-fetch-opts-filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @fileoverview Deprecated since v1.0.17
*
* The new filter options has less memory footprint of Network Extension, it is
* recommended to use the option when response body is larger than 512 KB.
* For the data size less then 128 KB filter is not recommended.
* The JSON file requested in this sample is about 20 MB.
*
* The original body in the filter script environment is the variable "body", and the
* filter script should just return directly (synchronize) with the filtered result (as
* a string). The returned result will be passed to the receiver as the new response body.
* If the result is more than 16 KB, an error will be thrown.
*
* It is worth noting that the filter module is only for "filter" usage, no other APIs are supported.
*
* Since the sample JSON file is a little bit large, so the parsing process may take a while.
*
*/
const url = 'https://raw.githubusercontent.com/json-iterator/test-data/master/large-file.json';
const bodyFilter = 'var result = JSON.parse(body); return JSON.stringify(result[167]);';
const myRequest = {
url: url,
opts:{'filter': bodyFilter}
};
$task.fetch(myRequest).then(response => {
console.log(response.body);
$notify("T", "S", response.body);
$done();
}, reason => {
$done();
// reason.error
});