-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (33 loc) · 979 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function cleanURL(s) {
try {
const url = new URL(s);
const p = url.pathname
.replace(/\/gp\/(?:product|aw\/d)\//, "/dp/")
.split("/");
const i = p.indexOf("dp") + 1;
if (i <= 0) {
return `${s} is not an Amazon product's URL.`;
}
return `${url.origin.replace("www.", "")}/dp/${p[i]}`;
} catch (e) {
return e.message;
}
}
const urlInput = document.getElementById("url-input");
const cleanButton = document.getElementById("clean-button");
const cleanedURL = document.getElementById("cleaned-url");
const copyButton = document.getElementById("copy-button");
urlInput.addEventListener("keydown", (ev) => {
if (ev.key === "Enter") {
cleanButton.click();
}
});
cleanButton.addEventListener("click", () => {
if (!urlInput.reportValidity()) {
return;
}
cleanedURL.textContent = cleanURL(urlInput.value);
});
copyButton.addEventListener("click", () => {
navigator.clipboard.writeText(cleanedURL.textContent);
});