Ryan

Ryan

// Name: ๐Ÿง™โ€โ™‚๏ธ Resolve Short Links
// Description: Resolve short links & removes any tracking info
// Author: Ryan Baer
import "@johnlindquist/kit";
const Resolver = {
requiredParams: {
// 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"],
},
getURL(input: string) {
let processed = input;
if (!input.match(/^https?:\/\//)) {
processed = `https://${processed}`;
}
return new URL(processed);
},
sanitizeUrl(url: URL) {
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 = this.requiredParams[baseDomain];
if (skip?.includes(key)) {
return;
}
url.searchParams.delete(key);
});
return url.href;
},
async fetchHead(input: string) {
let url: URL;
try {
url = this.getURL(input);
} catch (error) {
return { resolvedUrl: undefined, error: "Invaid or unreachable URL" };
}
const options = { method: "HEAD", credentials: "omit" } as const;
try {
const response = await fetch(url, options);
const responseUrl = new URL(response.url);
const sanitizedUrl = this.sanitizeUrl(responseUrl);
return { resolvedUrl: sanitizedUrl, error: undefined };
} catch (error) {
return { resolvedUrl: undefined, error: this.getErrorMessage(error) };
}
},
getErrorMessage(error: unknown) {
return error instanceof Error
? error.message
: "Something unexpected happened.";
},
async resolve(url: string) {
return this.fetchHead(url);
},
};
const url = await arg("Enter a shortened URL");
const response = await Resolver.resolve(url);
const { resolvedUrl, error } = response;
const result = error ?? resolvedUrl ?? "(Something went wrong)";
const shouldCopy = !!resolvedUrl;
if (shouldCopy) {
await copy(result);
}
await div(
md(`# ๐Ÿ”ฌ Result${shouldCopy ? " (automatically copied to clipboard)" : ""}
\`\`\`
${result}
\`\`\`
`)
);

// 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}
\`\`\`
`)
);