1(function ($, Drupal, drupalSettings) { 2 /** 3 * FAMS-1135: Helper to remove commas from price. 4 * Google Search Console returns errors when prices contain commas. 5 */ 6 function formatPrice(price) { 7 return price ? price.replace(/,/g, '') : ''; 8 } 9 10 Drupal.behaviors.jsonldData = { 11 attach: function (context, settings) { 12 $(window).on("load", function () { 13 // Script to update th shopify related information to JSON-LD of "Product TOC" screen 14 if ($(".pane-quicktabs-product-toc-listings").length !== 0) { 15 var $productPriceData = []; 16 $(".pane-quicktabs-product-toc-listings .toc-item", context) 17 .slice(0, 3) 18 .each(function (index, element) { 19 var $product = []; 20 var $tocItem = $(element); 21 22 var $tocPrice = $tocItem.find(".toc-price"); 23 var $url = $tocItem.find(".toc-item-title a").attr("href"); 24 if ($tocPrice.length) { 25 var $currencyCode = $tocPrice.find(".product-currency-code"); 26 var $currency = $tocPrice.find(".shopify-actual-price"); 27 $product["currencyCode"] = $currencyCode.attr("data-currencycode"); 28 $product["currency"] = $currency.text().trim(); 29 $product["availability"] = $tocPrice 30 .find(".shopify-actual-price") 31 .attr("data-availability"); 32 } else { 33 $product["currencyCode"] = ""; 34 $product["currency"] = ""; 35 $product["availability"] = ""; 36 } 37 $product["url"] = $url; 38 $productPriceData.push($product); 39 }); 40 41 // Select the JSON-LD script tag 42 var $jsonLdScript = $('script[data-schema="WebPage"]'); 43 44 if ($jsonLdScript.length) { 45 var jsonLdData = JSON.parse($jsonLdScript.html()); 46 if (jsonLdData["@graph"]) { 47 jsonLdData["@graph"].forEach(function (item) { 48 if ( 49 item["@type"] === "CollectionPage" && 50 item.mainEntity && 51 item.mainEntity.itemListElement 52 ) { 53 item.mainEntity.itemListElement.forEach(function (product) { 54 var urlObj = new URL(product.url); 55 var pathOnly = 56 urlObj.pathname + urlObj.search + urlObj.hash; 57 var matched = $productPriceData.find(function ( 58 productItem 59 ) { 60 return productItem.url === pathOnly; 61 }); 62 if (matched.currency) { 63 product.offers.price = formatPrice(matched.currency); 64 product.offers.priceCurrency = matched.currencyCode || ""; 65 product.offers.availability = 66 "https://schema.org/" + matched.availability || ""; 67 } 68 else{ 69 delete product.offers; 70 } 71 }); 72 } 73 }); 74 } 75 $jsonLdScript.remove(); // Remove the old script 76 var updatedScript = document.createElement("script"); 77 updatedScript.type = "application/ld+json"; 78 updatedScript.setAttribute("data-schema", "WebPage"); 79 updatedScript.text = JSON.stringify(jsonLdData); 80 // Append the updated script to the head or body 81 document.head.appendChild(updatedScript); 82 } 83 } 84 85 // Script to update th shopify related information to JSON-LD of "Product" screen 86 if ($("#fluke-product-atf").length !== 0) { 87 var $rating = 0; 88 var $ratingCount = 0; 89 if ($(".bv_main_container").length !== 0) { 90 if ($(".bv_avgRating_component_container").length) { 91 $rating = $(".bv_avgRating_component_container").text().trim(); 92 $ratingCount = $(".bv_numReviews_text") 93 .text() 94 .trim() 95 .replace(/[()]/g, ""); 96 } 97 } 98 // Select the JSON-LD script tag 99 var $jsonLdScript = $('script[data-schema="WebPage"]'); 100 101 if ($jsonLdScript.length) { 102 var jsonLdData = JSON.parse($jsonLdScript.html()); 103 if (jsonLdData["@graph"]) { 104 jsonLdData["@graph"].forEach(function (item) { 105 if (item["@type"] === "Product" && item.aggregateRating) { 106 item.aggregateRating.ratingValue = $rating; 107 item.aggregateRating.reviewCount = $ratingCount; 108 } 109 }); 110 } 111 $jsonLdScript.remove(); // Remove the old script 112 113 var updatedScript = document.createElement("script"); 114 updatedScript.type = "application/ld+json"; 115 updatedScript.setAttribute("data-schema", "WebPage"); 116 updatedScript.text = JSON.stringify(jsonLdData); 117 118 // Append the updated script to the head or body 119 document.head.appendChild(updatedScript); 120 } 121 } 122 // Script to remove the microdata and meta tags related to bazaarvoice rating in the PDP page 123 setTimeout(function () { 124 var selectors = [ 125 "#fluke-product-atf .bv_main_container", // main product rating 126 "#fluke-product-display-reviews .bv-content-list-container", // Questions & Reviews section 127 "#fluke-product-display-reviews .bv-content-review", 128 ".bv-hidden", 129 ".bv-avatar-popup-target", 130 ".bv-content-datetime", 131 ".bv-content-title-container", 132 ".bv-content-data-count", 133 ".bv-content-container",
134 ".bv-content-details-offset-off", 135 ".fluke-product-display-related-products-list .bv_main_container", // Related products rating 136 "#fluke-product-display-reviews-stars .bv_main_container", 137 '#fluke-product-display-questions .bv-content-list-container' 138 ]; 139 140 selectors.forEach(function(selector) { 141 var $elements = $(selector); 142 if ($elements.length > 0) { 143 $elements.each(function () { 144 $(this).removeAttr("itemprop itemscope itemtype"); 145 $(this) 146 .find("*") 147 .removeAttr("itemprop itemscope itemtype"); 148 $(this).find("meta").remove(); 149 }); 150 } 151 }); 152 }, 4000); 153 154 }); 155 }, 156 }; 157})(jQuery, Drupal, drupalSettings);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.