Skip to content
const observer = new MutationObserver(() => {
const ratingElement = document.querySelector('.overallRating');
const reviewsElement = document.querySelector('.numReviews');
if (ratingElement && reviewsElement) {
const ratingValue = parseFloat(ratingElement.textContent.trim());
const reviewsValue = parseInt(reviewsElement.textContent.replace(/[^\d]/g, '').trim(), 10);
if (ratingValue && reviewsValue) {
const aggregateRatingSchema = {
"@context": "https://schema.org",
"@type": "AggregateRating",
"@id": "#rating",
"ratingValue": ratingValue,
"reviewCount": reviewsValue,
};
const script = document.createElement('script');
script.type = 'application/ld+json';
script.textContent = JSON.stringify(aggregateRatingSchema);
document.head.appendChild(script);
console.log("Schema added successfully");
}
//
observer.disconnect();
}
});
//
observer.observe(document.body, { childList: true, subtree: true });