Skip to content

Commit 0ea641b

Browse files
committed
Thumbnail is showing
1 parent 440d8a1 commit 0ea641b

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

injected/src/features/duckplayer-native/duckplayer-native.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ export async function initDuckPlayerNative(messages) {
4040

4141
// TODO: move to settings.selectors.videoElement/videoElementContainer or something similar
4242
const videoElementContainer = document.querySelector('#player .html5-video-player');
43-
const videoElement = document.querySelector('#player video');
44-
if (videoElement && videoElementContainer) {
43+
const videoSelector = '#player video';
44+
if (videoElementContainer) {
4545
sideEffects.push(
46-
stopVideoFromPlaying(/** @type {HTMLVideoElement} */ (videoElement)),
46+
stopVideoFromPlaying(videoSelector),
4747
appendThumbnailOverlay(/** @type {HTMLElement} */ (videoElementContainer)),
4848
);
4949
}

injected/src/features/duckplayer-native/error-detection.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ export class ErrorDetection {
154154
if (node?.nodeType === Node.ELEMENT_NODE) {
155155
const element = /** @type {HTMLElement} */ (node);
156156
// Check if element has the error class or contains any children with that class
157-
return element.classList.contains('ytp-error') || !!element.querySelector('.ytp-error');
157+
const isError = element.classList.contains('ytp-error') || !!element.querySelector('.ytp-error');
158+
console.log('Is error?', isError);
159+
return isError;
158160
}
159161

160162
return false;

injected/src/features/duckplayer-native/overlays/thumbnail-overlay.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import mobilecss from './thumbnail-overlay.css';
22
import { createPolicy, html } from '../../../dom-utils.js';
33
import { customElementsDefine, customElementsGet } from '../../../captured-globals.js';
4+
import { VideoParams, appendImageAsBackground } from '../../duckplayer/util';
45

56
export function registerCustomElements() {
67
if (!customElementsGet(DDGVideoThumbnailOverlay.CUSTOM_TAG_NAME)) {
@@ -18,6 +19,8 @@ export class DDGVideoThumbnailOverlay extends HTMLElement {
1819
policy = createPolicy();
1920
/** @type {boolean} */
2021
testMode = false;
22+
/** @type {HTMLElement} */
23+
container;
2124

2225
connectedCallback() {
2326
this.createMarkupAndStyles();
@@ -32,6 +35,43 @@ export class DDGVideoThumbnailOverlay extends HTMLElement {
3235
container.innerHTML = this.policy.createHTML(content);
3336
shadow.append(style, container);
3437
this.container = container;
38+
this.appendThumbnail();
39+
}
40+
41+
appendThumbnail() {
42+
const params = VideoParams.forWatchPage(this.getPlayerPageHref());
43+
const videoId = params?.id;
44+
45+
const imageUrl = this.getLargeThumbnailSrc(videoId);
46+
console.log('Image url', imageUrl);
47+
appendImageAsBackground(this.container, '.ddg-vpo-bg', imageUrl);
48+
}
49+
50+
getLargeThumbnailSrc(videoId) {
51+
const url = new URL(`/vi/${videoId}/maxresdefault.jpg`, 'https://i.ytimg.com');
52+
return url.href;
53+
}
54+
55+
/**
56+
* This is the URL of the page that the user is currently on
57+
* It's abstracted so that we can mock it in tests
58+
* @return {string}
59+
*/
60+
getPlayerPageHref() {
61+
if (this.testMode) {
62+
const url = new URL(window.location.href);
63+
if (url.hostname === 'www.youtube.com') return window.location.href;
64+
65+
// reflect certain query params, this is useful for testing
66+
if (url.searchParams.has('v')) {
67+
const base = new URL('/watch', 'https://youtube.com');
68+
base.searchParams.set('v', url.searchParams.get('v') || '');
69+
return base.toString();
70+
}
71+
72+
return 'https://youtube.com/watch?v=123';
73+
}
74+
return window.location.href;
3575
}
3676

3777
/**
@@ -47,6 +87,10 @@ export class DDGVideoThumbnailOverlay extends HTMLElement {
4787
}
4888
}
4989

90+
91+
92+
93+
5094
/**
5195
*
5296
* @param {HTMLElement} targetElement
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/**
22
*
3-
* @param {HTMLVideoElement} videoElement
3+
* @param {string} videoSelector
44
*/
5-
export function stopVideoFromPlaying(videoElement) {
5+
export function stopVideoFromPlaying(videoSelector) {
66
console.log('Setting up video pause');
77
/**
88
* Set up the interval - keep calling .pause() to prevent
99
* the video from playing
1010
*/
1111
const int = setInterval(() => {
12-
if (videoElement?.isConnected) {
13-
videoElement.pause();
12+
const video = /** @type {HTMLVideoElement} */ (document.querySelector(videoSelector));
13+
if (video?.isConnected) {
14+
video.pause();
1415
}
1516
}, 10);
1617

@@ -21,8 +22,9 @@ export function stopVideoFromPlaying(videoElement) {
2122
return () => {
2223
clearInterval(int);
2324

24-
if (videoElement?.isConnected) {
25-
videoElement.play();
25+
const video = /** @type {HTMLVideoElement} */ (document.querySelector(videoSelector));
26+
if (video?.isConnected) {
27+
video.play();
2628
}
2729
};
2830
}

0 commit comments

Comments
 (0)