From 8765d2ae6d6a527ea718ac4218d0c3f4d3ff4e6d Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Wed, 3 May 2023 12:05:30 +0800 Subject: [PATCH] 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 --- src/renderer/helpers/api/local.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/renderer/helpers/api/local.js b/src/renderer/helpers/api/local.js index 3766447d4..b58d96e7b 100644 --- a/src/renderer/helpers/api/local.js +++ b/src/renderer/helpers/api/local.js @@ -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)