Skip to content

Commit

Permalink
fix embed conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
ttop32 committed Oct 15, 2023
1 parent fc1c2de commit d7cdd1d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 63 deletions.
2 changes: 2 additions & 0 deletions doc/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ English, Russian, Japanese, Chinese and so on

# Change Log

- 0.1.89
- fix youtube embed conflict (request by BH J)
- 0.1.88
- fix youtube sub conflict
- add voice target option (request by trionline1234)
Expand Down
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mouse_tooltip_translator",
"version": "0.1.88",
"version": "0.1.89",
"description": "Mouse over to translate",
"private": true,
"scripts": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"webpack-merge": "^5.7.3"
},
"dependencies": {
"@mswjs/interceptors": "^0.24.1",
"@mswjs/interceptors": "^0.25.7",
"async-wait-until": "^2.0.12",
"bcp-47": "^2.0.0",
"bootstrap": "^4.5.3",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"manifest_version": 3,
"version": "0.1.88",
"version": "0.1.89",
"name": "__MSG_appName__",
"description": "__MSG_appDesc__",
"default_locale": "en",
Expand Down
86 changes: 51 additions & 35 deletions src/subtitle/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { waitUntil, WAIT_FOREVER } from "async-wait-until";
import delay from "delay";

const interceptor = new XMLHttpRequestInterceptor();
interceptor.apply();
var interceptorLoaded = false;
var targetLang = "";
var subSetting = "";
var subStartDelayTime = 2000;
var subStartDelayTime = 1500;
var googleTrafficDelayTime = 0;
var failSkipTime = 5000;
var pausedByExtension = false;
Expand All @@ -37,38 +37,6 @@ window.addEventListener(
false
);

// check any subtitle request and concat dual sub
interceptor.on("request", async ({ request, requestId }) => {
try {
// do sub concat when activation subtitle is done
if (
request.url.includes("www.youtube.com/api/timedtext") &&
activatedVideoId == getVideoIdParam(request.url)
) {
//get source lang sub
var response = await requestSubtitle(request.url);
var sub1 = await response.json();
var sub1 = concatWordSub(sub1);
var lang = getSearchParam(request.url, "lang");
var responseSub = sub1;

//get target lang sub, if not same lang
if (lang != targetLang && subSetting == "dualsub") {
await delay(googleTrafficDelayTime); //prevent google traffic error
var sub2 = await getTranslatedSubtitle(request.url, targetLang);
var mergedSub = mergeSubtitles(sub1, sub2);
responseSub = mergedSub;
}

request.respondWith(new Response(JSON.stringify(responseSub), response));
}
} catch (error) {
console.log(error);
failTimestamp = Date.now();
await delay(googleTrafficDelayTime); //prevent traffic error
}
});

// listener start ========================================
async function initPlayer(data) {
targetLang = data.targetLang;
Expand All @@ -79,6 +47,11 @@ async function initPlayer(data) {
addPlayerStartListener();
addUrlListener();
activateCaption();

//if not embed, load interceptor directly else load when start video // embed has interceptor conflict
if (!isEmbed(window.location.href)) {
loadInterceptor();
}
}

async function addPlayerStartListener() {
Expand All @@ -89,6 +62,7 @@ async function addPlayerStartListener() {
element.addEventListener("onStateChange", (e) => {
// turn on caption when first loads a video
if (e == -1) {
loadInterceptor();
activateCaption();
}
//check pause
Expand All @@ -110,6 +84,48 @@ function addUrlListener() {
});
}

//intercept sub request ===================================================================
// check any subtitle request and concat dual sub
function loadInterceptor() {
if (interceptorLoaded == true) {
return;
}
interceptorLoaded = true;
interceptor.apply();
interceptor.on("request", async ({ request, requestId }) => {
try {
// do sub concat when activation subtitle is done
if (
request.url.includes("www.youtube.com/api/timedtext") &&
activatedVideoId == getVideoIdParam(request.url)
) {
//get source lang sub
var response = await requestSubtitle(request.url);
var sub1 = await response.json();
var sub1 = concatWordSub(sub1);
var lang = getSearchParam(request.url, "lang");
var responseSub = sub1;

//get target lang sub, if not same lang
if (lang != targetLang && subSetting == "dualsub") {
await delay(googleTrafficDelayTime); //prevent google traffic error
var sub2 = await getTranslatedSubtitle(request.url, targetLang);
var mergedSub = mergeSubtitles(sub1, sub2);
responseSub = mergedSub;
}

request.respondWith(
new Response(JSON.stringify(responseSub), response)
);
}
} catch (error) {
console.log(error);
failTimestamp = Date.now();
await delay(googleTrafficDelayTime); //prevent traffic error
}
});
}

// handle player ==================================================
function getVideoPlayer() {
return document.querySelector(".html5-video-player");
Expand Down Expand Up @@ -178,7 +194,7 @@ const activateCaption = debounce(
async (url = window.location.href) => {
// do not turn on caption if user off
// if is shorts skip
// || isShorts(url)

if (captionOnStatusByUser == "false" || !isVideoUrl(url)) {
return;
}
Expand Down

0 comments on commit d7cdd1d

Please sign in to comment.