Last active
October 7, 2021 11:47
-
-
Save aleksanderwalczuk/ecdeba1e4c4276b39b4123cabd1ff80d to your computer and use it in GitHub Desktop.
tdot
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
| const pageFunctions = { | |
| computedHeight: { | |
| selector: '.cart-buttons', | |
| callback: function () { | |
| const table = document.querySelector('#shopping-cart-table') | |
| const btns = document.querySelector('.cart-buttons') | |
| const tableHeightStr = window.getComputedStyle(table).height | |
| const getTableHeight = () => { | |
| if (tableHeightStr) { | |
| const arr = Array.from(tableHeightStr).slice(0, tableHeightStr.length - 2).join('') | |
| return (parseInt(arr)) | |
| } | |
| throw Error('computed style undefined') | |
| } | |
| const height = getTableHeight() | |
| btns.style.top = `${height + 40}px` | |
| } | |
| } | |
| } | |
| function setElementInterval(elementSelector, callbackFn, interval) { | |
| const element = { interval: false, defined: false } | |
| function modifyElement(elementSelector) { | |
| const optionContainer = document.querySelector(elementSelector); | |
| if (!optionContainer) { | |
| return null | |
| } | |
| callbackFn() | |
| element.defined = true | |
| if (element.interval) { | |
| clearInterval(elementInterval) | |
| } | |
| } | |
| if (!element.interval) { | |
| element.interval = setInterval(modifyElement(elementSelector), interval) | |
| } | |
| } | |
| for (methodObj in pageFunctions) { | |
| setElementInterval(pageFunctions[methodObj].selector, pageFunctions[methodObj].callback, 100) | |
| } |
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
| function tidy(calledFromObserver) { | |
| const products = Array.from(document.querySelectorAll(".item .product-description")); | |
| products.map((el, idx) => { | |
| // console.log('beforeCondition: ',el); | |
| if (calledFromObserver) { | |
| debugger | |
| } | |
| if (idx === 0 && calledFromObserver) { | |
| const currentProductName = el.querySelector(".product-name").innerText; | |
| const lastCurrentProductName = products[products.length - 1].querySelector(".product-name").innerText; | |
| let currentProductSummary = document.querySelector('#catalog-products-list > div.category-products > div.toolbar-bottom > div > div.pagination-block.clearfix > div.items > p') | |
| const currentProductSummaryText = currentProductSummary ? currentProductSummary.innerText : 0 | |
| // console.log('currentLast:', lastCurrentProductName) | |
| if ( | |
| currentProductName && currentProductName === firstProductName | |
| && lastCurrentProductName && lastCurrentProductName === lastProductName | |
| && currentProductSummaryText === prevProductSummaryText | |
| ) { | |
| return; | |
| } | |
| prevProductSummaryText = currentProductSummaryText; | |
| firstProductName = currentProductName; | |
| lastProductName = lastCurrentProductName; | |
| } | |
| // console.log('elAfterCondition: ',el); | |
| const capturedDescription = el.querySelector("div.desc.std"); | |
| if (capturedDescription.children) { | |
| const hasFitment = Array.from(capturedDescription.children).filter( | |
| (el) => { | |
| if (el.tagName) { | |
| if ( | |
| el.tagName.toLowerCase() !== "li" && el.tagName.toLowerCase() !== "ul" && | |
| el.innerText.toLowerCase().includes("fitment") | |
| ) { | |
| return el | |
| } | |
| } | |
| } | |
| ); | |
| if (hasFitment.length === 0) { | |
| const fitment = document.createElement("span"); | |
| fitment.innerHTML = "<strong>Fitment:</strong>"; | |
| fitment.classList.add("fitment"); | |
| capturedDescription.appendChild(fitment); | |
| } | |
| const keyFeatures = Array.from(capturedDescription.children).filter((el) => | |
| el.innerText.toLowerCase().includes("key features") | |
| ); | |
| const hasKeyFeaturesItems = Array.from(capturedDescription.children).filter( | |
| (el) => el.tagName.toLowerCase() === "ul" | |
| ); | |
| if (keyFeatures.length === 0 && hasKeyFeaturesItems.length) { | |
| const newHeading = document.createElement("span"); | |
| newHeading.innerHTML = `<strong>Key Features:</strong>`; | |
| capturedDescription.appendChild(newHeading); | |
| } | |
| const capturedLink = document.querySelector("div.desc.std > a"); | |
| if (capturedLink) { | |
| el.appendChild(capturedLink); | |
| return capturedLink; | |
| } | |
| } | |
| }); | |
| } | |
| let firstProductName = null; | |
| let lastProductName = null; | |
| let prevProductSummaryText = null; | |
| const content = document.querySelector("div.content"); | |
| let i = 0; | |
| const observer = new MutationObserver((mutations) => { | |
| tidy(true); | |
| }); | |
| observer.observe(content, { | |
| attributes: false, | |
| childList: true, | |
| subtree: false, | |
| }); | |
| tidy(false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment