Skip to content

Commit 17d3bac

Browse files
committed
Cleanup
1 parent bb139b5 commit 17d3bac

File tree

2 files changed

+62
-52
lines changed

2 files changed

+62
-52
lines changed

injected/src/features/duck-player-native.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { setupDuckPlayerForNoCookie, setupDuckPlayerForSerp, setupDuckPlayerForY
66
import { Environment } from './duckplayer-native/environment.js';
77

88
/**
9-
* @import {DuckPlayerNative} from './duckplayer-native/duckplayer-native.js'
9+
* @import {DuckPlayerNativePage} from './duckplayer-native/duckplayer-native.js'
1010
* @import {DuckPlayerNativeSettings} from '@duckduckgo/privacy-configuration/schema/features/duckplayer-native.js'
1111
* @import {UrlChangeSettings} from './duckplayer-native/messages.js'
1212
*/
@@ -15,23 +15,20 @@ import { Environment } from './duckplayer-native/environment.js';
1515
* @typedef InitialSettings - The initial payload used to communicate render-blocking information
1616
* @property {string} locale - UI locale
1717
* @property {UrlChangeSettings['pageType']} pageType - The type of the current page
18+
* @property {boolean} playbackPaused - Should video start playing or paused
1819
*/
1920

2021
export class DuckPlayerNativeFeature extends ContentFeature {
21-
/** @type {DuckPlayerNative} */
22-
current;
22+
/** @type {DuckPlayerNativePage} */
23+
currentPage;
2324

2425
async init(args) {
2526
console.log('DUCK PLAYER NATIVE LOADING', args, window.location.href); // TODO: REMOVE
2627

27-
// TODO: May depend on page type
2828
/**
2929
* This feature never operates in a frame
3030
*/
31-
if (isBeingFramed()) {
32-
console.log('FRAMED. ABORTING.'); // TODO: REMOVE
33-
return;
34-
}
31+
if (isBeingFramed()) return;
3532

3633
const selectors = this.getFeatureSetting('selectors');
3734
console.log('DUCK PLAYER NATIVE SELECTORS', selectors); // TODO: REMOVE
@@ -44,17 +41,16 @@ export class DuckPlayerNativeFeature extends ContentFeature {
4441
locale,
4542
});
4643

47-
// TODO: Decide which feature to run
48-
4944
if (env.isIntegrationMode()) {
5045
// TODO: Better way than patching transport?
5146
this.messaging.transport = mockTransport();
5247
}
5348

5449
const messages = new DuckPlayerNativeMessages(this.messaging);
5550
messages.subscribeToURLChange(({ pageType }) => {
51+
const playbackPaused = false; // TODO: Get this from the native notification too?
5652
console.log('GOT PAGE TYPE', pageType);
57-
this.urlChangeHandler(pageType, selectors, env, messages);
53+
this.urlChanged(pageType, selectors, playbackPaused, env, messages);
5854
});
5955

6056
/** @type {InitialSettings} */
@@ -71,45 +67,48 @@ export class DuckPlayerNativeFeature extends ContentFeature {
7167
console.log('INITIAL SETUP', initialSetup);
7268

7369
if (initialSetup.pageType) {
70+
const playbackPaused = initialSetup.playbackPaused || false;
7471
console.log('GOT INITIAL PAGE TYPE', initialSetup.pageType); // TODO: REMOVE
75-
this.urlChangeHandler(initialSetup.pageType, selectors, env, messages);
72+
this.urlChanged(initialSetup.pageType, selectors, playbackPaused, env, messages);
7673
}
7774
}
7875

7976
/**
8077
*
8178
* @param {UrlChangeSettings['pageType']} pageType
8279
* @param {DuckPlayerNativeSettings['selectors']} selectors
80+
* @param {boolean} playbackPaused
8381
* @param {Environment} env
8482
* @param {DuckPlayerNativeMessages} messages
8583
*/
86-
urlChangeHandler(pageType, selectors, env, messages) {
87-
let next;
84+
urlChanged(pageType, selectors, playbackPaused, env, messages) {
85+
/** @type {DuckPlayerNativePage | null} */
86+
let nextPage = null;
8887

8988
switch (pageType) {
9089
case 'NOCOOKIE':
91-
next = setupDuckPlayerForNoCookie(selectors, env, messages);
90+
nextPage = setupDuckPlayerForNoCookie(selectors, env, messages);
9291
break;
9392
case 'YOUTUBE':
94-
next = setupDuckPlayerForYouTube(selectors, env, messages);
93+
nextPage = setupDuckPlayerForYouTube(selectors, playbackPaused, env, messages);
9594
break;
9695
case 'SERP':
97-
next = setupDuckPlayerForSerp(selectors, env, messages);
96+
nextPage = setupDuckPlayerForSerp(selectors, env, messages);
9897
break;
9998
case 'UNKNOWN':
10099
default:
101100
console.warn('No known pageType');
102101
}
103102

104-
if (this.current) {
105-
console.log('DESTROYING CURRENT INSTANCE', this.current);
106-
this.current.destroy();
103+
if (this.currentPage) {
104+
console.log('DESTROYING CURRENT INSTANCE', this.currentPage);
105+
this.currentPage.destroy();
107106
}
108107

109-
if (next) {
110-
console.log('LOADING NEXT INSTANCE', next);
111-
next.init();
112-
this.current = next;
108+
if (nextPage) {
109+
console.log('LOADING NEXT INSTANCE', nextPage);
110+
nextPage.init();
111+
this.currentPage = nextPage;
113112
}
114113
}
115114
}

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

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { Logger, SideEffects } from './util.js';
2020
*/
2121
// TODO: Abort controller?
2222

23-
export class DuckPlayerNative {
23+
export class DuckPlayerNativePage {
2424
/** @type {SideEffects} */
2525
sideEffects;
2626
/** @type {Logger} */
@@ -102,45 +102,56 @@ export class DuckPlayerNative {
102102

103103
/**
104104
* @param {DuckPlayerNativeSelectors} selectors
105+
* @param {boolean} playbackPaused
105106
* @param {Environment} environment
106107
* @param {DuckPlayerNativeMessages} messages
107108
*/
108-
export function setupDuckPlayerForYouTube(selectors, environment, messages) {
109-
const onLoad = (sideEffects) => {
109+
export function setupDuckPlayerForYouTube(selectors, playbackPaused, environment, messages) {
110+
const mediaControlHandler = (sideEffects, logger, pause) => {
111+
console.log('MEDIA CONTROL', pause); // TODO: Remove
112+
logger.log('Running media control handler. Pause:', pause);
113+
114+
const videoElement = selectors?.videoElement;
115+
const videoElementContainer = selectors?.videoElementContainer;
116+
if (!videoElementContainer || !videoElement) {
117+
logger.warn('Missing media control selectors in config');
118+
return;
119+
}
120+
121+
const targetElement = document.querySelector(videoElementContainer);
122+
if (targetElement) {
123+
if (pause) {
124+
sideEffects.add('stopping video from playing', () => stopVideoFromPlaying(videoElement));
125+
sideEffects.add('appending thumbnail', () =>
126+
appendThumbnailOverlay(/** @type {HTMLElement} */ (targetElement), environment),
127+
);
128+
} else {
129+
sideEffects.destroy('stopping video from playing');
130+
sideEffects.destroy('appending thumbnail');
131+
}
132+
}
133+
};
134+
135+
const onLoad = (sideEffects, logger) => {
110136
sideEffects.add('started polling current timestamp', () => {
111137
const handler = (timestamp) => {
112138
messages.notifyCurrentTimestamp(timestamp.toFixed(0));
113139
};
114140

115141
return pollTimestamp(300, handler);
116142
});
143+
144+
if (playbackPaused) {
145+
console.log('PAUSING VIDEO');
146+
mediaControlHandler(sideEffects, logger, !!playbackPaused);
147+
}
117148
};
118149

119150
const onInit = (sideEffects, logger) => {
120151
sideEffects.add('subscribe to media control', () => {
121152
return messages.subscribeToMediaControl(({ pause }) => {
122-
console.log('MEDIA CONTROL', pause); // TODO: Remove
123-
logger.log('Running media control handler. Pause:', pause);
124-
125-
const videoElement = selectors?.videoElement;
126-
const videoElementContainer = selectors?.videoElementContainer;
127-
if (!videoElementContainer || !videoElement) {
128-
logger.warn('Missing media control selectors in config');
129-
return;
130-
}
131-
132-
const targetElement = document.querySelector(videoElementContainer);
133-
if (targetElement) {
134-
if (pause) {
135-
sideEffects.add('stopping video from playing', () => stopVideoFromPlaying(videoElement));
136-
sideEffects.add('appending thumbnail', () =>
137-
appendThumbnailOverlay(/** @type {HTMLElement} */ (targetElement), environment),
138-
);
139-
} else {
140-
sideEffects.destroy('stopping video from playing');
141-
sideEffects.destroy('appending thumbnail');
142-
}
143-
}
153+
console.log('GOT MC SUB');
154+
mediaControlHandler(sideEffects, logger, pause);
144155
});
145156
});
146157

@@ -152,7 +163,7 @@ export function setupDuckPlayerForYouTube(selectors, environment, messages) {
152163
});
153164
};
154165

155-
const duckPlayerNative = new DuckPlayerNative({
166+
const duckPlayerNative = new DuckPlayerNativePage({
156167
selectors,
157168
environment,
158169
messages,
@@ -215,7 +226,7 @@ export function setupDuckPlayerForNoCookie(selectors, environment, messages) {
215226
});
216227
};
217228

218-
const duckPlayerNative = new DuckPlayerNative({
229+
const duckPlayerNative = new DuckPlayerNativePage({
219230
selectors,
220231
environment,
221232
messages,
@@ -235,7 +246,7 @@ export function setupDuckPlayerForSerp(selectors, environment, messages) {
235246
serpNotify();
236247
};
237248

238-
const duckPlayerNative = new DuckPlayerNative({
249+
const duckPlayerNative = new DuckPlayerNativePage({
239250
selectors,
240251
environment,
241252
messages,

0 commit comments

Comments
 (0)