Skip to content

Commit

Permalink
Merge pull request #581 from SashaXser/main
Browse files Browse the repository at this point in the history
1.5.1.5
  • Loading branch information
ilyhalight authored Apr 6, 2024
2 parents 9713db1 + b9d8b16 commit b3c5f83
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 256 deletions.
4 changes: 2 additions & 2 deletions dist/vot-cloudflare-min.user.js

Large diffs are not rendered by default.

185 changes: 95 additions & 90 deletions dist/vot-cloudflare.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
// @exclude file://*/*.mp4*
// @connect api.browser.yandex.ru
// @namespace vot-cloudflare
// @version 1.5.1.4
// @version 1.5.1.5
// @icon https://translate.yandex.ru/icons/favicon.ico
// @author sodapng, mynovelhost, Toil, SashaXser, MrSoczekXD
// @homepageURL https://github.com/ilyhalight/voice-over-translation/issues
Expand Down Expand Up @@ -1157,38 +1157,35 @@ const localizationProvider = new (class {
}

async update(force = false) {
const localeVersion = await storage/* votStorage */.d.get("locale-version", 0, true);
const localeLang = await storage/* votStorage */.d.get("locale-lang");
if (
!force &&
(await storage/* votStorage */.d.get("locale-version", 0, true)) === localesVersion &&
(await storage/* votStorage */.d.get("locale-lang")) === this.lang
localeVersion === localesVersion &&
localeLang === this.lang
) {
return;
}

debug/* default */.A.log("Updating locale...");

await fetch(`${localesUrl}/${this.lang}.json`)
.then((response) => {
if (response.status === 200) return response.text();
throw response.status;
})
.then(async (text) => {
await storage/* votStorage */.d.set("locale-phrases", text);
this.setLocaleFromJsonString(text);
const version = this.getFromLocale(this.locale, "__version__");
if (typeof version === "number")
await storage/* votStorage */.d.set("locale-version", version);
await storage/* votStorage */.d.set("locale-lang", this.lang);
})
.catch(async (error) => {
console.error(
"[VOT] [localizationProvider] failed get locale, cause:",
error,
);
this.setLocaleFromJsonString(
await storage/* votStorage */.d.get("locale-phrases", ""),
);
});
try {
const response = await fetch(`${localesUrl}/${this.lang}.json`);
if (response.status !== 200) throw response.status;
const text = await response.text();
await storage/* votStorage */.d.set("locale-phrases", text);
this.setLocaleFromJsonString(text);
const version = this.getFromLocale(this.locale, "__version__");
if (typeof version === "number")
await storage/* votStorage */.d.set("locale-version", version);
await storage/* votStorage */.d.set("locale-lang", this.lang);
} catch (error) {
console.error(
"[VOT] [localizationProvider] failed get locale, cause:",
error,
);
this.setLocaleFromJsonString(await storage/* votStorage */.d.get("locale-phrases", ""));
}
}

setLocaleFromJsonString(json) {
Expand Down Expand Up @@ -1946,16 +1943,20 @@ async function getLanguage(player, response, title, description) {
/Auto-generated by YouTube/g,
/Provided to YouTube by/g,
/Released on/g,
/Bitcoin/g,
/USDT/g,
/^0x[a-fA-F0-9]{40}$/g, // Ethereum, USDT
/^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$/g, // Bitcoin
/4[0-9AB][1-9A-HJ-NP-Za-km-z]{93}$/g, // Monero
/Paypal/g,
];

const combinedRegex = new RegExp(
deletefilter.map((regex) => regex.source).join("|"),
);
const cleanedDescription = description
? description
.split("\n\n")
.filter((line) => !deletefilter.some((regex) => regex.test(line)))
.join("\n\n")
.split("\n")
.filter((line) => !combinedRegex.test(line))
.join("")
: "";

const cleanText = [title, cleanedDescription]
Expand Down Expand Up @@ -2891,37 +2892,36 @@ function formatYoutubeSubtitles(subtitles) {
}

async function fetchSubtitles(subtitlesObject) {
let resolved = false;
let subtitles = await Promise.race([
new Promise((resolve) => {
setTimeout(() => {
if (!resolved) {
console.error("[VOT] Failed to fetch subtitles. Reason: timeout");
resolve([]);
}
}, 5000);
}),
new Promise((resolve) => {
debug/* default */.A.log("Fetching subtitles:", subtitlesObject);
fetch(subtitlesObject.url)
.then((response) => response.json())
.then((json) => {
resolved = true;
resolve(json);
})
.catch((error) => {
console.error("[VOT] Failed to fetch subtitles. Reason:", error);
resolved = true;
resolve({
containsTokens: false,
subtitles: [],
});
});
}),
]);
const timeoutPromise = new Promise((resolve) =>
setTimeout(
() =>
resolve({
containsTokens: false,
subtitles: [],
}),
5000,
),
);

const fetchPromise = (async () => {
try {
const response = await fetch(subtitlesObject.url);
return await response.json();
} catch (error) {
console.error("[VOT] Failed to fetch subtitles. Reason:", error);
return {
containsTokens: false,
subtitles: [],
};
}
})();

let subtitles = await Promise.race([timeoutPromise, fetchPromise]);

if (subtitlesObject.source === "youtube") {
subtitles = formatYoutubeSubtitles(subtitles);
}

subtitles.subtitles = getSubtitlesTokens(subtitles, subtitlesObject.source);
console.log("[VOT] subtitles:", subtitles);
return subtitles;
Expand Down Expand Up @@ -3623,25 +3623,22 @@ async function bannedvideoUtils_getVideoData(videoId) {

;// CONCATENATED MODULE: ./src/utils/crypto.js
async function getHmacSha1(hmacKey, salt) {
const utf8Encoder = new TextEncoder("utf-8");
salt = utf8Encoder.encode(salt);

return window.crypto.subtle
.importKey(
try {
const utf8Encoder = new TextEncoder("utf-8");
salt = utf8Encoder.encode(salt);
const key = await window.crypto.subtle.importKey(
"raw",
utf8Encoder.encode(hmacKey),
{ name: "HMAC", hash: { name: "SHA-1" } },
false,
["sign", "verify"],
)
.then((key) => window.crypto.subtle.sign("HMAC", key, salt))
.then((arrayBuffer) =>
btoa(String.fromCharCode(...new Uint8Array(arrayBuffer))),
)
.catch((err) => {
console.error(err);
return false;
});
);
const signature = await window.crypto.subtle.sign("HMAC", key, salt);
return btoa(String.fromCharCode(...new Uint8Array(signature)));
} catch (err) {
console.error(err);
return false;
}
}

;// CONCATENATED MODULE: ./src/utils/weverseUtils.js
Expand Down Expand Up @@ -4435,7 +4432,7 @@ class VideoHandler {
this.container = container;
this.site = site;
this.handleSrcChangedBound = this.handleSrcChanged.bind(this);
this.video.addEventListener("loadedmetadata", this.handleSrcChangedBound);
this.video.addEventListener("loadeddata", this.handleSrcChangedBound);
this.stopTranslationBound = this.stopTranslation.bind(this);
this.handleVideoEventBound = this.handleVideoEvent.bind(this);
this.changeOpacityOnEventBound = this.changeOpacityOnEvent.bind(this);
Expand Down Expand Up @@ -4478,15 +4475,14 @@ class VideoHandler {
proxyWorkerHost: storage/* votStorage */.d.get("proxyWorkerHost", config/* proxyWorkerHost */.Pm),
};

const dataEntries = await Promise.all(
Object.entries(dataPromises).map(async ([key, promise]) => [
key,
await promise,
]),
this.data = Object.fromEntries(
await Promise.all(
Object.entries(dataPromises).map(async ([key, promise]) => [
key,
await promise,
]),
),
);
this.data = Object.fromEntries(dataEntries);

this.videoData = await this.getVideoData();

console.log("[db] data from db: ", this.data);

Expand All @@ -4506,14 +4502,16 @@ class VideoHandler {
this.votButton.container.hidden = videoHasNoSource;
if (videoHasNoSource) {
this.votMenu.container.hidden = true;
} else {
this.videoData = await this.getVideoData();
this.setSelectMenuValues(
this.videoData.detectedLanguage,
this.data.responseLanguage ?? "ru",
);
}

await this.updateSubtitles();
await this.changeSubtitlesLang("disabled");
this.setSelectMenuValues(
this.videoData.detectedLanguage,
this.data.responseLanguage ?? "ru",
);
this.translateToLang = this.data.responseLanguage ?? "ru";

this.initExtraEvents();
Expand Down Expand Up @@ -5464,7 +5462,10 @@ class VideoHandler {
}

addExtraEventListener(this.video, "emptied", () => {
if (getVideoId(this.site.host, this.video) === this.videoData.videoId)
if (
this.video.src &&
getVideoId(this.site.host, this.video) === this.videoData.videoId
)
return;
debug/* default */.A.log("lipsync mode is emptied");
this.stopTranslation();
Expand All @@ -5473,6 +5474,7 @@ class VideoHandler {
addExtraEventListener(this.video, "progress", async () => {
if (
!this.videoData.videoId ||
this.audio.src ||
!this.firstPlay ||
this.data.autoTranslate !== 1 ||
getVideoId(this.site.host, this.video) !== this.videoData.videoId
Expand Down Expand Up @@ -5576,6 +5578,7 @@ class VideoHandler {
);
this.subtitlesList = [];
this.subtitlesListVideoId = null;
this.votButton.container.hidden = true;
await this.updateSubtitlesLangSelect();
return;
}
Expand Down Expand Up @@ -5689,7 +5692,7 @@ class VideoHandler {
if (window.location.hostname.includes("youtube.com")) {
this.ytData = await youtubeUtils.getVideoData();
videoData.isStream = this.ytData.isLive;
if (this.ytData.author !== "") {
if (this.ytData.title) {
videoData.detectedLanguage = this.ytData.detectedLanguage;
videoData.responseLanguage = this.translateToLang;
}
Expand Down Expand Up @@ -6202,7 +6205,10 @@ class VideoHandler {
}

async handleSrcChanged() {
if (getVideoId(this.site.host, this.video) === this.videoData.videoId)
if (
this.audio.src &&
getVideoId(this.site.host, this.video) === this.videoData.videoId
)
return;
debug/* default */.A.log("[VideoHandler] src changed", this);

Expand All @@ -6215,8 +6221,7 @@ class VideoHandler {
);

const hide =
(!this.video.src && !this.video.currentSrc && !this.video.srcObject) ||
!this.videoData.videoId;
!this.video.src && !this.video.currentSrc && !this.video.srcObject;
this.votButton.container.hidden = hide;
hide && (this.votMenu.container.hidden = hide);

Expand Down
4 changes: 2 additions & 2 deletions dist/vot-min.user.js

Large diffs are not rendered by default.

Loading

0 comments on commit b3c5f83

Please sign in to comment.