From d7cdd1d887eec323f053921424abc7fae575310a Mon Sep 17 00:00:00 2001 From: daniel k Date: Sun, 15 Oct 2023 22:15:14 +0900 Subject: [PATCH] fix embed conflict --- doc/description.md | 2 + package-lock.json | 50 ++++++++++++------------ package.json | 4 +- public/manifest.json | 2 +- src/subtitle/youtube.js | 86 ++++++++++++++++++++++++----------------- 5 files changed, 81 insertions(+), 63 deletions(-) diff --git a/doc/description.md b/doc/description.md index e89b2fe0..0e270d69 100644 --- a/doc/description.md +++ b/doc/description.md @@ -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) diff --git a/package-lock.json b/package-lock.json index 815bd4f4..3b104078 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,14 @@ { "name": "mouse_tooltip_translator", - "version": "0.1.87", + "version": "0.1.88", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mouse_tooltip_translator", - "version": "0.1.87", + "version": "0.1.88", "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", @@ -231,16 +231,16 @@ "dev": true }, "node_modules/@mswjs/interceptors": { - "version": "0.24.1", - "resolved": "https://registry.npmjs.org/@mswjs/interceptors/-/interceptors-0.24.1.tgz", - "integrity": "sha512-fCH1t3yc8JW3gMqtkmO/L+kII7GAXWw9bTmCZBsrwSFKkG1y5RmNZN9BEygmWMJGxjdjAp3JTLAQpnBqLV6wpA==", + "version": "0.25.7", + "resolved": "https://registry.npmjs.org/@mswjs/interceptors/-/interceptors-0.25.7.tgz", + "integrity": "sha512-U7iFYs/qU/5jfz1VDpoYz3xqX9nzhsBXw7q923dv6GiGTy+m2ZLhD33L80R/shHOW/YWjeH6k16GbIHGw+bAng==", "dependencies": { - "@open-draft/deferred-promise": "^2.1.0", + "@open-draft/deferred-promise": "^2.2.0", "@open-draft/logger": "^0.3.0", "@open-draft/until": "^2.0.0", "is-node-process": "^1.2.0", "outvariant": "^1.2.1", - "strict-event-emitter": "^0.5.0" + "strict-event-emitter": "^0.5.1" }, "engines": { "node": ">=18" @@ -295,9 +295,9 @@ } }, "node_modules/@open-draft/deferred-promise": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@open-draft/deferred-promise/-/deferred-promise-2.1.0.tgz", - "integrity": "sha512-Rzd5JrXZX8zErHzgcGyngh4fmEbSHqTETdGj9rXtejlqMIgXFlyKBA7Jn1Xp0Ls0M0Y22+xHcWiEzbmdWl0BOA==" + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@open-draft/deferred-promise/-/deferred-promise-2.2.0.tgz", + "integrity": "sha512-CecwLWx3rhxVQF6V4bAgPS5t+So2sTbPgAzafKkVizyi7tlwpcFpdFqq+wqF2OwNBmqFuu6tOyouTuxgpMfzmA==" }, "node_modules/@open-draft/logger": { "version": "0.3.0", @@ -5478,9 +5478,9 @@ } }, "node_modules/strict-event-emitter": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/strict-event-emitter/-/strict-event-emitter-0.5.0.tgz", - "integrity": "sha512-sqnMpVJLSB3daNO6FcvsEk4Mq5IJeAwDeH80DP1S8+pgxrF6yZnE1+VeapesGled7nEcIkz1Ax87HzaIy+02kA==" + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/strict-event-emitter/-/strict-event-emitter-0.5.1.tgz", + "integrity": "sha512-vMgjE/GGEPEFnhFub6pa4FmJBRBVOLpIII2hvCZ8Kzb7K0hlHo7mQv6xYrBvCL2LtAIBwFUK8wvuJgTVSQ5MFQ==" }, "node_modules/strict-uri-encode": { "version": "1.1.0", @@ -6665,16 +6665,16 @@ "dev": true }, "@mswjs/interceptors": { - "version": "0.24.1", - "resolved": "https://registry.npmjs.org/@mswjs/interceptors/-/interceptors-0.24.1.tgz", - "integrity": "sha512-fCH1t3yc8JW3gMqtkmO/L+kII7GAXWw9bTmCZBsrwSFKkG1y5RmNZN9BEygmWMJGxjdjAp3JTLAQpnBqLV6wpA==", + "version": "0.25.7", + "resolved": "https://registry.npmjs.org/@mswjs/interceptors/-/interceptors-0.25.7.tgz", + "integrity": "sha512-U7iFYs/qU/5jfz1VDpoYz3xqX9nzhsBXw7q923dv6GiGTy+m2ZLhD33L80R/shHOW/YWjeH6k16GbIHGw+bAng==", "requires": { - "@open-draft/deferred-promise": "^2.1.0", + "@open-draft/deferred-promise": "^2.2.0", "@open-draft/logger": "^0.3.0", "@open-draft/until": "^2.0.0", "is-node-process": "^1.2.0", "outvariant": "^1.2.1", - "strict-event-emitter": "^0.5.0" + "strict-event-emitter": "^0.5.1" } }, "@nodelib/fs.scandir": { @@ -6713,9 +6713,9 @@ } }, "@open-draft/deferred-promise": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@open-draft/deferred-promise/-/deferred-promise-2.1.0.tgz", - "integrity": "sha512-Rzd5JrXZX8zErHzgcGyngh4fmEbSHqTETdGj9rXtejlqMIgXFlyKBA7Jn1Xp0Ls0M0Y22+xHcWiEzbmdWl0BOA==" + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@open-draft/deferred-promise/-/deferred-promise-2.2.0.tgz", + "integrity": "sha512-CecwLWx3rhxVQF6V4bAgPS5t+So2sTbPgAzafKkVizyi7tlwpcFpdFqq+wqF2OwNBmqFuu6tOyouTuxgpMfzmA==" }, "@open-draft/logger": { "version": "0.3.0", @@ -10646,9 +10646,9 @@ } }, "strict-event-emitter": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/strict-event-emitter/-/strict-event-emitter-0.5.0.tgz", - "integrity": "sha512-sqnMpVJLSB3daNO6FcvsEk4Mq5IJeAwDeH80DP1S8+pgxrF6yZnE1+VeapesGled7nEcIkz1Ax87HzaIy+02kA==" + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/strict-event-emitter/-/strict-event-emitter-0.5.1.tgz", + "integrity": "sha512-vMgjE/GGEPEFnhFub6pa4FmJBRBVOLpIII2hvCZ8Kzb7K0hlHo7mQv6xYrBvCL2LtAIBwFUK8wvuJgTVSQ5MFQ==" }, "strict-uri-encode": { "version": "1.1.0", diff --git a/package.json b/package.json index 8b6d0b3b..7dd5bb40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mouse_tooltip_translator", - "version": "0.1.88", + "version": "0.1.89", "description": "Mouse over to translate", "private": true, "scripts": { @@ -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", diff --git a/public/manifest.json b/public/manifest.json index 8b7565d2..06bf2f0a 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -1,6 +1,6 @@ { "manifest_version": 3, - "version": "0.1.88", + "version": "0.1.89", "name": "__MSG_appName__", "description": "__MSG_appDesc__", "default_locale": "en", diff --git a/src/subtitle/youtube.js b/src/subtitle/youtube.js index 2da6f4d4..cae6c982 100644 --- a/src/subtitle/youtube.js +++ b/src/subtitle/youtube.js @@ -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; @@ -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; @@ -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() { @@ -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 @@ -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"); @@ -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; }