Skip to content

Commit

Permalink
Merge pull request #757 from SashaXser/main
Browse files Browse the repository at this point in the history
Cleanup code
  • Loading branch information
ilyhalight authored Aug 15, 2024
2 parents 5412c34 + c8ab537 commit 49356a6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 109 deletions.
10 changes: 5 additions & 5 deletions dist/vot-min.user.js

Large diffs are not rendered by default.

77 changes: 28 additions & 49 deletions dist/vot.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2078,13 +2078,6 @@ const votStorage = new (class {
}

let val = window.localStorage.getItem(name);
if (name === "udemyData" && typeof val === "string") {
try {
val = JSON.parse(val);
} catch {
val = def;
}
}

const result = val ?? def;
return toNumber ? Number(result) : result;
Expand All @@ -2103,10 +2096,6 @@ const votStorage = new (class {
return GM_setValue(name, value);
}

if (name === "udemyData") {
value = JSON.stringify(value);
}

return window.localStorage.setItem(name, value);
}

Expand Down Expand Up @@ -5350,17 +5339,17 @@ class SubtitlesWidget {
}

bindEvents() {
this.onMouseDownBound = this.onMouseDown.bind(this);
this.onMouseUpBound = this.onMouseUp.bind(this);
this.onMouseMoveBound = this.onMouseMove.bind(this);
this.onTimeUpdateBound = this.debounce(this.update.bind(this), 100);
this.onMouseDownBound = (e) => this.onMouseDown(e);
this.onMouseUpBound = () => this.onMouseUp();
this.onMouseMoveBound = (e) => this.onMouseMove(e);
this.onTimeUpdateBound = this.debounce(() => this.update(), 100);

document.addEventListener("mousedown", this.onMouseDownBound);
document.addEventListener("mouseup", this.onMouseUpBound);
document.addEventListener("mousemove", this.onMouseMoveBound);
this.video?.addEventListener("timeupdate", this.onTimeUpdateBound);

this.resizeObserver = new ResizeObserver(this.onResize.bind(this));
this.resizeObserver = new ResizeObserver(() => this.onResize());
this.resizeObserver.observe(this.container);
}

Expand Down Expand Up @@ -5910,10 +5899,6 @@ class VideoHandler {
this.video = video;
this.container = container;
this.site = site;
this.stopTranslationBound = this.stopTranslation.bind(this);
this.handleVideoEventBound = this.handleVideoEvent.bind(this);
this.changeOpacityOnEventBound = this.changeOpacityOnEvent.bind(this);
this.resetTimerBound = this.resetTimer.bind(this);
this.init();
}

Expand Down Expand Up @@ -6190,18 +6175,11 @@ class VideoHandler {
this.initUIEvents();
this.initAudioBooster();

const videoHasNoSource =
!this.video.src && !this.video.currentSrc && !this.video.srcObject;
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",
);
}
this.videoData = await this.getVideoData();
this.setSelectMenuValues(
this.videoData.detectedLanguage,
this.data.responseLanguage ?? "ru",
);

