-
Notifications
You must be signed in to change notification settings - Fork 279
/
sample-bytes-rewrite.js
41 lines (34 loc) · 1.67 KB
/
sample-bytes-rewrite.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
37
38
39
40
41
/**
* @fileoverview Example of HTTP rewrite of raw data.
* You can modify the original buffer then call $done with the modified result.
* You can create a new buffer data then $done with the result.
* You can replace the original buffer with the binary data fetched from another URL.
*
* The below rewrite sample specifies the process to replace the bytes of an image with the bytes of another image.
* It is worth noting that it's simpler to use the 302 redirection rewrite, but this is a good example on the usage of modifing the body that is not a string.
*
* @supported "Quantumult X (v1.0.19-build480) and iOS 14.0 +" or "Quantumult X (v1.0.21-build535) and iOS 13.0 +"
*
* $response.bodyBytes is instance of ArrayBuffer.
*
* [rewrite_local]
* ^https://raw\.githubusercontent\.com/crossutility/Quantumult-X/master/icon-samples/apple\.PNG url script-response-body https://raw.githubusercontent.com/crossutility/Quantumult-X/master/sample-bytes-rewrite.js
*/
const url = "https://raw.githubusercontent.com/crossutility/Quantumult-X/master/icon-samples/telegram.PNG";
const myRequest = {
url: url
};
$task.fetch(myRequest).then(response => {
$done({bodyBytes: response.bodyBytes});
}, reason => {
$done();
});
// var originalBuffer = $response.bodyBytes;
// var buffer = new ArrayBuffer(8);
// var uint8 = new Uint8Array(buffer);
// uint8[0] = 3;
// uint8[7] = 5;
// $done({bodyBytes: buffer});
// If you are getting buffer directly from another Uint8Array, you probably want slicing the results.
// The otherUint8 is a Uint8Array from somewhere else.
// $done({bodyBytes: otherUint8.buffer.slice(otherUint8.byteOffset, otherUint8.byteLength + otherUint8.byteOffset)});