@@ -162,6 +162,104 @@ export function setupDuckPlayerForYouTube(settings, environment, messages) {
162
162
return duckPlayerNative ;
163
163
}
164
164
165
+ /**
166
+ * @param {Settings } settings
167
+ * @param {Environment } environment
168
+ * @param {DuckPlayerNativeMessages } messages
169
+ */
170
+ export function setupDuckPlayerForEverything ( settings , environment , messages ) {
171
+ const onLoad = ( sideEffects , logger ) => {
172
+ sideEffects . add ( 'started polling current timestamp' , ( ) => {
173
+ const handler = ( timestamp ) => {
174
+ messages . notifyCurrentTimestamp ( timestamp . toFixed ( 0 ) ) ;
175
+ } ;
176
+
177
+ return pollTimestamp ( 300 , handler ) ;
178
+ } ) ;
179
+
180
+ const errorContainer = settings . selectors ?. errorContainer ;
181
+ const signInRequiredError = settings . selectors ?. signInRequiredError ;
182
+ if ( ! errorContainer || ! signInRequiredError ) {
183
+ logger . warn ( 'Missing error selectors in configuration' ) ;
184
+ return ;
185
+ }
186
+
187
+ /** @type {(errorId: import('./error-detection.js').YouTubeError) => void } */
188
+ const errorHandler = ( errorId ) => {
189
+ logger . log ( 'Received error' , errorId ) ;
190
+
191
+ // Notify the browser of the error
192
+ messages . notifyYouTubeError ( errorId ) ;
193
+
194
+ const targetElement = document . querySelector ( errorContainer ) ;
195
+ if ( targetElement ) {
196
+ showError ( /** @type {HTMLElement } */ ( targetElement ) , errorId , environment ) ;
197
+ }
198
+ } ;
199
+
200
+ /** @type {ErrorDetectionSettings } */
201
+ const errorDetectionSettings = {
202
+ selectors : settings . selectors ,
203
+ testMode : environment . isTestMode ( ) ,
204
+ callback : errorHandler ,
205
+ } ;
206
+
207
+ sideEffects . add ( 'setting up error detection' , ( ) => {
208
+ const errorDetection = new ErrorDetection ( errorDetectionSettings ) ;
209
+ const destroy = errorDetection . observe ( ) ;
210
+
211
+ return ( ) => {
212
+ if ( destroy ) destroy ( ) ;
213
+ } ;
214
+ } ) ;
215
+ } ;
216
+
217
+ const onInit = ( sideEffects , logger ) => {
218
+ sideEffects . add ( 'subscribe to media control' , ( ) => {
219
+ return messages . subscribeToMediaControl ( ( { pause } ) => {
220
+ logger . log ( 'Running media control handler. Pause:' , pause ) ;
221
+
222
+ const videoElement = settings . selectors ?. videoElement ;
223
+ const videoElementContainer = settings . selectors ?. videoElementContainer ;
224
+ if ( ! videoElementContainer || ! videoElement ) {
225
+ logger . warn ( 'Missing media control selectors in config' ) ;
226
+ return ;
227
+ }
228
+
229
+ const targetElement = document . querySelector ( videoElementContainer ) ;
230
+ if ( targetElement ) {
231
+
232
+ if ( pause ) {
233
+ sideEffects . add ( 'stopping video from playing' , ( ) => stopVideoFromPlaying ( videoElement ) ) ;
234
+ sideEffects . add ( 'appending thumbnail' , ( ) =>
235
+ appendThumbnailOverlay ( /** @type {HTMLElement } */ ( targetElement ) , environment ) ,
236
+ ) ;
237
+ } else {
238
+ sideEffects . destroy ( 'stopping video from playing' ) ;
239
+ sideEffects . destroy ( 'appending thumbnail' ) ;
240
+ }
241
+ }
242
+ } ) ;
243
+ } )
244
+
245
+ sideEffects . add ( 'subscribing to mute audio' , ( ) => {
246
+ return messages . subscribeToMuteAudio ( ( { mute } ) => {
247
+ logger . log ( 'Running mute audio handler. Mute:' , mute ) ;
248
+ muteAudio ( mute ) ;
249
+ } ) ;
250
+ } ) ;
251
+ } ;
252
+
253
+ const duckPlayerNative = new DuckPlayerNative ( {
254
+ settings,
255
+ environment,
256
+ messages,
257
+ onInit,
258
+ onLoad,
259
+ } ) ;
260
+ return duckPlayerNative ;
261
+ }
262
+
165
263
/**
166
264
* @param {Settings } settings
167
265
* @param {Environment } environment
0 commit comments