Skip to content

Commit

Permalink
Fix Facebook videos if protocol isn't specified in data-href (#1411)
Browse files Browse the repository at this point in the history
Embedded Facebook videos are created by having a div element in the
page that has (amongst other attributes) a data-href attribute that
specifies the video's URL. The Facebook SDK then uses that to create
the video iframe. Our "surrogate script" does the same.

It turns out that when the protocol prefix of that URL isn't specified
(e.g. the URL starts '//'), then the SDK and our surrogate script need
to take care to add the correct protocol to the URL (e.g. 'http:' or
'https:') based on the window location. Otherwise, the video fails to
load.
  • Loading branch information
kzar committed Sep 22, 2022
1 parent 283f57e commit 85f2227
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions shared/js/content-scripts/click-to-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,24 @@
}

for (const key of Object.keys(this.dataElements)) {
const attrValue = encodeURIComponent(this.dataElements[key])
if (attrValue) {
this.clickAction.targetURL = this.clickAction.targetURL.replace(key, attrValue)
let attrValue = this.dataElements[key]

if (!attrValue) {
continue
}

// The URL for Facebook videos are specified as the data-href
// attribute on a div, that is then used to create the iframe.
// Some websites omit the protocol part of the URL when doing
// that, which then prevents the iframe from loading correctly.
if (key === 'data-href' && attrValue.startsWith('//')) {
attrValue = window.location.protocol + attrValue
}

this.clickAction.targetURL =
this.clickAction.targetURL.replace(
key, encodeURIComponent(attrValue)
)
}
}

Expand Down

0 comments on commit 85f2227

Please sign in to comment.