Skip to content

Commit aacbd93

Browse files
authored
Merge pull request #1035 from SashaXser/dev
Упрощение парсинга заголовков GM_fetch
2 parents 787dd02 + 1141b5b commit aacbd93

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

dist/vot-min.user.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vot.user.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -3840,25 +3840,20 @@ async function GM_fetch(url, opts = {}) {
38403840
method: fetchOptions.method || "GET",
38413841
url,
38423842
responseType: "blob",
3843-
...fetchOptions,
38443843
data: fetchOptions.body,
38453844
timeout,
3846-
headers: Object.fromEntries(new Headers(fetchOptions.headers || {})),
3845+
headers: fetchOptions.headers || {},
38473846
onload: (resp) => {
3848-
const headers = {};
3849-
resp.responseHeaders
3850-
.trim()
3851-
.split(/\r?\n/)
3852-
.forEach((line) => {
3853-
const [name, value] = line.split(/:\s*/);
3854-
if (name && value) {
3855-
headers[name.trim()] = value.trim();
3856-
}
3857-
});
3847+
const headers = Object.fromEntries(
3848+
resp.responseHeaders.split(/\r?\n/).flatMap((line) => {
3849+
const match = /^([\w-]+): (.+)$/.exec(line);
3850+
return match ? [[match[1], match[2]]] : [];
3851+
}),
3852+
);
38583853

38593854
const response = new Response(resp.response, {
38603855
status: resp.status,
3861-
headers: new Headers(headers),
3856+
headers: headers,
38623857
});
38633858
// Response have empty url by default
38643859
// this need to get same response url as in classic fetch

src/utils/utils.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,20 @@ async function GM_fetch(url, opts = {}) {
134134
method: fetchOptions.method || "GET",
135135
url,
136136
responseType: "blob",
137-
...fetchOptions,
138137
data: fetchOptions.body,
139138
timeout,
140-
headers: Object.fromEntries(new Headers(fetchOptions.headers || {})),
139+
headers: fetchOptions.headers || {},
141140
onload: (resp) => {
142-
const headers = {};
143-
resp.responseHeaders
144-
.trim()
145-
.split(/\r?\n/)
146-
.forEach((line) => {
147-
const [name, value] = line.split(/:\s*/);
148-
if (name && value) {
149-
headers[name.trim()] = value.trim();
150-
}
151-
});
141+
const headers = Object.fromEntries(
142+
resp.responseHeaders.split(/\r?\n/).flatMap((line) => {
143+
const match = /^([\w-]+): (.+)$/.exec(line);
144+
return match ? [[match[1], match[2]]] : [];
145+
}),
146+
);
152147

153148
const response = new Response(resp.response, {
154149
status: resp.status,
155-
headers: new Headers(headers),
150+
headers: headers,
156151
});
157152
// Response have empty url by default
158153
// this need to get same response url as in classic fetch

0 commit comments

Comments
 (0)