Skip to content

Commit 920c06d

Browse files
committed
Code cleanup
1 parent e673d35 commit 920c06d

File tree

9 files changed

+26
-198
lines changed

9 files changed

+26
-198
lines changed

injected/src/features.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ const otherFeatures = /** @type {const} */ ([
3333
/** @typedef {baseFeatures[number]|otherFeatures[number]} FeatureName */
3434
/** @type {Record<string, FeatureName[]>} */
3535
export const platformSupport = {
36-
apple: ['webCompat', 'duckPlayerNative',...baseFeatures],
37-
'apple-isolated': ['duckPlayer', 'duckPlayerNative', 'brokerProtection', 'performanceMetrics', 'clickToLoad', 'messageBridge', 'favicon'],
36+
apple: ['webCompat', 'duckPlayerNative', ...baseFeatures],
37+
'apple-isolated': [
38+
'duckPlayer',
39+
'duckPlayerNative',
40+
'brokerProtection',
41+
'performanceMetrics',
42+
'clickToLoad',
43+
'messageBridge',
44+
'favicon',
45+
],
3846
android: [...baseFeatures, 'webCompat', 'breakageReporting', 'duckPlayer', 'messageBridge'],
3947
'android-broker-protection': ['brokerProtection'],
4048
'android-autofill-password-import': ['autofillPasswordImport'],

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ContentFeature from '../content-feature.js';
22
import { isBeingFramed } from '../utils.js';
33
import { DuckPlayerNativeMessages } from './duckplayer-native/messages.js';
4-
import { mockTransport } from './duckplayer-native/mock-transport.js';
54
import { setupDuckPlayerForNoCookie, setupDuckPlayerForSerp, setupDuckPlayerForYouTube } from './duckplayer-native/duckplayer-native.js';
65
import { Environment } from './duckplayer-native/environment.js';
76

@@ -39,18 +38,13 @@ export class DuckPlayerNativeFeature extends ContentFeature {
3938

4039
const locale = args?.locale || args?.language || 'en';
4140
const env = new Environment({
42-
debug: this.isDebug || true, // TODO: REMOVE
41+
debug: this.isDebug,
4342
injectName: import.meta.injectName,
4443
platform: this.platform,
4544
locale,
4645
});
4746

48-
if (env.isIntegrationMode()) {
49-
// TODO: Better way than patching transport?
50-
this.messaging.transport = mockTransport();
51-
}
52-
53-
const messages = new DuckPlayerNativeMessages(this.messaging);
47+
const messages = new DuckPlayerNativeMessages(this.messaging, env);
5448
messages.subscribeToURLChange(({ pageType }) => {
5549
const playbackPaused = false; // TODO: Get this from the native notification too?
5650
console.log('GOT PAGE TYPE', pageType);
@@ -60,7 +54,6 @@ export class DuckPlayerNativeFeature extends ContentFeature {
6054
/** @type {InitialSettings} */
6155
let initialSetup;
6256

63-
// TODO: This seems to get initted twice. Check with Daniel
6457
try {
6558
initialSetup = await messages.initialSetup();
6659
} catch (e) {

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { Logger, SideEffects } from './util.js';
1818
* @typedef {(SideEffects, Logger) => void} CustomEventHandler
1919
* @typedef {DuckPlayerNativeSettings['selectors']} DuckPlayerNativeSelectors
2020
*/
21-
// TODO: Abort controller?
2221

2322
export class DuckPlayerNativePage {
2423
/** @type {SideEffects} */
@@ -49,8 +48,6 @@ export class DuckPlayerNativePage {
4948
throw new Error('Missing arguments');
5049
}
5150

52-
console.log('SELECTORS', selectors);
53-
5451
this.setupLogger();
5552

5653
this.onLoad = onLoad || (() => {});
@@ -108,7 +105,6 @@ export class DuckPlayerNativePage {
108105
*/
109106
export function setupDuckPlayerForYouTube(selectors, playbackPaused, environment, messages) {
110107
const mediaControlHandler = (sideEffects, logger, pause) => {
111-
console.log('MEDIA CONTROL', pause); // TODO: Remove
112108
logger.log('Running media control handler. Pause:', pause);
113109

114110
const videoElement = selectors?.videoElement;
@@ -147,15 +143,13 @@ export function setupDuckPlayerForYouTube(selectors, playbackPaused, environment
147143
});
148144

149145
if (playbackPaused) {
150-
console.log('PAUSING VIDEO');
151146
mediaControlHandler(sideEffects, logger, !!playbackPaused);
152147
}
153148
};
154149

155150
const onInit = (sideEffects, logger) => {
156151
sideEffects.add('subscribe to media control', () => {
157152
return messages.subscribeToMediaControl(({ pause }) => {
158-
console.log('GOT MC SUB');
159153
mediaControlHandler(sideEffects, logger, pause);
160154
});
161155
});
@@ -247,7 +241,6 @@ export function setupDuckPlayerForNoCookie(selectors, environment, messages) {
247241
*/
248242
export function setupDuckPlayerForSerp(selectors, environment, messages) {
249243
const onLoad = () => {
250-
console.log('SERP NOTIFY');
251244
serpNotify();
252245
};
253246

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// TODO: Update locales
21
import strings from '../../../../build/locales/duckplayer-locales.js';
32

43
export class Environment {

injected/src/features/duckplayer-native/get-current-timestamp.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,31 @@
55
* @returns {number}
66
*/
77
export function getCurrentTimestamp(selector) {
8-
const video = /** @type {HTMLVideoElement|null} */ (document.querySelector("video")); // TODO: Return to remote config
8+
const video = /** @type {HTMLVideoElement|null} */ (document.querySelector(selector));
99
return video?.currentTime || 0;
1010
}
1111

1212
/**
13-
* Sends the timestamp to the browser every 300ms
14-
* TODO: Can we not brute force this?
15-
*/
16-
/**
17-
* Sends the timestamp to the browser every 300ms
18-
* TODO: Can we not brute force this?
13+
* Sends the timestamp to the browser at an interval
14+
*
1915
* @param {number} interval - Polling interval
2016
* @param {(number) => void} callback - Callback handler for polling event
2117
* @param {DuckPlayerNativeSelectors} selectors - Selectors for the player
2218
* @returns
2319
*/
2420
export function pollTimestamp(interval = 300, callback, selectors) {
25-
window.addEventListener("message", function(event) {
26-
console.log('MESSAGE', event);
27-
});
2821
if (!callback || !selectors) {
2922
console.error('Timestamp polling failed. No callback or selectors defined');
23+
return;
3024
}
3125

3226
const isShowingAd = () => {
3327
return selectors.adShowing && !!document.querySelector(selectors.adShowing);
3428
};
3529

3630
const timestampPolling = setInterval(() => {
37-
if (isShowingAd()) {
38-
console.log('Ad showing. Not polling timestamp');
39-
return;
40-
}
31+
if (isShowingAd()) return;
4132
const timestamp = getCurrentTimestamp(selectors.videoElement);
42-
console.log('Polling timestamp', timestamp);
4333
callback(timestamp);
4434
}, interval);
4535

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

Lines changed: 0 additions & 149 deletions
This file was deleted.

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as constants from './constants.js';
2+
import { mockTransport } from './mock-transport.js';
23

34
/** @import {YouTubeError} from './error-detection.js' */
5+
/** @import {Environment} from './environment.js' */
46

57
/**
68
* @typedef {object} MuteSettings - Settings passed to the onMute callback
@@ -32,20 +34,19 @@ import * as constants from './constants.js';
3234
export class DuckPlayerNativeMessages {
3335
/**
3436
* @param {Messaging} messaging
37+
* @param {Environment} environment
3538
* @internal
3639
*/
37-
constructor(messaging) {
40+
constructor(messaging, environment) {
3841
/**
3942
* @internal
4043
*/
4144
this.messaging = messaging;
42-
// this.environment = environment;
43-
// TODO: Replace with class if needed
44-
this.environment = {
45-
isIntegrationMode: function () {
46-
return true;
47-
},
48-
};
45+
this.environment = environment;
46+
47+
if (this.environment.isIntegrationMode()) {
48+
this.messaging.transport = mockTransport(); // TODO: Better way than patching transport?
49+
}
4950
}
5051

5152
/**
@@ -84,7 +85,6 @@ export class DuckPlayerNativeMessages {
8485
* @param {(urlSettings: UrlChangeSettings) => void} callback
8586
*/
8687
subscribeToURLChange(callback) {
87-
console.log('SUBSCRIBING TO URL CHANGE');
8888
return this.messaging.subscribe(constants.MSG_NAME_URL_CHANGE, callback);
8989
}
9090

@@ -100,23 +100,20 @@ export class DuckPlayerNativeMessages {
100100
* Notifies browser that the feature is ready
101101
*/
102102
notifyFeatureIsReady() {
103-
console.log('FIRING', constants.MSG_NAME_FEATURE_READY); // TODO: Remove
104103
this.messaging.notify(constants.MSG_NAME_FEATURE_READY, {});
105104
}
106105

107106
/**
108107
* Notifies browser that scripts are ready to be acalled
109108
*/
110109
notifyScriptIsReady() {
111-
console.log('FIRING', constants.MSG_NAME_SCRIPTS_READY); // TODO: Remove
112110
this.messaging.notify(constants.MSG_NAME_SCRIPTS_READY, {});
113111
}
114112

115113
/**
116114
* Notifies browser that the overlay was dismissed
117115
*/
118116
notifyOverlayDismissed() {
119-
console.log('FIRING', constants.MSG_NAME_DISMISS_OVERLAY); // TODO: Remove
120117
this.messaging.notify(constants.MSG_NAME_DISMISS_OVERLAY, {});
121118
}
122119
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class TestTransport {
6868
break;
6969
case constants.MSG_NAME_URL_CHANGE:
7070
response = { pageType: this.getPageType() };
71-
console.log('PAGE TYPE', response);
7271
timeout = 100;
7372
break;
7473
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// TODO: This was copied from Duck Player. May need a cleanup
2-
31
/* eslint-disable promise/prefer-await-to-then */
42
/**
53
* Add an event listener to an element that is only executed if it actually comes from a user action

0 commit comments

Comments
 (0)