47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
function addShopLinks(imageUrl) {
|
|
document.querySelectorAll(".book-wrapper").forEach(wrapper => {
|
|
if (wrapper.querySelector(".shop_link")) return;
|
|
|
|
const coverLink = wrapper.querySelector(".sp__the-cover a");
|
|
if (!coverLink) return;
|
|
|
|
const href = coverLink.getAttribute("href");
|
|
const isbn = href.split("/")[1];
|
|
if (!isbn) return;
|
|
|
|
const shopLink = document.createElement("p");
|
|
shopLink.innerHTML = `
|
|
<a class="shop_link" href="https://cdcshoppingcart.uchicago.edu/Cart2/ChicagoBook?ISBN=${isbn}&PRESS=wvp" target="_blank">
|
|
<img src="${imageUrl}" alt="Shop Link">
|
|
</a>
|
|
`;
|
|
|
|
wrapper.appendChild(shopLink);
|
|
});
|
|
}
|
|
|
|
function waitForImageUrl(callback) {
|
|
if (window.SearchShopLinkData && window.SearchShopLinkData.imageUrl) {
|
|
callback(window.SearchShopLinkData.imageUrl);
|
|
} else {
|
|
setTimeout(() => waitForImageUrl(callback), 50);
|
|
}
|
|
}
|
|
|
|
waitForImageUrl(function(imageUrl) {
|
|
|
|
// Run once in case elements already exist
|
|
addShopLinks(imageUrl);
|
|
|
|
// Watch for dynamically added book wrappers
|
|
const observer = new MutationObserver(() => {
|
|
addShopLinks(imageUrl);
|
|
});
|
|
|
|
observer.observe(document.body, {
|
|
childList: true,
|
|
subtree: true
|
|
});
|
|
});
|
|
|