You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB
JavaScript

"use strict";
const cheerio = require("cheerio");
let onclickRegex = /^loadDetailPage\('([^']+)'\);/;
let urlRegex = /url\('([^']+)'\)/;
function getOnclickUrl(onclickText) {
let match = onclickRegex.exec(onclickText);
if (match == null) {
throw new Error("Expected URL not found in onclick handler");
} else {
return match[1];
}
}
function getUrl(cssText) {
let match = urlRegex.exec(cssText);
if (match == null) {
return null; /* Sometimes there is no picture */
} else {
return match[1];
}
}
module.exports = {
parseSearch: function (html) {
let $ = cheerio.load(html);
let products = $(".productresult .media").get().map((row) => {
let $row = $(row);
let onclickValue = $row.attr("onclick");
return getOnclickUrl(onclickValue);
});
return products;
},
parseProduct: function (html) {
let $ = cheerio.load(html);
let brandTextElement = $(".section-data h1 a#brandlink");
let brand = brandTextElement.text().trim();
brandTextElement.remove();
let product = $(".section-data h1").text().trim();
return {
name: `${brand}, ${product}`,
image: getUrl($(".assetImageDefault").attr("style"))
}
}
};