Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions tools/pdp-scanner/pdp-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
return resp;
}

async function logResult(result, config) {
async function logResult(result, config, focus) {
const checkSame = (prop) => {
if (result.prod[prop] === undefined && result.new[prop] === undefined) {
return { same: true, value: '' };
Expand All @@ -24,6 +24,21 @@
New: result.new.url,
};

if (focus === 'prod-export') {
const row = document.createElement('tr');
row.innerHTML = `
<td class="url">${result.prod.url}</td>
<td class="status">${result.prod.status}</td>
`;
config.forEach((item) => {
const td = document.createElement('td');
td.innerHTML = result.prod[item.Field];
row.appendChild(td);
});
resultsTable.appendChild(row);
return;
}

const createImage = async (url) => {
const toHumanReadableAgo = (date) => {
if (!date) return 'never';
Expand Down Expand Up @@ -100,7 +115,7 @@
resultsTable.appendChild(row);
}

function extractData(prodDoc, _newDoc, JSONLDData, config, result) {
function extractData(prodDoc, _newDoc, JSONLDData, config, result, focus) {
const findSwatches = (scripts) => {
for (let i = 0; i < scripts.length; i += 1) {
const script = scripts[i];
Expand Down Expand Up @@ -137,14 +152,28 @@
break;
}
case 'productid': {
console.log('productid', item.QuerySelector);

Check warning on line 155 in tools/pdp-scanner/pdp-scanner.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
const prodElem = prodDoc.querySelector(item.QuerySelector);
result.prod.productId = prodElem ? prodElem.textContent : undefined;
result.new.productId = result.prod.productid;
result.new.productId = result.prod.productId;
result.prod.productid = result.prod.productId;
result.new.productid = result.prod.productId;
break;
}
case 'warranty': {
const prodElem = prodDoc.querySelector(item.QuerySelector);
if (prodElem) prodElem.querySelectorAll('style').forEach((style) => style.remove());
if (prodElem) {
prodElem.querySelectorAll('style').forEach((style) => style.remove());
if (focus === 'prod-export') {
const heading = prodElem.querySelector('h2');
const ps = [...prodElem.querySelectorAll('p')];
let sanitizedHTML = '';
if (heading) sanitizedHTML += `${heading.textContent}<br>`;
if (ps.length > 0) sanitizedHTML += `${ps.map((p) => p.innerHTML).join('<br>')}`;
result.prod.warranty = sanitizedHTML;
break;
}
}
result.prod.warranty = prodElem ? prodElem.textContent.trim() : undefined;
result.new.warranty = JSONLDData.custom?.options?.[0]?.name;
break;
Expand Down Expand Up @@ -210,8 +239,8 @@
});
}

async function scanPDP(row, config, reload = false) {
async function scanPDP(row, config, focus, reload = false) {
console.log(row.Prod, row.New);

Check warning on line 243 in tools/pdp-scanner/pdp-scanner.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
const prodUrl = row.Prod;
const newUrl = row.New;

Expand Down Expand Up @@ -241,9 +270,11 @@
JSONLDData = JSON.parse(JSONLD.textContent);
}

extractData(prodDoc, newDoc, JSONLDData, config, result);
await processAuxRequests(config, result);
mapResultValues(result, config);
extractData(prodDoc, newDoc, JSONLDData, config, result, focus);
if (!focus) {
await processAuxRequests(config, result);
mapResultValues(result, config);
}
return result;
}

Expand Down Expand Up @@ -275,7 +306,7 @@
urls = json.data;
}

if (focus) {
if (focus === 'images') {
config = [];
}

Expand All @@ -300,13 +331,15 @@
tableHead.appendChild(th);
});

const thProd = document.createElement('th');
thProd.textContent = 'Prod Screenshot';
tableHead.appendChild(thProd);
if (focus !== 'prod-export') {
const thProd = document.createElement('th');
thProd.textContent = 'Prod Screenshot';
tableHead.appendChild(thProd);

const thNew = document.createElement('th');
thNew.textContent = 'New Screenshot';
tableHead.appendChild(thNew);
const thNew = document.createElement('th');
thNew.textContent = 'New Screenshot';
tableHead.appendChild(thNew);
}

const params = new URLSearchParams(window.location.search);
const limit = +params.get('limit') || urls.length;
Expand All @@ -315,11 +348,11 @@
for (let i = 0; i < limit; i += 1) {
const row = urls[i];
// eslint-disable-next-line no-await-in-loop
const result = await scanPDP(row, config);
const result = await scanPDP(row, config, focus);
result.prod.url = row.Prod;
result.new.url = row.New;
// eslint-disable-next-line no-await-in-loop
await logResult(result, config);
await logResult(result, config, focus);
}
}

Expand Down
Loading