Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save webrulon/b556859b027b68863a8b94f8b5e1c276 to your computer and use it in GitHub Desktop.
Save webrulon/b556859b027b68863a8b94f8b5e1c276 to your computer and use it in GitHub Desktop.
GearBest Cherry Picker adapter
window.cpAdapter = {
getProductTitle: function () {
try {
const [pageData] = window.dataLayer
return pageData.goods.title
} catch (err) {
console.error(err)
return ""
}
},
getProductCategory: function () {
try {
const [pageData] = window.dataLayer
const dataKeys = Object.keys(pageData.goods)
const navKey = dataKeys
.map((key) => key.includes("nav_title"))
.lastIndexOf(true)
return pageData.goods[dataKeys[navKey]]
} catch (error) {
return ""
}
},
getProductPrice: function () {
try {
const [pageData] = window.dataLayer
return pageData.goods.price
} catch (err) {
console.error(err)
return 0
}
},
parseSpecsTable: (specsTable) => {
let textTable = ""
specsTable.forEach((row) => {
console.log(row)
const [keyNode, valueNode] = row.children
const key = keyNode.textContent.trim()
const value = valueNode.textContent.trim()
textTable += `<strong>${key}</strong>: <p>${value}</p>`
})
return textTable
},
getProductDescription: function () {
try {
const textDescNode =
document.querySelector(".textDescContent") ||
document.querySelector(".desc_simp")
let specsTable = document.querySelectorAll(".sizeDescTable")
if (!specsTable.length)
specsTable = document.querySelectorAll(".product_pz_info table tr")
const textDesc = textDescNode.innerHTML
if (!specsTable) return textDesc
const textTable = this.parseSpecsTable(specsTable)
return textDesc.concat(textTable)
} catch (error) {
console.error(error)
return ""
}
},
getProductImages: function () {
try {
const imageNodes = document.querySelectorAll(
"#js-goodsIntroThumbNail img"
)
const images = []
imageNodes.forEach((img) => {
images.push(`${img.dataset.originSrc}_600x600.jpg`)
})
return images
} catch (error) {
console.error(error)
return []
}
},
isProductPage: function () {
try {
const [pageData] = window.dataLayer
return pageData.PAGE === "goods" ? true : false
} catch (err) {
console.error(err)
return false
}
},
getProductData: function () {
const title = this.getProductTitle()
const price = this.getProductPrice()
const category = this.getProductCategory()
const description = this.getProductDescription()
const images = this.getProductImages()
return {
title,
price,
category,
description,
images
}
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment