Skip to content

Commit

Permalink
added GM_fetch insted of fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyhalight committed Apr 20, 2024
1 parent 51767d4 commit e9b222b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/localization/localizationProvider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import defaultLocale from "./locales/en.json";
import debug from "../utils/debug.js";
import { votStorage } from "../utils/storage.js";
import { GM_fetch } from "../utils/utils.js";

const localesVersion = 2;
const localesUrl = `https://raw.githubusercontent.com/ilyhalight/voice-over-translation/${
Expand Down Expand Up @@ -115,7 +116,7 @@ export const localizationProvider = new (class {
debug.log("Updating locale...");

try {
const response = await fetch(`${localesUrl}/${this.lang}.json`);
const response = await GM_fetch(`${localesUrl}/${this.lang}.json`);
if (response.status !== 200) throw response.status;
const text = await response.text();
await votStorage.set("locale-phrases", text);
Expand Down
4 changes: 2 additions & 2 deletions src/subtitles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import youtubeUtils from "./utils/youtubeUtils.js";
import { lang } from "./utils/utils.js";
import { lang, GM_fetch } from "./utils/utils.js";
import { yandexProtobuf } from "./yandexProtobuf.js";
import requestVideoSubtitles from "./rvs.js";
import debug from "./utils/debug.js";
Expand Down Expand Up @@ -146,7 +146,7 @@ export async function fetchSubtitles(subtitlesObject) {

const fetchPromise = (async () => {
try {
const response = await fetch(subtitlesObject.url);
const response = await GM_fetch(subtitlesObject.url);
return await response.json();
} catch (error) {
console.error("[VOT] Failed to fetch subtitles. Reason:", error);
Expand Down
47 changes: 46 additions & 1 deletion src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,49 @@ function initHls() {
: undefined;
}

export { getVideoId, secsToStrTime, langTo6391, isPiPAvailable, initHls };
function GM_fetch(url, opt = {}) {
// https://greasyfork.org/ru/scripts/421384-gm-fetch/code
return new Promise((resolve, reject) => {
// https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest
// https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest
opt.url = url;
opt.data = opt.body;
opt.responseType = "blob";
opt.onload = (resp) => {
resolve(
new Response(resp.response, {
status: resp.status,
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/getAllResponseHeaders#examples
headers: Object.fromEntries(
resp.responseHeaders
.trim()
.split("\r\n")
.map((line) => {
let parts = line.split(": ");
// if don't do this, you will get an error on some sites
if (parts?.[0] === "set-cookie") {
return;
}

return [parts.shift(), parts.join(": ")];
})
.filter((key) => key),
),
}),
);
};
opt.ontimeout = () => reject("fetch timeout");
opt.onerror = (error) => reject(error);
opt.onabort = () => reject("fetch abort");
GM_xmlhttpRequest(opt);
});
}

export {
getVideoId,
secsToStrTime,
langTo6391,
isPiPAvailable,
initHls,
GM_fetch,
};
10 changes: 10 additions & 0 deletions src/utils/youtubeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ function setVideoVolume(volume) {
}
}

function isMuted() {
const player = getPlayer();
if (player?.isMuted) {
return player.isMuted.call();
}

return false;
}

function videoSeek(video, time) {
// * TIME IN MS
debug.log("videoSeek", time);
Expand Down Expand Up @@ -179,4 +188,5 @@ export default {
getVideoData,
setVideoVolume,
videoSeek,
isMuted,
};
3 changes: 2 additions & 1 deletion src/yandexRequest-cloudflare.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { yandexUserAgent, proxyWorkerHost } from "./config/config.js";
import debug from "./utils/debug.js";
import { votStorage } from "./utils/storage.js";
import { GM_fetch } from "./utils/utils.js";

async function yandexRequest(path, body, headers, callback) {
let response;
Expand Down Expand Up @@ -35,7 +36,7 @@ async function yandexRequest(path, body, headers, callback) {
};
const workerHost = await votStorage.get("proxyWorkerHost", proxyWorkerHost);
// Fetch the translation from the worker host
response = await fetch(`https://${workerHost}${path}`, options);
response = await GM_fetch(`https://${workerHost}${path}`, options);
debug.log("yandexRequest:", response.status, response);
// Get the response body as an array buffer
responseBody = await response.arrayBuffer();
Expand Down

0 comments on commit e9b222b

Please sign in to comment.