Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Доработка GM_Fetch для Violentmonkey #1018

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/vot-min.user.js

Large diffs are not rendered by default.

40 changes: 19 additions & 21 deletions dist/vot.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,6 @@ const foswlyTranslateUrl = "https://translate.toil.cc/v2";
const detectRustServerUrl = "https://rust-server-531j.onrender.com/detect";

const proxyOnlyExtensions = [
"Violentmonkey",
"FireMonkey",
"Greasemonkey",
"AdGuard",
Expand Down Expand Up @@ -2553,39 +2552,45 @@ function clearFileName(filename) {
async function GM_fetch(url, opts = {}) {
const { timeout = 15000, ...fetchOptions } = opts;
const controller = new AbortController();
const headers = new Headers(fetchOptions.headers || {});

try {
if (url.includes("api.browser.yandex.ru")) {
throw new Error("Preventing yandex cors");
}

const response = await fetch(url, {
return await fetch(url, {
signal: controller.signal,
...fetchOptions,
});
return response;
} catch (err) {
// Если fetch завершился ошибкой, используем GM_xmlhttpRequest
// https://greasyfork.org/ru/scripts/421384-gm-fetch/code
debug.log("GM_fetch preventing cors by GM_xmlhttpRequest", err.message);
return new Promise((resolve, reject) => {
const requestHeaders = {};

for (const [key, value] of headers.entries()) {
requestHeaders[key] = value;
}

GM_xmlhttpRequest({
method: fetchOptions.method || "GET",
url,
responseType: "blob",
...fetchOptions,
data: fetchOptions.body,
timeout,
headers: requestHeaders,
onload: (resp) => {
// chrome \n and ":", firefox \r\n and ": " (Only in GM_xmlhttpRequest)
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#examples
const headers = Object.fromEntries(
resp.responseHeaders
.trim()
.split(/\r?\n/)
.map((line) => line.split(/: (.+)/))
.filter(([key]) => key && /^[\w-]+$/.test(key)),
);
const headers = new Headers();

const lines = resp.responseHeaders.trim().split(/\r?\n/);
for (const line of lines) {
const [key, ...values] = line.split(/:\s*/);
if (key) {
headers.set(key, values.join(": "));
}
}

const response = new Response(resp.response, {
status: resp.status,
Expand All @@ -2598,7 +2603,7 @@ async function GM_fetch(url, opts = {}) {
resolve(response);
},
ontimeout: () => reject(new Error("Timeout")),
onerror: (error) => reject(error),
onerror: (error) => reject(new Error(error)),
onabort: () => reject(new Error("AbortError")),
});
});
Expand Down Expand Up @@ -10242,13 +10247,6 @@ class VideoHandler {
debug.log("Extension compatibility passed...");

this.votOpts = {
headers: this.translateProxyEnabled
? {}
: {
"sec-ch-ua": null,
"sec-ch-ua-mobile": null,
"sec-ch-ua-platform": null,
},
fetchFn: GM_fetch,
hostVOT: votBackendUrl,
host: this.translateProxyEnabled ? this.data.proxyWorkerHost : workerHost,
Expand Down
1 change: 0 additions & 1 deletion src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const foswlyTranslateUrl = "https://translate.toil.cc/v2";
const detectRustServerUrl = "https://rust-server-531j.onrender.com/detect";

const proxyOnlyExtensions = [
"Violentmonkey",
"FireMonkey",
"Greasemonkey",
"AdGuard",
Expand Down
7 changes: 0 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,6 @@ class VideoHandler {
debug.log("Extension compatibility passed...");

this.votOpts = {
headers: this.translateProxyEnabled
? {}
: {
"sec-ch-ua": null,
"sec-ch-ua-mobile": null,
"sec-ch-ua-platform": null,
},
fetchFn: GM_fetch,
hostVOT: votBackendUrl,
host: this.translateProxyEnabled ? this.data.proxyWorkerHost : workerHost,
Expand Down
32 changes: 19 additions & 13 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,39 +115,45 @@ function clearFileName(filename) {
async function GM_fetch(url, opts = {}) {
const { timeout = 15000, ...fetchOptions } = opts;
const controller = new AbortController();
const headers = new Headers(fetchOptions.headers || {});

try {
if (url.includes("api.browser.yandex.ru")) {
throw new Error("Preventing yandex cors");
}

const response = await fetch(url, {
return await fetch(url, {
signal: controller.signal,
...fetchOptions,
});
return response;
} catch (err) {
// Если fetch завершился ошибкой, используем GM_xmlhttpRequest
// https://greasyfork.org/ru/scripts/421384-gm-fetch/code
debug.log("GM_fetch preventing cors by GM_xmlhttpRequest", err.message);
return new Promise((resolve, reject) => {
const requestHeaders = {};

for (const [key, value] of headers.entries()) {
requestHeaders[key] = value;
}

GM_xmlhttpRequest({
method: fetchOptions.method || "GET",
url,
responseType: "blob",
...fetchOptions,
data: fetchOptions.body,
timeout,
headers: requestHeaders,
onload: (resp) => {
// chrome \n and ":", firefox \r\n and ": " (Only in GM_xmlhttpRequest)
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#examples
const headers = Object.fromEntries(
resp.responseHeaders
.trim()
.split(/\r?\n/)
.map((line) => line.split(/: (.+)/))
.filter(([key]) => key && /^[\w-]+$/.test(key)),
);
const headers = new Headers();

const lines = resp.responseHeaders.trim().split(/\r?\n/);
for (const line of lines) {
const [key, ...values] = line.split(/:\s*/);
if (key) {
headers.set(key, values.join(": "));
}
}

const response = new Response(resp.response, {
status: resp.status,
Expand All @@ -160,7 +166,7 @@ async function GM_fetch(url, opts = {}) {
resolve(response);
},
ontimeout: () => reject(new Error("Timeout")),
onerror: (error) => reject(error),
onerror: (error) => reject(new Error(error)),
onabort: () => reject(new Error("AbortError")),
});
});
Expand Down
Loading