Fix local run parsing to ensure URL not manipulated unnecessarily breaking the URL (#3467)

* ! Fix local run parsing to ensure URL not manipulated unnecesarily breaking the URL

* Rename new variable
This commit is contained in:
PikachuEXE 2023-05-03 12:05:30 +08:00 committed by GitHub
parent f0748ea1eb
commit 8765d2ae6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,14 @@ import {
toLocalePublicationString
} from '../utils'
const TRACKING_PARAM_NAMES = [
'utm_source',
'utm_medium',
'utm_campaign',
'utm_term',
'utm_content',
]
/**
* Creates a lightweight Innertube instance, which is faster to create or
* an instance that can decode the streaming URLs, which is slower to create
@ -509,15 +517,20 @@ export function parseLocalTextRuns(runs, emojiSize = 16, options = { looseChanne
const url = new URL((endpoint.dialog?.type === 'ConfirmDialog' && endpoint.dialog.confirm_button.endpoint.payload.url) || endpoint.payload.url)
if (url.hostname === 'www.youtube.com' && url.pathname === '/redirect' && url.searchParams.has('q')) {
// remove utm tracking parameters
const realURL = new URL(url.searchParams.get('q'))
const realURLStr = url.searchParams.get('q')
const realURL = new URL(realURLStr)
let urlChanged = false
realURL.searchParams.delete('utm_source')
realURL.searchParams.delete('utm_medium')
realURL.searchParams.delete('utm_campaign')
realURL.searchParams.delete('utm_term')
realURL.searchParams.delete('utm_content')
TRACKING_PARAM_NAMES.forEach((paramName) => {
if (!realURL.searchParams.has(paramName)) { return }
parsedRuns.push(realURL.toString())
realURL.searchParams.delete(paramName)
urlChanged = true
})
// `searchParams.delete` changes query string unnecessarily
// Using original unless there is any change
parsedRuns.push(urlChanged ? realURL.toString() : realURLStr)
} else {
// this is probably a special YouTube URL like http://www.youtube.com/approachingnirvana
parsedRuns.push(endpoint.payload.url)