Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make YoutubeLinkEnhancement URL parsing more robust. #4003

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
link: $link
}

if (href.indexOf('/live_stream') >= 0) {
if (href.includes('/live_stream')) {
var channelId = YoutubeLinkEnhancement.parseLivestream(href)

if (!this.hasDisabledEmbed($link) && channelId) {
Expand Down Expand Up @@ -175,23 +175,16 @@
// This is a public class method so it can be used outside of this embed to
// check that user input for videos will be supported in govspeak
YoutubeLinkEnhancement.parseVideoId = function (url) {
var parts
var u

if (url.indexOf('youtube.com') > -1) {
var params = {}
parts = url.split('?')
if (parts.length === 1) {
return
}
parts = parts[1].split('&')
for (var i = 0; i < parts.length; i++) {
var part = parts[i].split('=')
params[part[0]] = part[1]
}
return params.v
} else if (url.indexOf('youtu.be') > -1) {
parts = url.split('/')
return parts.pop()
try {
u = new URL(url)
} catch (e) { return undefined }

if (u.host === 'www.youtube.com' || u.host === 'youtube.com') {
return u.searchParams.get('v') || undefined
} else if (u.host === 'youtu.be') {
return u.pathname.slice(1) // Trim the leading /
}
}

Expand Down
Loading