this.translateToLang = this.data.responseLanguage ?? "ru";
this.initExtraEvents();
Expand Down Expand Up @@ -6871,8 +6849,10 @@ class VideoHandler {
this.votAutoTranslateCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.autoTranslate = Number(e.target.checked);
await votStorage.set("autoTranslate", this.data.autoTranslate);
await this.autoTranslate();
await Promise.all([
votStorage.set("autoTranslate", this.data.autoTranslate),
this.autoTranslate(),
]);
utils_debug.log(
"autoTranslate value changed. New value: ",
this.data.autoTranslate,
Expand Down Expand Up @@ -7329,23 +7309,23 @@ class VideoHandler {
addExtraEventListeners(
eContainer,
["mousemove", "mouseout"],
this.resetTimerBound,
this.resetTimer,
);

addExtraEventListener(
this.votButton.container,
"mousemove",
this.changeOpacityOnEventBound,
this.changeOpacityOnEvent,
);
addExtraEventListener(
this.votMenu.container,
"mousemove",
this.changeOpacityOnEventBound,
this.changeOpacityOnEvent,
);
addExtraEventListeners(
document,
["touchstart", "touchmove", "touchend"],
this.changeOpacityOnEventBound,
this.changeOpacityOnEvent,
);

// fix youtube hold to fast
Expand All @@ -7372,9 +7352,8 @@ class VideoHandler {
}
if ((await getVideoID(this.site, this.video)) === this.videoData.videoId)
return;
await this.handleSrcChanged();
utils_debug.log("lipsync mode is loadeddata");
await this.autoTranslate();
await Promise.all([this.handleSrcChanged(), this.autoTranslate()]);
utils_debug.log("lipsync mode is canplay");
});

addExtraEventListener(this.video, "emptied", async () => {
Expand All @@ -7400,15 +7379,15 @@ class VideoHandler {
this.votButton.container.style.opacity = n;
}

resetTimer() {
resetTimer = () => {
clearTimeout(this.timer);
this.logout(1);
this.timer = setTimeout(() => {
this.logout(0);
}, 1000);
}

changeOpacityOnEvent(event) {
changeOpacityOnEvent = (event) => {
clearTimeout(this.timer);
this.logout(1);
event.stopPropagation();
Expand Down Expand Up @@ -7756,15 +7735,15 @@ class VideoHandler {
}

// Define a function to handle common events
handleVideoEvent(event) {
handleVideoEvent = (event) => {
utils_debug.log(`video ${event.type}`);
this.lipSync(event.type);
}

// Default actions on stop translate
stopTranslate() {
for (const e of videoLipSyncEvents) {
this.video.removeEventListener(e, this.handleVideoEventBound);
this.video.removeEventListener(e, this.handleVideoEvent);
}
this.audio.pause();
this.audio.src = "";
Expand Down Expand Up @@ -7933,7 +7912,7 @@ class VideoHandler {
case "twitter":
document
.querySelector('button[data-testid="app-bar-back"][role="button"]')
.addEventListener("click", this.stopTranslationBound);
.addEventListener("click", this.stopTranslation);
break;
case "invidious":
case "piped":
Expand All @@ -7942,7 +7921,7 @@ class VideoHandler {

if (this.video && !this.video.paused) this.lipSync("play");
for (const e of videoLipSyncEvents) {
this.video.addEventListener(e, this.handleVideoEventBound);
this.video.addEventListener(e, this.handleVideoEvent);
}
this.transformBtn("success", localizationProvider.get("disableTranslate"));
this.afterUpdateTranslation(audioUrl);
Expand Down Expand Up @@ -8007,7 +7986,7 @@ class VideoHandler {

if (this.video && !this.video.paused) this.lipSync("play");
for (const e of videoLipSyncEvents) {
this.video.addEventListener(e, this.handleVideoEventBound);
this.video.addEventListener(e, this.handleVideoEvent);
}

return this.afterUpdateTranslation(streamURL);
Expand Down Expand Up @@ -8120,7 +8099,7 @@ class VideoHandler {
}

// Define a function to stop translation and clean up
stopTranslation() {
stopTranslation = () => {
this.stopTranslate();
this.syncVideoVolumeSlider();
}
Expand Down
68 changes: 29 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ class VideoHandler {
this.video = video;
this.container = container;
this.site = site;
this.stopTranslationBound = this.stopTranslation.bind(this);
this.handleVideoEventBound = this.handleVideoEvent.bind(this);
this.changeOpacityOnEventBound = this.changeOpacityOnEvent.bind(this);
this.resetTimerBound = this.resetTimer.bind(this);
this.init();
}

Expand Down Expand Up @@ -173,8 +169,8 @@ class VideoHandler {
await this.updateTranslationErrorMsg(
res.remainingTime > 0
? secsToStrTime(res.remainingTime)
: res.message ??
localizationProvider.get("translationTakeFewMinutes"),
: (res.message ??
localizationProvider.get("translationTakeFewMinutes")),
);
} catch (err) {
console.error("[VOT] Failed to translate video", err);
Expand Down Expand Up @@ -405,18 +401,11 @@ class VideoHandler {
this.initUIEvents();
this.initAudioBooster();

const videoHasNoSource =
!this.video.src && !this.video.currentSrc && !this.video.srcObject;
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",
);
}
this.videoData = await this.getVideoData();
this.setSelectMenuValues(
this.videoData.detectedLanguage,
this.data.responseLanguage ?? "ru",
);

this.translateToLang = this.data.responseLanguage ?? "ru";
this.initExtraEvents();
Expand Down Expand Up @@ -1086,8 +1075,10 @@ class VideoHandler {
this.votAutoTranslateCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.autoTranslate = Number(e.target.checked);
await votStorage.set("autoTranslate", this.data.autoTranslate);
await this.autoTranslate();
await Promise.all([
votStorage.set("autoTranslate", this.data.autoTranslate),
this.autoTranslate(),
]);
debug.log(
"autoTranslate value changed. New value: ",
this.data.autoTranslate,
Expand Down Expand Up @@ -1544,23 +1535,23 @@ class VideoHandler {
addExtraEventListeners(
eContainer,
["mousemove", "mouseout"],
this.resetTimerBound,
this.resetTimer,
);

addExtraEventListener(
this.votButton.container,
"mousemove",
this.changeOpacityOnEventBound,
this.changeOpacityOnEvent,
);
addExtraEventListener(
this.votMenu.container,
"mousemove",
this.changeOpacityOnEventBound,
this.changeOpacityOnEvent,
);
addExtraEventListeners(
document,
["touchstart", "touchmove", "touchend"],
this.changeOpacityOnEventBound,
this.changeOpacityOnEvent,
);

// fix youtube hold to fast
Expand All @@ -1587,9 +1578,8 @@ class VideoHandler {
}
if ((await getVideoID(this.site, this.video)) === this.videoData.videoId)
return;
await this.handleSrcChanged();
debug.log("lipsync mode is loadeddata");
await this.autoTranslate();
await Promise.all([this.handleSrcChanged(), this.autoTranslate()]);
debug.log("lipsync mode is canplay");
});

addExtraEventListener(this.video, "emptied", async () => {
Expand All @@ -1615,19 +1605,19 @@ class VideoHandler {
this.votButton.container.style.opacity = n;
}

resetTimer() {
resetTimer = () => {
clearTimeout(this.timer);
this.logout(1);
this.timer = setTimeout(() => {
this.logout(0);
}, 1000);
}
};

changeOpacityOnEvent(event) {
changeOpacityOnEvent = (event) => {
clearTimeout(this.timer);
this.logout(1);
event.stopPropagation();
}
};

async changeSubtitlesLang(subs) {
debug.log("[onchange] subtitles", subs);
Expand Down Expand Up @@ -1971,15 +1961,15 @@ class VideoHandler {
}

// Define a function to handle common events
handleVideoEvent(event) {
handleVideoEvent = (event) => {
debug.log(`video ${event.type}`);
this.lipSync(event.type);
}
};

// Default actions on stop translate
stopTranslate() {
for (const e of videoLipSyncEvents) {
this.video.removeEventListener(e, this.handleVideoEventBound);
this.video.removeEventListener(e, this.handleVideoEvent);
}
this.audio.pause();
this.audio.src = "";
Expand Down Expand Up @@ -2148,7 +2138,7 @@ class VideoHandler {
case "twitter":
document
.querySelector('button[data-testid="app-bar-back"][role="button"]')
.addEventListener("click", this.stopTranslationBound);
.addEventListener("click", this.stopTranslation);
break;
case "invidious":
case "piped":
Expand All @@ -2157,7 +2147,7 @@ class VideoHandler {

if (this.video && !this.video.paused) this.lipSync("play");
for (const e of videoLipSyncEvents) {
this.video.addEventListener(e, this.handleVideoEventBound);
this.video.addEventListener(e, this.handleVideoEvent);
}
this.transformBtn("success", localizationProvider.get("disableTranslate"));
this.afterUpdateTranslation(audioUrl);
Expand Down Expand Up @@ -2222,7 +2212,7 @@ class VideoHandler {

if (this.video && !this.video.paused) this.lipSync("play");
for (const e of videoLipSyncEvents) {
this.video.addEventListener(e, this.handleVideoEventBound);
this.video.addEventListener(e, this.handleVideoEvent);
}

return this.afterUpdateTranslation(streamURL);
Expand Down Expand Up @@ -2335,10 +2325,10 @@ class VideoHandler {
}

// Define a function to stop translation and clean up
stopTranslation() {
stopTranslation = () => {
this.stopTranslate();
this.syncVideoVolumeSlider();
}
};

async handleSrcChanged() {
debug.log("[VideoHandler] src changed", this);
Expand Down
Loading

0 comments on commit 49356a6

Please sign in to comment.