From e9b222b5d67209094602e77bb9e8978e3dc92a62 Mon Sep 17 00:00:00 2001 From: Toil Date: Sat, 20 Apr 2024 03:30:12 +0300 Subject: [PATCH] added GM_fetch insted of fetch --- src/localization/localizationProvider.js | 3 +- src/subtitles.js | 4 +- src/utils/utils.js | 47 +++++++++++++++++++++++- src/utils/youtubeUtils.js | 10 +++++ src/yandexRequest-cloudflare.js | 3 +- 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/localization/localizationProvider.js b/src/localization/localizationProvider.js index 4dc2ee77..5b94fe6b 100644 --- a/src/localization/localizationProvider.js +++ b/src/localization/localizationProvider.js @@ -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/${ @@ -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); diff --git a/src/subtitles.js b/src/subtitles.js index 4dccac8a..84777c33 100644 --- a/src/subtitles.js +++ b/src/subtitles.js @@ -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"; @@ -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); diff --git a/src/utils/utils.js b/src/utils/utils.js index 2747766f..cbdbb5f9 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -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, +}; diff --git a/src/utils/youtubeUtils.js b/src/utils/youtubeUtils.js index def8bdc1..f4f79986 100644 --- a/src/utils/youtubeUtils.js +++ b/src/utils/youtubeUtils.js @@ -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); @@ -179,4 +188,5 @@ export default { getVideoData, setVideoVolume, videoSeek, + isMuted, }; diff --git a/src/yandexRequest-cloudflare.js b/src/yandexRequest-cloudflare.js index dc605c63..2e574705 100644 --- a/src/yandexRequest-cloudflare.js +++ b/src/yandexRequest-cloudflare.js @@ -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; @@ -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();