Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function getRecommendations(url) {
- const splittedUrl = url.split('/');
- const slug = splittedUrl[splittedUrl.length - 1].split('?')[0];
- const response = await fetch('https://depict-ai.appspot.com/recommendations', {
- body: ' { "store": "mini rodini", "slug": "' + slug + '" } ',
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'application/json'
- },
- method: 'POST'
- });
- const obj = await response.json();
- return obj;
- };
- function getRegion() {
- return document.location.href.split('/')[3];
- }
- function populateRecommendations(recommendations, region) {
- for (let idx=0; idx<recommendations.length; idx++) {
- const titleElement = document.querySelectorAll('.product-item-details p a')[idx];
- titleElement.innerText = recommendations[idx]['title'].toUpperCase();
- const priceElement = document.querySelectorAll('.product-item-details .price')[idx];
- priceElement.innerText = 'SEK ' + recommendations[idx]['price'];
- const imageElement = document.querySelectorAll('.product-item-photo img')[idx];
- imageElement.src = recommendations[idx]['imageUrl'].replace('en-se', region);
- const linkElement1 = document.querySelectorAll('.product-item-photo')[idx];
- linkElement1.href = recommendations[idx]['pageUrl'].replace('en-se', region);
- const linkElement2 = document.querySelectorAll('.product-item-details > p > a')[idx];
- linkElement2.href = recommendations[idx]['pageUrl'].replace('en-se', region);
- }
- }
- const recommendationObjects = await getRecommendations(document.location.href);
- const region = getRegion();
- populateRecommendations(recommendationObjects['beforePurchase'], region);
- document.querySelector('#product-addtocart-button').addEventListener("click", function(){
- populateRecommendations(recommendationObjects['afterPurchase'], region);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement