Last active
January 28, 2023 04:29
-
-
Save ryanbaer/ca7bcf557d3c81a4d13d1c15060f49ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# 🗡️ Strip Query Params | |
- ⌨️ Prompts the user for a URL | |
- 🗡️ Cuts off all query params | |
- 🛡️ Maintains essential query params (e.g., `youtube.com/watch?v=[videoId]`) | |
- 📋 Automatically copies the updated URL to the clipboard. | |
Useful for removing tracking / other information from URLs before sharing with others. | |
## Examples | |
|Site|Input|Output| | |
|-|-|-| | |
|Facebook Video|https://www.facebook.com/watch/?v=1233056997105202&extid=NS-UNK-CHE-UNK-IOS_WXYZ-ABCD&mibextid=cfeeni&ref=sharing|https://www.facebook.com/watch/?v=1233056997105202| | |
|Facebook|https://www.facebook.com/groups/funnycatsworld/permalink/5619404908185458/?mibextid=cfeeni|https://www.facebook.com/groups/funnycatsworld/permalink/5619404908185458/| | |
|Instagram|https://www.instagram.com/reel/CnxZnkRprAD/?igshid=YmNmMTdmZDM=|https://www.instagram.com/reel/CnxZnkRprAD/| | |
|YouTube|https://www.youtube.com/watch?v=MWRPYBoCEaY&ab_channel=NoBoilerplate|https://www.youtube.com/watch?v=MWRPYBoCEaY| | |
*/ | |
// Name: 🗡️ Strip Query Params | |
// Description: Strips query params from a URL. | |
// Author: Ryan Baer | |
import "@johnlindquist/kit"; | |
// TODO: let's leverage the awesome `db` feature and allow users to add their own | |
// rules. | |
const requiredParams: Record<string, string[]> = { | |
// youtube.com/watch?v=[videoId] requires the 'v' param to locate the video | |
"youtube.com": ["v"], | |
// facebook.com/watch/?v=[videoId] requires the 'v' param to locate the video | |
"facebook.com": ["v"], | |
}; | |
function stripQueryParams(input: string) { | |
const url = new URL(input); | |
function getBaseDomain(hostname: string) { | |
const [tld, base] = hostname.split(".").reverse(); | |
return `${base}.${tld}`; | |
} | |
// Clone so we're not deleting from the list as we're iterating it. | |
const searchParams = new URLSearchParams(url.searchParams); | |
searchParams.forEach((_value, key) => { | |
const baseDomain = getBaseDomain(url.hostname); | |
const skip = requiredParams[baseDomain]; | |
if (skip?.includes(key)) { | |
return; | |
} | |
url.searchParams.delete(key); | |
}); | |
return url.href; | |
} | |
const input = await arg("Enter a URL"); | |
const result = stripQueryParams(input); | |
await copy(result); | |
await div( | |
md(`# 🔬 Result (automatically copied to clipboard) | |
\`\`\` | |
${result} | |
\`\`\` | |
`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment