Skip to content

Commit 5434ffe

Browse files
Release build 4.3.2 [ci release]
1 parent 60b9a4c commit 5434ffe

File tree

10 files changed

+382
-274
lines changed

10 files changed

+382
-274
lines changed

Sources/ContentScopeScripts/dist/contentScope.js

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,9 @@
29932993
const entities = [];
29942994
const entityData = {};
29952995

2996+
let readyResolver;
2997+
const ready = new Promise(resolve => { readyResolver = resolve; });
2998+
29962999
/*********************************************************
29973000
* Widget Replacement logic
29983001
*********************************************************/
@@ -3337,22 +3340,6 @@
33373340
}
33383341
}
33393342

3340-
/**
3341-
* Initialise the Click to Load feature, once the necessary details have been
3342-
* returned by the platform.
3343-
* @returns {Promise}
3344-
*/
3345-
async function initCTL () {
3346-
await replaceClickToLoadElements();
3347-
3348-
window.addEventListener('ddg-ctp-replace-element', ({ target }) => {
3349-
replaceClickToLoadElements(target);
3350-
}, { capture: true });
3351-
3352-
// Inform surrogate scripts that CTP is ready
3353-
originalWindowDispatchEvent(createCustomEvent('ddg-ctp-ready'));
3354-
}
3355-
33563343
function replaceTrackingElement (widget, trackingElement, placeholderElement, hideTrackingElement = false, currentPlaceholder = null) {
33573344
widget.dispatchEvent(trackingElement, 'ddg-ctp-tracking-element');
33583345

@@ -3465,11 +3452,14 @@
34653452
}
34663453

