| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
document.addEventListener('DOMContentLoaded', function () { |
| 5 |
document.querySelectorAll('.bkbg-rvb-wrap[data-schema="1"]').forEach(function (wrap) { |
| 6 |
var d = wrap.dataset; |
| 7 |
var schemaType= d.schemaType || 'Product'; |
| 8 |
var name = d.name || document.title; |
| 9 |
var score = parseFloat(d.score) || 0; |
| 10 |
var maxScore = parseFloat(d.maxScore) || 5; |
| 11 |
var price = d.price || ''; |
| 12 |
|
| 13 |
/* Collect pros/cons from rendered HTML for description */ |
| 14 |
var proEls = wrap.querySelectorAll('.bkbg-rvb-pros li'); |
| 15 |
var conEls = wrap.querySelectorAll('.bkbg-rvb-cons li'); |
| 16 |
var summary = wrap.querySelector('.bkbg-rvb-summary p'); |
| 17 |
|
| 18 |
var pros = Array.prototype.map.call(proEls, function (el) { return el.textContent.trim(); }); |
| 19 |
var cons = Array.prototype.map.call(conEls, function (el) { return el.textContent.trim(); }); |
| 20 |
|
| 21 |
var description = (summary ? summary.textContent.trim() : '') || |
| 22 |
(pros.length ? 'Pros: ' + pros.join(', ') : ''); |
| 23 |
|
| 24 |
var schema = { |
| 25 |
'@context': 'https://schema.org', |
| 26 |
'@type': schemaType, |
| 27 |
'name': name, |
| 28 |
}; |
| 29 |
|
| 30 |
if (description) schema.description = description; |
| 31 |
|
| 32 |
/* AggregateRating */ |
| 33 |
schema.aggregateRating = { |
| 34 |
'@type': 'AggregateRating', |
| 35 |
'ratingValue': score, |
| 36 |
'bestRating': maxScore, |
| 37 |
'worstRating': 1, |
| 38 |
'ratingCount': 1, |
| 39 |
}; |
| 40 |
|
| 41 |
/* Offer / Price */ |
| 42 |
if (price && schemaType === 'Product') { |
| 43 |
schema.offers = { |
| 44 |
'@type': 'Offer', |
| 45 |
'price': price.replace(/[^0-9.,]/g, ''), |
| 46 |
'priceCurrency':'USD', |
| 47 |
'availability': 'https://schema.org/InStock', |
| 48 |
}; |
| 49 |
} |
| 50 |
|
| 51 |
var script = document.createElement('script'); |
| 52 |
script.type = 'application/ld+json'; |
| 53 |
script.textContent = JSON.stringify(schema); |
| 54 |
document.head.appendChild(script); |
| 55 |
}); |
| 56 |
}); |
| 57 |
})(); |
| 58 |
|