[DOM] Update YouTube embed rewrites.

Youtube's usage patterns have changed regarding video embeds, so we can
remove special handling for the enablejsapi flag.
Applies YouTube embed URL replacement to the path component only.
This commit is contained in:
Moonchild 2025-06-25 22:48:12 +02:00 • committed by roytam1
commit 0c2770ff7b
2 changed files with 15 additions and 17 deletions

View file

@ -1546,26 +1546,22 @@ nsObjectLoadingContent::MaybeRewriteYoutubeEmbed(nsIURI* aURI, nsIURI* aBaseURI,
} }
// See if requester is planning on using the JS API. // See if requester is planning on using the JS API.
nsAutoCString uri; nsAutoCString prePath;
nsresult rv = aURI->GetSpec(uri); nsresult rv = aURI->GetPrePath(prePath);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
return; return;
} }
if (uri.Find("enablejsapi=1", true, 0, -1) != kNotFound) {
return;
}
// Some YouTube urls have parameters in path components, e.g. // Some YouTube urls have parameters in path components, e.g.
// http://youtube.com/embed/7LcUOEP7Brc&start=35. These URLs work with flash, // http://youtube.com/embed/7LcUOEP7Brc&start=35. These URLs work with flash,
// but break iframe/object embedding. If this situation occurs with rewritten // but break iframe/object embedding. If this situation occurs with rewritten
// URLs, convert the parameters to query in order to make the video load // URLs, convert the parameters to query in order to make the video load
// correctly as an iframe. In either case, warn about it in the // correctly as an iframe. In either case, warn about it in the
// developer console. // developer console.
int32_t ampIndex = uri.FindChar('&', 0); int32_t ampIndex = path.FindChar('&', 0);
bool replaceQuery = false; bool replaceQuery = false;
if (ampIndex != -1) { if (ampIndex != -1) {
int32_t qmIndex = uri.FindChar('?', 0); int32_t qmIndex = path.FindChar('?', 0);
if (qmIndex == -1 || if (qmIndex == -1 ||
qmIndex > ampIndex) { qmIndex > ampIndex) {
replaceQuery = true; replaceQuery = true;
@ -1576,20 +1572,22 @@ nsObjectLoadingContent::MaybeRewriteYoutubeEmbed(nsIURI* aURI, nsIURI* aBaseURI,
return; return;
} }
nsAutoString utf16OldURI = NS_ConvertUTF8toUTF16(uri); NS_ConvertUTF8toUTF16 utf16OldURI(prePath);
AppendUTF8toUTF16(path, utf16OldURI);
// If we need to convert the URL, it means an ampersand comes first. // If we need to convert the URL, it means an ampersand comes first.
// Use the index we found earlier. // Use the index we found earlier.
if (replaceQuery) { if (replaceQuery) {
// Replace question marks with ampersands. // Replace question marks with ampersands.
uri.ReplaceChar('?', '&'); path.ReplaceChar('?', '&');
// Replace the first ampersand with a question mark. // Replace the first ampersand with a question mark.
uri.SetCharAt('?', ampIndex); path.SetCharAt('?', ampIndex);
} }
// Switch out video access url formats, which should possibly allow HTML5 // Switch out video access url formats, which should possibly allow HTML5
// video loading. // video loading.
uri.ReplaceSubstring(NS_LITERAL_CSTRING("/v/"), path.ReplaceSubstring(NS_LITERAL_CSTRING("/v/"),
NS_LITERAL_CSTRING("/embed/")); NS_LITERAL_CSTRING("/embed/"));
nsAutoString utf16URI = NS_ConvertUTF8toUTF16(uri); NS_ConvertUTF8toUTF16 utf16URI(prePath);
AppendUTF8toUTF16(path, utf16URI);
rv = nsContentUtils::NewURIWithDocumentCharset(aOutURI, rv = nsContentUtils::NewURIWithDocumentCharset(aOutURI,
utf16URI, utf16URI,
thisContent->OwnerDoc(), thisContent->OwnerDoc(),

View file

@ -572,12 +572,12 @@ class nsObjectLoadingContent : public nsImageLoadingContent
* *
* - is an embed or object node * - is an embed or object node
* - has a URL pointing at the youtube.com domain, using "/v/" style video * - has a URL pointing at the youtube.com domain, using "/v/" style video
* path reference, and without enablejsapi=1 in the path * path reference.
* *
* Having the enablejsapi flag means the document that contains the element * Having the enablejsapi flag means the document that contains the element
* could possibly be manipulating the youtube video elsewhere on the page * could possibly be manipulating the youtube video elsewhere on the page
* via javascript. We can't rewrite these kinds of elements without possibly * via javascript. In the context of embed elements, this usage has been
* breaking content, which we want to avoid. * deprecated by youtube, so we can just rewrite as normal.
* *
* If we can rewrite the URL, we change the "/v/" to "/embed/", and change * If we can rewrite the URL, we change the "/v/" to "/embed/", and change
* our type to eType_Document so that we render similarly to an iframe * our type to eType_Document so that we render similarly to an iframe