urldecode: cleanup, error handling
This commit is contained in:
parent
77283cc9d3
commit
6445664512
1 changed files with 20 additions and 12 deletions
|
@ -1,29 +1,37 @@
|
||||||
const Command = require("#lib/command.js");
|
const Command = require("#lib/command.js");
|
||||||
|
const {Icons} = require("#util/constants.js");
|
||||||
|
|
||||||
const urldecode = new Command("urldecode");
|
const urldecode = new Command("urldecode");
|
||||||
urldecode.category = "misc";
|
urldecode.category = "misc";
|
||||||
urldecode.helpText = "Get url information";
|
urldecode.helpText = "Get information on a URL";
|
||||||
urldecode.usage = "<url>";
|
urldecode.usage = "<url>";
|
||||||
urldecode.callback = async function (msg, _, [urlString]) {
|
urldecode.callback = async function (msg, line, [urlString]) {
|
||||||
|
try {
|
||||||
const url = new URL(urlString);
|
const url = new URL(urlString);
|
||||||
|
|
||||||
const info = [
|
const info = [
|
||||||
["scheme", url.protocol],
|
["scheme", url.protocol],
|
||||||
["username", url.username],
|
["username", url.username],
|
||||||
["password", url.password],
|
["password", url.password],
|
||||||
["host", url.host],
|
["host", url.host],
|
||||||
["path", url.pathname],
|
["path", url.pathname],
|
||||||
["hash", url.hash],
|
["hash", url.hash],
|
||||||
];
|
];
|
||||||
|
|
||||||
return `\`\`\`py
|
return `\`\`\`py
|
||||||
${info.filter(kv => kv[1].length > 0).map(([k, v]) => `${k} = ${v}`).join("\n")}
|
${info
|
||||||
|
.filter((kv) => kv[1].length > 0)
|
||||||
|
.map(([k, v]) => `${k} = ${v}`)
|
||||||
|
.join("\n")}
|
||||||
|
|
||||||
${JSON.stringify(
|
${JSON.stringify(
|
||||||
url.searchParams,
|
url.searchParams,
|
||||||
(_, v) => v instanceof URLSearchParams ? Object.fromEntries(v.entries()) : v,
|
(_, v) => (v instanceof URLSearchParams ? Object.fromEntries(v.entries()) : v),
|
||||||
"\t"
|
"\t"
|
||||||
)}
|
)}
|
||||||
\`\`\``;
|
\`\`\``;
|
||||||
|
} catch (err) {
|
||||||
|
return `${Icons.silk.error} Failed to decode URL:\n\`\`\`\n${err}\`\`\``;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
hf.registerCommand(urldecode);
|
hf.registerCommand(urldecode);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue