From f7f4dfa0035d599c3fb3b384c01a7ca4c1b090a1 Mon Sep 17 00:00:00 2001 From: R-J Lim Date: Sat, 3 Aug 2024 21:55:34 -0700 Subject: [PATCH] Use embed URLs for URL field on youtube videos --- .../src/controllers/anki-ui-controller.ts | 2 +- extension/src/services/binding.ts | 22 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/extension/src/controllers/anki-ui-controller.ts b/extension/src/controllers/anki-ui-controller.ts index bb4900c9..ec1b330c 100755 --- a/extension/src/controllers/anki-ui-controller.ts +++ b/extension/src/controllers/anki-ui-controller.ts @@ -80,7 +80,7 @@ export default class AnkiUiController { this._prepareShow(context); const client = await this._client(context); - const url = context.url; + const url = context.url(subtitle.start, subtitle.end); const themeType = await context.settings.getSingle('themeType'); const state: AnkiUiInitialState = { diff --git a/extension/src/services/binding.ts b/extension/src/services/binding.ts index 27bc870d..95a86393 100755 --- a/extension/src/services/binding.ts +++ b/extension/src/services/binding.ts @@ -70,6 +70,8 @@ document.addEventListener('asbplayer-netflix-enabled', (e) => { }); document.dispatchEvent(new CustomEvent('asbplayer-query-netflix')); +const youtube = /(m|www)\.youtube\.com/.test(window.location.host); + enum RecordingState { requested, started, @@ -174,10 +176,6 @@ export default class Binding { return this._synced; } - get url() { - return window.location !== window.parent.location ? document.referrer : document.location.href; - } - get speedChangeStep() { return this._speedChangeStep; } @@ -948,7 +946,7 @@ export default class Binding { surroundingSubtitles: surroundingSubtitles, record: this.recordMedia, screenshot: this.takeScreenshot, - url: this.url, + url: this.url(subtitle.start, subtitle.end), mediaTimestamp: this.video.currentTime * 1000, subtitleFileName: this.subtitleFileName(subtitle.track), postMineAction: postMineAction, @@ -985,7 +983,7 @@ export default class Binding { playbackRate: this.video.playbackRate, screenshot: this.recordingMediaWithScreenshot, videoDuration: this.video.duration * 1000, - url: this.url, + url: this.url(this.recordingMediaStartedTimestamp!, currentTimestamp), subtitleFileName: this.subtitleFileName(), ...this._imageCaptureParams, ...this._surroundingSubtitlesAroundInterval(this.recordingMediaStartedTimestamp!, currentTimestamp), @@ -1022,7 +1020,7 @@ export default class Binding { record: this.recordMedia, postMineAction: postMineAction, screenshot: this.takeScreenshot, - url: this.url, + url: this.url(timestamp), subtitleFileName: this.subtitleFileName(), imageDelay: this.imageDelay, ...this._imageCaptureParams, @@ -1365,4 +1363,14 @@ export default class Binding { chrome.runtime.sendMessage(command); } + + url(start: number, end?: number) { + if (youtube) { + const toSeconds = (ms: number) => Math.floor(ms / 1000); + const embedUrl = `https://www.youtube.com/embed/Khqwm3np9wc?start=${toSeconds(start)}&autoplay=1`; + return end === undefined ? embedUrl : `${embedUrl}&end=${toSeconds(end)}`; + } + + return window.location !== window.parent.location ? document.referrer : document.location.href; + } }