Skip to content

Instantly share code, notes, and snippets.

@PabloDinella
Created May 3, 2021 23:33
Show Gist options
  • Save PabloDinella/d3aa35de771f74449b52303c61787c31 to your computer and use it in GitHub Desktop.
Save PabloDinella/d3aa35de771f74449b52303c61787c31 to your computer and use it in GitHub Desktop.
function copy(text) {
var copyText = document.createElement('textarea');
document.body.appendChild(copyText);
copyText.value = text;
copyText.select();
document.execCommand("copy");
}
function infosToCsv() {
function composeHeaders(hv) {
return hv.map(({ header }) => header).join(" ");
}
function composeValues(hv) {
return hv.map(({ value }) => `"${value}"`).join(" ");
}
const infos = document.querySelector(
"#seller-profile-container > div.a-row.a-spacing-medium > div > ul"
);
const headersAndValues = Array.from(infos.children).reduce((final, info) => {
const address = info.querySelector("ul");
if (address) {
const addressAllElements = Array.from(address.children);
const addressUnstructuredElements = addressAllElements.slice(0, addressAllElements.length - 3);
const addressFixedElements = addressAllElements.slice(addressAllElements.length - 3);
const addressHeadersAndValues = addressUnstructuredElements.map(
(addressPart, i) => {
return { header: "addr" + (i + 1), value: addressPart.textContent };
}
);
const fixedAddressInfo = [
{ header: "Location/City/Province", value: addressFixedElements[0].textContent },
{ header: "PostalCode", value: addressFixedElements[1].textContent },
{ header: "Country", value: addressFixedElements[2].textContent },
]
return [...final, ...fixedAddressInfo, ...addressHeadersAndValues];
}
const [header, value] = info.textContent.split(":");
return [...final, { header, value }];
}, []);
const headers = composeHeaders(headersAndValues);
const values = composeValues(headersAndValues);
return headers + "\n" + values;
}
copy(infosToCsv());
function copy(text) {
var copyText = document.createElement('textarea');
document.body.appendChild(copyText);
copyText.value = text;
copyText.select();
document.execCommand("copy");
}
copy(document.querySelector('#sellerProfileTriggerId')?.href);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment