Skip to content

Commit 3f2e7d6

Browse files
authored
Revert "Display Click to Load placeholders on demand (#287)" (#294)
This reverts commit 46ca757. The change was causing an integration test failure, which we ran out of time to debug. We will investigate and reapply this shortly.
1 parent 46ca757 commit 3f2e7d6

File tree

3 files changed

+49
-78
lines changed

3 files changed

+49
-78
lines changed

inject/chrome.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,16 @@ function init () {
126126
}
127127
})
128128

129-
window.addEventListener('sendMessageProxy' + messageSecret, event => {
130-
event.stopImmediatePropagation()
131-
132-
if (!(event instanceof CustomEvent) || !event?.detail) {
133-
return console.warn('no details in sendMessage proxy', event)
134-
}
135-
136-
const messageType = event.detail?.messageType
129+
window.addEventListener('sendMessageProxy' + messageSecret, (m) => {
130+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
131+
const messageType = m.detail.messageType
137132
if (!allowedMessages.includes(messageType)) {
138133
return console.warn('Ignoring invalid sendMessage messageType', messageType)
139134
}
140-
141-
chrome.runtime.sendMessage(event.detail, response => {
142-
const message = {
143-
messageType: 'response',
144-
responseMessageType: messageType,
145-
response
146-
}
147-
const stringifiedArgs = JSON.stringify(message)
135+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
136+
chrome.runtime.sendMessage(m && m.detail, response => {
137+
const msg = { func: messageType, response }
138+
const stringifiedArgs = JSON.stringify({ detail: msg })
148139
const callRandomUpdateFunction = `
149140
window.${reusableMethodName}('${reusableSecret}', ${stringifiedArgs});
150141
`

inject/mozilla.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,16 @@ function init () {
5959
}
6060
})
6161

62-
window.addEventListener('sendMessageProxy' + messageSecret, event => {
63-
event.stopImmediatePropagation()
64-
65-
if (!(event instanceof CustomEvent) || !event?.detail) {
66-
return console.warn('no details in sendMessage proxy', event)
67-
}
68-
69-
const messageType = event.detail?.messageType
62+
window.addEventListener('sendMessageProxy' + messageSecret, (m) => {
63+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
64+
const messageType = m.detail.messageType
7065
if (!allowedMessages.includes(messageType)) {
7166
return console.warn('Ignoring invalid sendMessage messageType', messageType)
7267
}
73-
74-
chrome.runtime.sendMessage(event.detail, response => {
75-
const message = {
76-
messageType: 'response',
77-
responseMessageType: messageType,
78-
response
79-
}
80-
81-
contentScopeFeatures.update(message)
68+
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
69+
chrome.runtime.sendMessage(m && m.detail, response => {
70+
const msg = { func: messageType, response }
71+
contentScopeFeatures.update({ detail: msg })
8272
})
8373
})
8474
}

src/features/click-to-play.js

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ let sharedStrings = null
2020
const entities = []
2121
const entityData = {}
2222

23-
let readyResolver
24-
const ready = new Promise(resolve => { readyResolver = resolve })
25-
2623
/*********************************************************
2724
* Widget Replacement logic
2825
*********************************************************/
@@ -367,6 +364,22 @@ class DuckWidget {
367364
}
368365
}
369366

367+
/**
368+
* Initialise the Click to Load feature, once the necessary details have been
369+
* returned by the platform.
370+
* @returns {Promise}
371+
*/
372+
async function initCTL () {
373+
await replaceClickToLoadElements()
374+
375+
window.addEventListener('ddg-ctp-replace-element', ({ target }) => {
376+
replaceClickToLoadElements(target)
377+
}, { capture: true })
378+
379+
// Inform surrogate scripts that CTP is ready
380+
originalWindowDispatchEvent(createCustomEvent('ddg-ctp-ready'))
381+
}
382+
370383
function replaceTrackingElement (widget, trackingElement, placeholderElement, hideTrackingElement = false, currentPlaceholder = null) {
371384
widget.dispatchEvent(trackingElement, 'ddg-ctp-tracking-element')
372385

@@ -479,14 +492,11 @@ async function replaceYouTubeCTL (trackingElement, widget, togglePlaceholder = f
479492
}
480493

481494
// Show YouTube Preview for embedded video
482-
// TODO: Fix the hideTrackingElement option and reenable, or remove it. It's
483-
// disabled for YouTube videos so far since it caused multiple
484-
// placeholders to be displayed on the page.
485495
if (isYoutubePreviewsEnabled === true) {
486496
const { youTubePreview, shadowRoot } = await createYouTubePreview(trackingElement, widget)
487497
const currentPlaceholder = togglePlaceholder ? document.getElementById(`yt-ctl-dialog-${widget.widgetID}`) : null
488498
replaceTrackingElement(
489-
widget, trackingElement, youTubePreview, /* hideTrackingElement= */ false, currentPlaceholder
499+
widget, trackingElement, youTubePreview, /* hideTrackingElement= */ true, currentPlaceholder
490500
)
491501
showExtraUnblockIfShortPlaceholder(shadowRoot, youTubePreview)
492502

@@ -496,7 +506,7 @@ async function replaceYouTubeCTL (trackingElement, widget, togglePlaceholder = f
496506
const { blockingDialog, shadowRoot } = await createYouTubeBlockingDialog(trackingElement, widget)
497507
const currentPlaceholder = togglePlaceholder ? document.getElementById(`yt-ctl-preview-${widget.widgetID}`) : null
498508
replaceTrackingElement(
499-
widget, trackingElement, blockingDialog, /* hideTrackingElement= */ false, currentPlaceholder
509+
widget, trackingElement, blockingDialog, /* hideTrackingElement= */ true, currentPlaceholder
500510
)
501511
showExtraUnblockIfShortPlaceholder(shadowRoot, blockingDialog)
502512
}
@@ -527,8 +537,6 @@ function showExtraUnblockIfShortPlaceholder (shadowRoot, placeholder) {
527537
* in the document will be replaced instead.
528538
*/
529539
async function replaceClickToLoadElements (targetElement) {
530-
await ready
531-
532540
for (const entity of Object.keys(config)) {
533541
for (const widgetData of Object.values(config[entity].elementData)) {
534542
const selector = widgetData.selectors.join()
@@ -1304,7 +1312,7 @@ async function createYouTubePreview (originalElement, widget) {
13041312
// Convention is that each function should be named the same as the sendMessage
13051313
// method we are calling into eg. calling `sendMessage('getClickToLoadState')`
13061314
// will result in a response routed to `updateHandlers.getClickToLoadState()`.
1307-
const messageResponseHandlers = {
1315+
const updateHandlers = {
13081316
getClickToLoadState (response) {
13091317
devMode = response.devMode
13101318
isYoutubePreviewsEnabled = response.youtubePreviewsEnabled
@@ -1314,15 +1322,14 @@ const messageResponseHandlers = {
13141322
// first.
13151323

13161324
// Start Click to Load
1317-
window.addEventListener('ddg-ctp-replace-element', ({ target }) => {
1318-
replaceClickToLoadElements(target)
1319-
}, { capture: true })
1320-
1321-
// Inform surrogate scripts that CTP is ready
1322-
originalWindowDispatchEvent(createCustomEvent('ddg-ctp-ready'))
1323-
1324-
// Mark the feature as ready, to allow placeholder replacements.
1325-
readyResolver()
1325+
if (document.readyState === 'complete') {
1326+
initCTL()
1327+
} else {
1328+
// Content script loaded before page content, so wait for load.
1329+
window.addEventListener('load', (event) => {
1330+
initCTL()
1331+
})
1332+
}
13261333
},
13271334
setYoutubePreviewsEnabled: function (resp) {
13281335
if (resp?.messageType && typeof resp?.value === 'boolean') {
@@ -1339,8 +1346,6 @@ const messageResponseHandlers = {
13391346
}
13401347
}
13411348

1342-
const knownMessageResponseType = Object.prototype.hasOwnProperty.bind(messageResponseHandlers)
1343-
13441349
export function init (args) {
13451350
const websiteOwner = args?.site?.parentEntity
13461351
const settings = args?.featureSettings?.clickToPlay || {}
@@ -1406,32 +1411,17 @@ export function init (args) {
14061411
})
14071412

14081413
// Request the current state of Click to Load from the platform.
1409-
// Note: When the response is received, the response handler finishes
1410-
// starting up the feature.
1414+
// Note: When the response is received, initCTL() is then called by the
1415+
// response handler to finish starting up the feature.
14111416
sendMessage('getClickToLoadState')
14121417
}
14131418

1414-
export function update (message) {
1415-
// TODO: Once all Click to Load messages include the feature property, drop
1416-
// messages that don't include the feature property too.
1417-
if (message?.feature && message?.feature !== 'clickToLoad') return
1419+
export function update (args) {
1420+
const detail = args && args.detail
1421+
if (!(detail && detail.func)) { return }
14181422

1419-
const messageType = message?.messageType
1420-
if (!messageType) return
1423+
const fn = updateHandlers[detail.func]
1424+
if (typeof fn !== 'function') { return }
14211425

1422-
// Message responses.
1423-
if (messageType === 'response') {
1424-
const messageResponseType = message?.responseMessageType
1425-
if (messageResponseType && knownMessageResponseType(messageResponseType)) {
1426-
return messageResponseHandlers[messageResponseType](message.response)
1427-
}
1428-
}
1429-
1430-
// Other known update messages.
1431-
if (messageType === 'displayClickToLoadPlaceholders') {
1432-
// TODO: Pass `message.options.ruleAction` through, that way only
1433-
// content corresponding to the entity for that ruleAction need to
1434-
// be replaced with a placeholder.
1435-
return replaceClickToLoadElements()
1436-
}
1426+
fn(detail.response)
14371427
}

0 commit comments

Comments
 (0)