34673454
// Show YouTube Preview for embedded video
3455+
// TODO: Fix the hideTrackingElement option and reenable, or remove it. It's
3456+
// disabled for YouTube videos so far since it caused multiple
3457+
// placeholders to be displayed on the page.
34683458
if (isYoutubePreviewsEnabled === true) {
34693459
const { youTubePreview, shadowRoot } = await createYouTubePreview(trackingElement, widget);
34703460
const currentPlaceholder = togglePlaceholder ? document.getElementById(`yt-ctl-dialog-${widget.widgetID}`) : null;
34713461
replaceTrackingElement(
3472-
widget, trackingElement, youTubePreview, /* hideTrackingElement= */ true, currentPlaceholder
3462+
widget, trackingElement, youTubePreview, /* hideTrackingElement= */ false, currentPlaceholder
34733463
);
34743464
showExtraUnblockIfShortPlaceholder(shadowRoot, youTubePreview);
34753465

@@ -3479,7 +3469,7 @@
34793469
const { blockingDialog, shadowRoot } = await createYouTubeBlockingDialog(trackingElement, widget);
34803470
const currentPlaceholder = togglePlaceholder ? document.getElementById(`yt-ctl-preview-${widget.widgetID}`) : null;
34813471
replaceTrackingElement(
3482-
widget, trackingElement, blockingDialog, /* hideTrackingElement= */ true, currentPlaceholder
3472+
widget, trackingElement, blockingDialog, /* hideTrackingElement= */ false, currentPlaceholder
34833473
);
34843474
showExtraUnblockIfShortPlaceholder(shadowRoot, blockingDialog);
34853475
}
@@ -3510,6 +3500,8 @@
35103500
* in the document will be replaced instead.
35113501
*/
35123502
async function replaceClickToLoadElements (targetElement) {
3503+
await ready;
3504+
35133505
for (const entity of Object.keys(config)) {
35143506
for (const widgetData of Object.values(config[entity].elementData)) {
35153507
const selector = widgetData.selectors.join();
@@ -4285,7 +4277,7 @@
42854277
// Convention is that each function should be named the same as the sendMessage
42864278
// method we are calling into eg. calling `sendMessage('getClickToLoadState')`
42874279
// will result in a response routed to `updateHandlers.getClickToLoadState()`.
4288-
const updateHandlers = {
4280+
const messageResponseHandlers = {
42894281
getClickToLoadState (response) {
42904282
devMode = response.devMode;
42914283
isYoutubePreviewsEnabled = response.youtubePreviewsEnabled;
@@ -4295,14 +4287,15 @@
42954287
// first.
42964288

42974289
// Start Click to Load
4298-
if (document.readyState === 'complete') {
4299-
initCTL();
4300-
} else {
4301-
// Content script loaded before page content, so wait for load.
4302-
window.addEventListener('load', (event) => {
4303-
initCTL();
4304-
});
4305-
}
4290+
window.addEventListener('ddg-ctp-replace-element', ({ target }) => {
4291+
replaceClickToLoadElements(target);
4292+
}, { capture: true });
4293+
4294+
// Inform surrogate scripts that CTP is ready
4295+
originalWindowDispatchEvent(createCustomEvent('ddg-ctp-ready'));
4296+
4297+
// Mark the feature as ready, to allow placeholder replacements.
4298+
readyResolver();
43064299
},
43074300
setYoutubePreviewsEnabled: function (resp) {
43084301
if (resp?.messageType && typeof resp?.value === 'boolean') {
@@ -4319,6 +4312,8 @@
43194312
}
43204313
};
43214314

4315+
const knownMessageResponseType = Object.prototype.hasOwnProperty.bind(messageResponseHandlers);
4316+
43224317
function init$f (args) {
43234318
const websiteOwner = args?.site?.parentEntity;
43244319
const settings = args?.featureSettings?.clickToPlay || {};
@@ -4384,19 +4379,34 @@
43844379
});
43854380

43864381
// Request the current state of Click to Load from the platform.
4387-
// Note: When the response is received, initCTL() is then called by the
4388-
// response handler to finish starting up the feature.
4382+
// Note: When the response is received, the response handler finishes
4383+
// starting up the feature.
43894384
sendMessage('getClickToLoadState');
43904385
}
43914386

4392-
function update$1 (args) {
4393-
const detail = args && args.detail;
4394-
if (!(detail && detail.func)) { return }
4387+
function update$1 (message) {
4388+
// TODO: Once all Click to Load messages include the feature property, drop
4389+
// messages that don't include the feature property too.
4390+
if (message?.feature && message?.feature !== 'clickToLoad') return
43954391

4396-
const fn = updateHandlers[detail.func];
4397-
if (typeof fn !== 'function') { return }
4392+
const messageType = message?.messageType;
4393+
if (!messageType) return
43984394

4399-
fn(detail.response);
4395+
// Message responses.
4396+
if (messageType === 'response') {
4397+
const messageResponseType = message?.responseMessageType;
4398+
if (messageResponseType && knownMessageResponseType(messageResponseType)) {
4399+
return messageResponseHandlers[messageResponseType](message.response)
4400+
}
4401+
}
4402+
4403+
// Other known update messages.
4404+
if (messageType === 'displayClickToLoadPlaceholders') {
4405+
// TODO: Pass `message.options.ruleAction` through, that way only
4406+
// content corresponding to the entity for that ruleAction need to
4407+
// be replaced with a placeholder.
4408+
return replaceClickToLoadElements()
4409+
}
44004410
}
44014411

44024412
var clickToPlay = /*#__PURE__*/Object.freeze({

build/android/contentScope.js

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,9 @@
29932993
const entities = [];
29942994
const entityData = {};
29952995

2996+
let readyResolver;
2997+
const ready = new Promise(resolve => { readyResolver = resolve; });
2998+
29962999
/*********************************************************
29973000
* Widget Replacement logic
29983001
*********************************************************/
@@ -3337,22 +3340,6 @@
33373340
}
33383341
}
33393342

3340-
/**
3341-
* Initialise the Click to Load feature, once the necessary details have been
3342-
* returned by the platform.
3343-
* @returns {Promise}
3344-
*/
3345-
async function initCTL () {
3346-
await replaceClickToLoadElements();
3347-
3348-
window.addEventListener('ddg-ctp-replace-element', ({ target }) => {
3349-
replaceClickToLoadElements(target);
3350-
}, { capture: true });
3351-
3352-
// Inform surrogate scripts that CTP is ready
3353-
originalWindowDispatchEvent(createCustomEvent('ddg-ctp-ready'));
3354-
}
3355-
33563343
function replaceTrackingElement (widget, trackingElement, placeholderElement, hideTrackingElement = false, currentPlaceholder = null) {
33573344
widget.dispatchEvent(trackingElement, 'ddg-ctp-tracking-element');
33583345

@@ -3465,11 +3452,14 @@
34653452
}
34663453

34673454
// Show YouTube Preview for embedded video
3455+
// TODO: Fix the hideTrackingElement option and reenable, or remove it. It's
3456+
// disabled for YouTube videos so far since it caused multiple
3457+
// placeholders to be displayed on the page.
34683458
if (isYoutubePreviewsEnabled === true) {
34693459
const { youTubePreview, shadowRoot } = await createYouTubePreview(trackingElement, widget);
34703460
const currentPlaceholder = togglePlaceholder ? document.getElementById(`yt-ctl-dialog-${widget.widgetID}`) : null;
34713461
replaceTrackingElement(
3472-
widget, trackingElement, youTubePreview, /* hideTrackingElement= */ true, currentPlaceholder
3462+
widget, trackingElement, youTubePreview, /* hideTrackingElement= */ false, currentPlaceholder
34733463
);
34743464
showExtraUnblockIfShortPlaceholder(shadowRoot, youTubePreview);
34753465

@@ -3479,7 +3469,7 @@
34793469
const { blockingDialog, shadowRoot } = await createYouTubeBlockingDialog(trackingElement, widget);
34803470
const currentPlaceholder = togglePlaceholder ? document.getElementById(`yt-ctl-preview-${widget.widgetID}`) : null;
34813471
replaceTrackingElement(
3482-
widget, trackingElement, blockingDialog, /* hideTrackingElement= */ true, currentPlaceholder
3472+
widget, trackingElement, blockingDialog, /* hideTrackingElement= */ false, currentPlaceholder
34833473
);
34843474
showExtraUnblockIfShortPlaceholder(shadowRoot, blockingDialog);
34853475
}
@@ -3510,6 +3500,8 @@
35103500
* in the document will be replaced instead.
35113501
*/
35123502
async function replaceClickToLoadElements (targetElement) {
3503+
await ready;
3504+
35133505
for (const entity of Object.keys(config)) {
35143506
for (const widgetData of Object.values(config[entity].elementData)) {
35153507
const selector = widgetData.selectors.join();
@@ -4285,7 +4277,7 @@
42854277
// Convention is that each function should be named the same as the sendMessage
42864278
// method we are calling into eg. calling `sendMessage('getClickToLoadState')`
42874279
// will result in a response routed to `updateHandlers.getClickToLoadState()`.
4288-
const updateHandlers = {
4280+
const messageResponseHandlers = {
42894281
getClickToLoadState (response) {
42904282
devMode = response.devMode;
42914283
isYoutubePreviewsEnabled = response.youtubePreviewsEnabled;
@@ -4295,14 +4287,15 @@
42954287
// first.
42964288

42974289
// Start Click to Load
4298-
if (document.readyState === 'complete') {
4299-
initCTL();
4300-
} else {
4301-
// Content script loaded before page content, so wait for load.
4302-
window.addEventListener('load', (event) => {
4303-
initCTL();
4304-
});
4305-
}
4290+
window.addEventListener('ddg-ctp-replace-element', ({ target }) => {
4291+
replaceClickToLoadElements(target);
4292+
}, { capture: true });
4293+
4294+
// Inform surrogate scripts that CTP is ready
4295+
originalWindowDispatchEvent(createCustomEvent('ddg-ctp-ready'));
4296+
4297+
// Mark the feature as ready, to allow placeholder replacements.
4298+
readyResolver();
43064299
},
43074300
setYoutubePreviewsEnabled: function (resp) {
43084301
if (resp?.messageType && typeof resp?.value === 'boolean') {
@@ -4319,6 +4312,8 @@
43194312
}
43204313
};
43214314

4315+
const knownMessageResponseType = Object.prototype.hasOwnProperty.bind(messageResponseHandlers);
4316+
43224317
function init$f (args) {
43234318
const websiteOwner = args?.site?.parentEntity;
43244319
const settings = args?.featureSettings?.clickToPlay || {};
@@ -4384,19 +4379,34 @@
43844379
});
43854380

43864381
// Request the current state of Click to Load from the platform.
4387-
// Note: When the response is received, initCTL() is then called by the
4388-
// response handler to finish starting up the feature.
4382+
// Note: When the response is received, the response handler finishes
4383+
// starting up the feature.
43894384
sendMessage('getClickToLoadState');
43904385
}
43914386

4392-
function update$1 (args) {
4393-
const detail = args && args.detail;
4394-
if (!(detail && detail.func)) { return }
4387+
function update$1 (message) {
4388+
// TODO: Once all Click to Load messages include the feature property, drop
4389+
// messages that don't include the feature property too.
4390+
if (message?.feature && message?.feature !== 'clickToLoad') return
43954391

4396-
const fn = updateHandlers[detail.func];
4397-
if (typeof fn !== 'function') { return }
4392+
const messageType = message?.messageType;
4393+
if (!messageType) return
43984394

4399-
fn(detail.response);
4395+
// Message responses.
4396+
if (messageType === 'response') {
4397+
const messageResponseType = message?.responseMessageType;
4398+
if (messageResponseType && knownMessageResponseType(messageResponseType)) {
4399+
return messageResponseHandlers[messageResponseType](message.response)
4400+
}
4401+
}
4402+
4403+
// Other known update messages.
4404+
if (messageType === 'displayClickToLoadPlaceholders') {
4405+
// TODO: Pass `message.options.ruleAction` through, that way only
4406+
// content corresponding to the entity for that ruleAction need to
4407+
// be replaced with a placeholder.
4408+
return replaceClickToLoadElements()
4409+
}
44004410
}
44014411

44024412
var clickToPlay = /*#__PURE__*/Object.freeze({

0 commit comments

Comments
 (0)