Last active
April 10, 2025 17:50
-
-
Save i8degrees/4872257e3854d2e508f838df3f1a0c12 to your computer and use it in GitHub Desktop.
Borrowed from a chrome web extension at https://github.com/mikan/ec-url-cleaner
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
#!/usr/bin/env node | |
`use strict`; | |
import {argv} from "node:process"; | |
import {URL} from "node:url"; | |
//const url = require('node:url'); | |
// [SOURCE](https://github.com/mikan/ec-url-cleaner) | |
/* node:coverage disable */ | |
/* | |
if (this.chrome) { | |
chrome.runtime.onMessage.addListener(() => { | |
const result = clean(window.location.href); | |
document.getElementsByTagName("body")[0].focus(); | |
navigator.clipboard.writeText(result).then(() => { | |
console.log("ec-url-cleaner: " + result); | |
}); | |
}); | |
} | |
*/ | |
/* node:coverage enable */ | |
/** | |
* Remove noisy params from given URL. | |
* | |
* @param url {string} URL as string | |
* @return string | |
*/ | |
function clean(url) { | |
const parser = new URL(url); | |
let path = parser.pathname; | |
const tokens = path.split("/"); | |
// only query params are deleted for non-amazon sites | |
if (!parser.host.startsWith("www.amazon.")) { | |
return parser.origin + path; | |
} | |
// delete ref | |
if (tokens[tokens.length - 1].startsWith("ref=")) { | |
tokens.splice(tokens.length - 1, 1); | |
path = tokens.join("/"); | |
} | |
// category | |
if (tokens.indexOf("b") > 0) { | |
return parser.origin + "/b/?node=" + parser.searchParams.get("node"); | |
} else if (tokens.indexOf("s") > 0) { | |
return ( | |
parser.origin + | |
"/s/?bbn=" + | |
parser.searchParams.get("bbn") + | |
"&rh=" + | |
parser.searchParams.get("rh") | |
); | |
} else if (path.startsWith("/gp/browse.html")) { | |
return parser.origin + "/gp/browse.html?node=" + parser.searchParams.get("node"); | |
} | |
// products | |
if (tokens.indexOf("dp") > 1 && tokens[2] === "dp") { | |
path = "/dp/" + tokens[3] + "/"; | |
} else if (path.startsWith("/gp/product/")) { | |
path = "/dp/" + tokens[3] + "/"; | |
} else if (path.startsWith("/gp/video/detail/")) { | |
path = "/dp/" + tokens[4] + "/"; | |
} | |
return parser.origin + path; | |
} | |
try { | |
module.exports = { clean }; | |
} catch (e) { | |
// module used by unit testing | |
} | |
/* | |
usage | |
./clean.mjs <INPUT_URL> | |
...where INPUT_URL must be shell escaped when passing said URL string inside a shell context | |
input -> "https://www.amazon.com/Document-Portable-Auto-Feed-Rechargeable-PDSDK-ST470PU-VP/dp/B00J2FB9EA/ref=sr_1_4?dib=eyJ2IjoiMSJ9.KCG4iplZNLIw3RqLj-0-VsxZfQWVkJBqH2B1EgdNDXG7j4d8GCXD4PhKZsFYsLgP-kayvBzWgfjoxPKNNeofS6-ILUxTIr7R1cpu-yvY4Jcms1fbs9IrIoHiSpebqM1eTNbblvN1AUKnfI98ThTrPDu9G3WqHLI5uEjOKBupanv7y3Iwo8Qx_umNJNsHDxHIfdmKVZe7TQIBFDvNgu8jSUZ95wyAm19kHwnKkvCSrJ8.L3sDYuaadVvssV5ZfpUScO5MEgSBkiXQSmCHsd6FOPM&dib_tag=se&keywords=vupoint+magic+scanner&qid=1744304364&sr=8-4" | |
output -> https://www.amazon.com/dp/B00J2FB9EA/ | |
input -> 'https://www.amazon.com/Document-Portable-Auto-Feed-Rechargeable-PDSDK-ST470PU-VP/dp/B00J2FB9EA/ref=sr_1_4?dib=eyJ2IjoiMSJ9.KCG4iplZNLIw3RqLj-0-VsxZfQWVkJBqH2B1EgdNDXG7j4d8GCXD4PhKZsFYsLgP-kayvBzWgfjoxPKNNeofS6-ILUxTIr7R1cpu-yvY4Jcms1fbs9IrIoHiSpebqM1eTNbblvN1AUKnfI98ThTrPDu9G3WqHLI5uEjOKBupanv7y3Iwo8Qx_umNJNsHDxHIfdmKVZe7TQIBFDvNgu8jSUZ95wyAm19kHwnKkvCSrJ8.L3sDYuaadVvssV5ZfpUScO5MEgSBkiXQSmCHsd6FOPM&dib_tag=se&keywords=vupoint+magic+scanner&qid=1744304364&sr=8-4' | |
output -> https://www.amazon.com/dp/B00J2FB9EA/ | |
*/ | |
import { strict as assert } from 'node:assert'; | |
{ | |
const testUrl = "https://www.amazon.com/"; | |
const expectedUrl = "https://www.amazon.com/"; | |
const res = clean(testUrl); | |
assert.deepEqual(res, expectedUrl); | |
} | |
let res; | |
// print process.argv | |
argv.forEach((arg, idx) => { | |
// console.log(`${idx}: ${arg}`); | |
if(arg && arg != null && arg != "") { | |
const url = arg; | |
let sanitizedUrl = url.replaceAll(`'`, ``).replaceAll(`"`, ``); | |
// FIXME(JEFF): failure case | |
// sanitizedUrl = new URL(url); | |
// ok | |
sanitizedUrl = URL.parse(url); | |
res = sanitizedUrl; | |
console.log(res); | |
} | |
}); | |
// print process.argv | |
argv.forEach((arg, idx) => { | |
// console.log(`${idx}: ${arg}`); | |
if(arg && arg != null && arg != "") { | |
const url = arg; | |
let sanitizedUrl = url.replaceAll(`'`, ``).replaceAll(`"`, ``); | |
// FIXME(JEFF): failure case | |
// sanitizedUrl = new URL(url); | |
sanitizedUrl = URL.parse(url); | |
// error | |
res = clean(sanitizedUrl); | |
console.log(res); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment