Skip to content

Commit 0b186ee

Browse files
shakyShanemgurgel
authored andcommitted
Shane/shared demo (#1690)
* demo * more tweaks
1 parent 2a4d8d9 commit 0b186ee

File tree

17 files changed

+131
-492
lines changed

17 files changed

+131
-492
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ContentFeature from '../content-feature.js';
22
import { isBeingFramed } from '../utils.js';
33
import { DuckPlayerNativeMessages } from './duckplayer-native/messages.js';
44
import { setupDuckPlayerForNoCookie, setupDuckPlayerForSerp, setupDuckPlayerForYouTube } from './duckplayer-native/duckplayer-native.js';
5-
import { Environment } from './duckplayer-native/environment.js';
5+
import { Environment } from './duckplayer/environment.js';
66

77
/**
88
* @import {DuckPlayerNativePage} from './duckplayer-native/duckplayer-native.js'
@@ -18,7 +18,7 @@ import { Environment } from './duckplayer-native/environment.js';
1818
*/
1919

2020
export class DuckPlayerNativeFeature extends ContentFeature {
21-
/** @type {DuckPlayerNativePage} */
21+
/** @type {{init: () => void, destroy: () => void} | null} */
2222
currentPage;
2323

2424
async init(args) {

injected/src/features/duck-player.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import ContentFeature from '../content-feature.js';
3535

3636
import { DuckPlayerOverlayMessages, OpenInDuckPlayerMsg, Pixel } from './duckplayer/overlay-messages.js';
3737
import { isBeingFramed } from '../utils.js';
38-
import { Environment, initOverlays } from './duckplayer/overlays.js';
38+
import { initOverlays } from './duckplayer/overlays.js';
39+
import { Environment } from './duckplayer/environment.js';
3940

4041
/**
4142
* @typedef UserValues - A way to communicate user settings

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import css from './custom-error.css';
2-
import { Logger } from '../util';
2+
import { Logger } from '../../duckplayer/util.js';
33
import { createPolicy, html } from '../../../dom-utils.js';
44
import { customElementsDefine, customElementsGet } from '../../../captured-globals.js';
55

@@ -100,10 +100,10 @@ function getErrorStrings(errorId, t) {
100100
*
101101
* @param {HTMLElement} targetElement
102102
* @param {YouTubeError} errorId
103-
* @param {import('../environment').Environment} environment
103+
* @param {import('../../duckplayer/environment.js').Environment} environment
104104
*/
105105
export function showError(targetElement, errorId, environment) {
106-
const { title, messages } = getErrorStrings(errorId, environment.strings);
106+
const { title, messages } = getErrorStrings(errorId, environment.strings('native.json'));
107107
const logger = new Logger({
108108
id: 'CUSTOM_ERROR',
109109
shouldLog: () => environment.isTestMode(),

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import { ErrorDetection } from './error-detection.js';
55
import { appendThumbnailOverlay as showThumbnailOverlay } from './overlays/thumbnail-overlay.js';
66
import { stopVideoFromPlaying } from './pause-video.js';
77
import { showError } from './custom-error/custom-error.js';
8-
import { Logger, SideEffects } from './util.js';
8+
import { Logger, SideEffects } from '../duckplayer/util.js';
99

1010
/**
1111
* @import {DuckPlayerNativeMessages} from './messages.js'
12-
* @import {Environment} from './environment.js'
12+
* @import {Environment} from '../duckplayer/environment.js'
1313
* @import {ErrorDetectionSettings} from './error-detection.js'
1414
* @import {DuckPlayerNativeSettings} from "@duckduckgo/privacy-configuration/schema/features/duckplayer-native.js"
1515
*/
1616

1717
/**
18-
* @typedef {(SideEffects, Logger) => void} CustomEventHandler
18+
* @typedef {(effects: SideEffects, logger: Logger) => void} CustomEventHandler
1919
* @typedef {DuckPlayerNativeSettings['selectors']} DuckPlayerNativeSelectors
2020
*/
2121

@@ -32,12 +32,12 @@ export class DuckPlayerNativePage {
3232
messages;
3333
/**
3434
* Runs when an instance of this class is initialized
35-
* @type {CustomEventHandler}
35+
* @type {CustomEventHandler|undefined}
3636
*/
3737
onInit;
3838
/**
3939
* Runs after the current document has been loaded
40-
* @type {CustomEventHandler}
40+
* @type {CustomEventHandler|undefined}
4141
*/
4242
onLoad;
4343

@@ -56,8 +56,8 @@ export class DuckPlayerNativePage {
5656

5757
this.setupLogger();
5858

59-
this.onLoad = onLoad || (() => {});
60-
this.onInit = onInit || (() => {});
59+
this.onLoad = onLoad;
60+
this.onInit = onInit;
6161

6262
this.selectors = selectors;
6363
this.environment = environment;
@@ -80,13 +80,13 @@ export class DuckPlayerNativePage {
8080

8181
init() {
8282
this.logger.log('Running init handlers');
83-
this.onInit(this.sideEffects, this.logger);
83+
this.onInit?.(this.sideEffects, this.logger);
8484

8585
if (document.readyState === 'loading') {
8686
this.sideEffects.add('setting up load event listener', () => {
8787
const loadHandler = () => {
8888
this.logger.log('Running deferred load handlers');
89-
this.onLoad(this.sideEffects, this.logger);
89+
this.onLoad?.(this.sideEffects, this.logger);
9090
this.messages.notifyScriptIsReady();
9191
};
9292
document.addEventListener('DOMContentLoaded', loadHandler, { once: true });
@@ -97,7 +97,7 @@ export class DuckPlayerNativePage {
9797
});
9898
} else {
9999
this.logger.log('Running load handlers immediately');
100-
this.onLoad(this.sideEffects, this.logger);
100+
this.onLoad?.(this.sideEffects, this.logger);
101101
this.messages.notifyScriptIsReady();
102102
}
103103
}
@@ -112,6 +112,9 @@ export class DuckPlayerNativePage {
112112
* @param {DuckPlayerNativeMessages} messages
113113
*/
114114
export function setupDuckPlayerForYouTube(selectors, playbackPaused, environment, messages) {
115+
/**
116+
* @type {(sideEffects: SideEffects, logger: Logger, pause: boolean) => void}
117+
*/
115118
const mediaControlHandler = (sideEffects, logger, pause) => {
116119
logger.log('Running media control handler. Pause:', pause);
117120

@@ -141,6 +144,7 @@ export function setupDuckPlayerForYouTube(selectors, playbackPaused, environment
141144
}
142145
};
143146

147+
/** @type {CustomEventHandler} */
144148
const onLoad = (sideEffects, logger) => {
145149
sideEffects.add('started polling current timestamp', () => {
146150
const handler = (timestamp) => {
@@ -155,6 +159,7 @@ export function setupDuckPlayerForYouTube(selectors, playbackPaused, environment
155159
}
156160
};
157161

162+
/** @type {CustomEventHandler} */
158163
const onInit = (sideEffects, logger) => {
159164
sideEffects.add('subscribe to media control', () => {
160165
return messages.subscribeToMediaControl(({ pause }) => {
@@ -188,6 +193,7 @@ export function setupDuckPlayerForYouTube(selectors, playbackPaused, environment
188193
* @param {DuckPlayerNativeMessages} messages
189194
*/
190195
export function setupDuckPlayerForNoCookie(selectors, environment, messages) {
196+
/** @type {CustomEventHandler} */
191197
const onLoad = (sideEffects, logger) => {
192198
sideEffects.add('started polling current timestamp', () => {
193199
const handler = (timestamp) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Logger } from './util';
1+
import { Logger } from '../duckplayer/util.js';
22

33
/**
44
* @import {DuckPlayerNativeSettings} from "@duckduckgo/privacy-configuration/schema/features/duckplayer-native.js"

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ export function getCurrentTimestamp(selector) {
1313
* Sends the timestamp to the browser at an interval
1414
*
1515
* @param {number} interval - Polling interval
16-
* @param {(number) => void} callback - Callback handler for polling event
16+
* @param {(timestamp: number) => void} callback - Callback handler for polling event
1717
* @param {DuckPlayerNativeSelectors} selectors - Selectors for the player
18-
* @returns
1918
*/
2019
export function pollTimestamp(interval = 300, callback, selectors) {
2120
if (!callback || !selectors) {
2221
console.error('Timestamp polling failed. No callback or selectors defined');
23-
return;
22+
return () => {};
2423
}
2524

2625
const isShowingAd = () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as constants from './constants.js';
22
import { mockTransport } from './mock-transport.js';
33

44
/** @import {YouTubeError} from './error-detection.js' */
5-
/** @import {Environment} from './environment.js' */
5+
/** @import {Environment} from '../duckplayer/environment.js' */
66

77
/**
88
* @typedef {object} MuteSettings - Settings passed to the onMute callback

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as constants from './constants.js';
2-
import { Logger } from './util.js';
2+
import { Logger } from '../duckplayer/util.js';
33

44
const logger = new Logger({
55
id: 'MOCK_TRANSPORT',

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

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

66
/**
77
* The custom element that we use to present our UI elements
@@ -87,7 +87,7 @@ export class DDGVideoThumbnailOverlay extends HTMLElement {
8787
/**
8888
*
8989
* @param {HTMLElement} targetElement
90-
* @param {import('../environment').Environment} environment
90+
* @param {import("../../duckplayer/environment").Environment} environment
9191
* @param {() => void} [onClick] Optional callback to be called when the overlay is clicked
9292
*/
9393
export function appendThumbnailOverlay(targetElement, environment, onClick) {

0 commit comments

Comments
 (0)