Skip to content

Commit

Permalink
Merge pull request #1018 from SashaXser/dev
Browse files Browse the repository at this point in the history
Доработка GM_Fetch для Violentmonkey
  • Loading branch information
ilyhalight authored Jan 3, 2025
2 parents ca71fa2 + c2dac34 commit 174646b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 44 deletions.
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

0 comments on commit 174646b

Please sign in to comment.