Created
January 23, 2023 15:57
-
-
Save Cauen/fe7000e00ddac25700a1172f7d1bb39b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Name: Deep URL decode | |
// Description: Sometimes you want to know what inside a URL, use this to show as a deep json... | |
// Example url: https://google.com/notify/messages/1295812905/click?signature=fake_signature_id&url=https%3A%2F%2Fgoogle.com%2Fusers%2Flink%2FZW1hbnVlbC5-9AF80a98fg8vbQ%3D%3DMTY3NDQzMjg2NA%3D%3D6a8f74a552%3Fredirect_to%3Dhttps%253A%252F%252Fgoogle.com%252Fteam%252F832678%252Fincidents%252F331734795%26utm_source%3Descalation_mailer%26utm_medium%3Demail%26utm_campaign%3Dresolved | |
import "@johnlindquist/kit" | |
const decode = (str: string) => decodeURIComponent((str + '').replace(/\+/g, '%20')) | |
function getParamsFromUrl(url: string): Record<string, string> | void { | |
if (url.startsWith("https%")) return getParamsFromUrl(decode(url)) | |
try { | |
if (typeof url === 'string') { | |
let params = url.split('?'); | |
let eachParamsArr = params[1].split('&'); | |
let obj: Record<string, string> = {}; | |
if (eachParamsArr && eachParamsArr.length) { | |
eachParamsArr.map(param => { | |
let keyValuePair = param.split('=') | |
let key = keyValuePair[0]; | |
let value = keyValuePair[1]; | |
obj[key] = value; | |
}) | |
} | |
return obj; | |
} | |
} catch (err) { | |
// return getParamsFromUrl(decode(url)) | |
return {} | |
} | |
} | |
interface DecodeType { | |
['1: URL Original']: string | |
['2: URL Decoded']: string | |
['3: URL Params']: void | Record<string, string> | |
['4: Children']: Record<string, DecodeType> | |
} | |
function decodeDeep(urlString: string): DecodeType { | |
const decodedUrl = decode(urlString) | |
const paramsFromUrl = getParamsFromUrl(urlString) | |
return { | |
['1: URL Original']: urlString, | |
['2: URL Decoded']: decodedUrl, | |
['3: URL Params']: paramsFromUrl, | |
['4: Children']: (() => { | |
if (!paramsFromUrl) return {} | |
const entries = Object.entries(paramsFromUrl) | |
return entries.reduce((current, [paramKey, paramValue]) => { | |
const itemIsUrl = paramValue.startsWith("http") | |
if (!itemIsUrl) return current | |
const decoded = decodeDeep(paramValue) | |
if (!decoded) return current | |
// return current | |
return { ...current, [paramKey]: decoded } | |
}, {}) | |
})() | |
} | |
} | |
let url = await arg("enter URL") | |
inspect(decodeDeep(url)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment