Files
crs-app-real/config/website/Real.js
2021-07-13 17:15:12 +09:00

65 lines
2.8 KiB
JavaScript

class Real {
constructor() {
this.selectors = {
title: '.rd-title',
price: '.rd-buybox__price',
main_image: '.rd-gallery__container--thumbnails .swiper-slide',
// info_box: '.rd-product-description__top-accordion-content-attributes .rd-attribute-table > div',
info_box: 'div.rd-product-description__top-accordion-content > div.rd-product-description__top-accordion-content-attributes .rd-attribute-table > div',
// info_title: 'span.rd-product-description__attribute-title',
info_title: '> span',
// info_data: 'div.rd-product-description__attribute-values',
info_data: 'div > div',
description: '#rd-product-description',
waitElements: []
}
this.selectors.waitElements.push(this.selectors.main_image)
this.selectors.waitElements.push(this.selectors.info_box)
}
extract($, cb) {
let cleanup = (txt) => {
return typeof txt == 'string' ? txt.replace(/[\-\t\n\r]+/g, '').replace(/[\s]{2}/g, '').trim() : txt
}
let info = {}
$(this.selectors.info_box).map((k, el) => {
let infoData = $(el).find(this.selectors.info_data).map((k, v) => {
return cleanup($(v).text())
}).get()
info[$(el).find(this.selectors.info_title).text()] = cleanup(infoData.filter((item, index) => infoData.indexOf(item) === index).join(' | '))
})
info = Object.assign({
title: cleanup($(this.selectors.title).text()),
price: cleanup($(this.selectors.price).text()).replace(',', '.') + ($(this.selectors.price).data('cents') ? cleanup($(this.selectors.price).data('cents')) : ''),
main_img: (() => {
let images = $(this.selectors.main_image)
if (images.length) {
images = images.map((k, v) => {
// return $(v).css('background-image').slice(4, -1).replace(/"/g, "")
return $(v).find('img').attr('src')
}).get()
} else {
images = []
let img = $('.rd-product-image-gallery__slide-large-image')
if (img.length) images = [img.attr('src')]
}
return images.length ? images : null
})()
}, info)
$(this.selectors.description).find('*').each((k, elem) => this.removeAllAttributes($, elem))
info.description = '<div>' + $(this.selectors.description).html() + '</div>'
cb(info)
}
removeAllAttributes = function ($, node) {
node.attributes || Object.keys(node.attribs).map(
name => $(node).removeAttr(name)
);
return node
}
}
module.exports = new Real