-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsa-yotpo-rich-snippets.min.js
8 lines (8 loc) · 6.46 KB
/
wsa-yotpo-rich-snippets.min.js
1
2
3
4
5
6
7
8
/*
Web Site Advantage: Adding Schema.org rating and review markup to Yotpo [v2.3]
https://websiteadvantage.com.au/Yotpo-Product-Rating-Review-Rich-Snippets
Copyright (C) 2016 Web Site Advantage
*/
var wsa_yotpoPlaceHolder = ".yotpo-main-widget";
var wsa_yotpoSdFormat = "json-ld"; // microdata, json-ld
var webSiteAdvantage=webSiteAdvantage||{};webSiteAdvantage.yotpoRichSnippets=webSiteAdvantage.yotpoRichSnippets||[];webSiteAdvantage.yotpoRichSnippets=function(n){this.initialise(n)};webSiteAdvantage.yotpoRichSnippets.prototype={yotpoPlaceHolder:wsa_yotpoPlaceHolder,format:wsa_yotpoSdFormat,constructor:webSiteAdvantage.yotpoRichSnippets,initialise:function(n){try{if(typeof n!="undefined"&&n!=null&&(this.yotpoPlaceHolder=n),document.querySelector(this.yotpoPlaceHolder)==null&&(console.log("yotpoRichSnippets: Could not find yotpoPlaceHolder "+this.yotpoPlaceHolder),this.yotpoPlaceHolder=".yotpo.placeholder",console.log("yotpoRichSnippets: Trying "+this.yotpoPlaceHolder)),this._yotpoPlaceHolderElement=document.querySelector(this.yotpoPlaceHolder),this._yotpoPlaceHolderElement!=null){var t=this;this._observer=new MutationObserver(function(n){n.forEach(function(n){var r=n.addedNodes,u;if(r!==null)for(i=0;i<r.length;i++)u=r[i],typeof u.classList!="undefined"&&u.classList.contains("yotpo-display-wrapper")&&t._observer!=null&&(t._observer.disconnect(),t._observer=null,t._addMarkup())})});this._observer.observe(this._yotpoPlaceHolderElement,this._observerConfig)}else console.log("yotpoRichSnippets: Could not find yotpoPlaceHolder "+this.yotpoPlaceHolder),console.log("yotpoRichSnippets: Failed");this._observerHead=new MutationObserver(function(n){n.forEach(function(n){var r=n.addedNodes,t;if(r!==null)for(i=0;i<r.length;i++)t=r[i],t.nodeName==="SCRIPT"&&t.getAttribute("type")==="application/ld+json"&&typeof t.classList!="undefined"&&t.classList.contains("y-rich-snippet-script")&&t.parentNode.removeChild(t)})});this._observerHead.observe(document.querySelector("head"),this._observerConfig)}catch(r){console.log("yotpoRichSnippets: initialise Error: "+r.message)}},_markupAdded:!1,_observerHead:null,_observer:null,_observerConfig:{attributes:!0,childList:!0,characterData:!0,subtree:!0},_yotpoPlaceHolderElement:null,_getCount:function(n){var i=document.querySelector('.yotpo-distibutions-sum-reviews span[data-score-distribution="'+n+'"]'),t;return i!=null&&(t=i.textContent,t.length>=3)?parseInt(t.substr(1,t.length-2)):0},_getReviewCount:function(){var t=document.querySelector(".yotpo-reviews-nav-tab-sum"),n;return t!=null&&(n=t.textContent,n.length>=3)?parseInt(n.substr(1,n.length-2)):(console.log("yotpoRichSnippets: Failed to find reviewCount. Using 0"),0)},_addMarkup:function(){var v,y,s,t,h,n,u,c,nt,l,e,p,tt,o,it;if(!this._markupAdded){this._markupAdded=!0;try{var w=this._getCount(5),b=this._getCount(4),k=this._getCount(3),d=this._getCount(2),g=this._getCount(1),f=w+b+k+d+g,a=(w*5+b*4+k*3+d*2+g*1)/f;if(f==0&&(f=this._getReviewCount(),a=document.querySelectorAll(".yotpo-stars-and-sum-reviews .yotpo-stars .yotpo-icon-star").length+document.querySelectorAll(".yotpo-stars-and-sum-reviews .yotpo-stars .yotpo-icon-half-star").length/2),f>0){for(v=window.location.href,y=document.querySelector("link[rel='canonical']"),y&&(v=y.getAttribute("href")),s={"@context":"http://schema.org/","@type":"Product","@id":v+"#Product",review:[]},this.format=="microdata"&&(t=document.querySelector(".yotpo-stars-and-sum-reviews"),t!=null?(t.setAttribute("id","yotpo-aggregate-rating"),t.setAttribute("itemprop","aggregateRating"),t.setAttribute("itemscope",""),t.setAttribute("itemtype","http://schema.org/AggregateRating")):console.log("yotpoRichSnippets: Failed to find aggregateRating Element"),this._addItemProps(t,".based-on","reviewCount",f),this._addItemProps(t,".yotpo-stars-and-sum-reviews .yotpo-stars","ratingValue",a),this._appendItemPropMetaTag(t,"worstRating","1"),this._appendItemPropMetaTag(t,"bestRating","5")),this.format=="json-ld"&&(s.aggregateRating={"@type":"AggregateRating",worstRating:"1",bestRating:"5",ratingValue:a,reviewCount:f}),h=this._yotpoPlaceHolderElement.querySelectorAll(".yotpo-review"),h.length==0&&console.log("yotpoRichSnippets: Failed to find any reviews"),r=0;r<h.length;r++)n=h[r],n.classList.contains("yotpo-hidden")||(this.format=="microdata"&&(n.setAttribute("itemprop","review"),n.setAttribute("itemscope",""),n.setAttribute("itemtype","http://schema.org/Review")),u=n.querySelector(".yotpo-review-stars"),c="",u!=null?(this.format=="microdata"&&(u.setAttribute("itemprop","reviewRating"),u.setAttribute("itemscope",""),u.setAttribute("itemtype","http://schema.org/Rating")),c=u.querySelectorAll(".yotpo-icon-star").length,this.format=="microdata"&&this._appendItemPropMetaTag(u,"ratingValue",c)):console.log("yotpoRichSnippets: Failed to find reviewRating Element for review "+i),this.format=="microdata"&&(this._addItemProps(n,'div[itemprop="review"] > .yotpo-header .yotpo-user-name',"author"),this._addItemProps(n,'div[itemprop="review"] > .yotpo-header .content-title .yotpo-font-bold',"name"),this._addItemProps(n,'div[itemprop="review"] > .yotpo-main .content-review',"reviewBody"),this._addItemProps(n,'div[itemprop="review"] > .yotpo-header .yotpo-review-date',"datePublished")),this.format=="json-ld"&&(nt=this._getInnerHTML(n,"div > .yotpo-header .yotpo-user-name"),l=this._getInnerHTML(n,"div > .yotpo-header .content-title .yotpo-font-bold"),l===""&&(l=this._getInnerHTML(n,"div > .yotpo-main .content-title.yotpo-font-bold")),e=this._getInnerHTML(n,"div > .yotpo-main .content-review"),p=e.indexOf("<span"),p>-1&&(e=e.substring(0,p)),tt=this._getInnerHTML(n,"div > .yotpo-header .yotpo-review-date"),s.review.push({"@type":"Review",author:{"@type":"Person",name:nt},reviewRating:{"@type":"Rating",ratingValue:c},name:l,reviewBody:e,datePublished:tt})));this.format=="json-ld"&&(o=document.createElement("script"),o.type="application/ld+json",o.setAttribute("id","wsa-yotpo-json-ld"),it=document.createTextNode(JSON.stringify(s)),o.appendChild(it),document.querySelector("head").appendChild(o))}}catch(rt){console.log("yotpoRichSnippets: _addMarkup Error: "+rt.message)}}},_appendItemPropMetaTag:function(n,t,i){var r=document.createElement("meta");r.setAttribute("itemprop",t);r.setAttribute("content",i);n.appendChild(r)},_addItemProps:function(n,t,r,u){var e=n.querySelectorAll(t),f;for(i=0;i<e.length;i++)f=e[i],f.setAttribute("itemprop",r),typeof u!="undefined"&&u!=null&&f.setAttribute("content",u)},_getInnerHTML:function(n,t){var i=n.querySelectorAll(t);return i.length>0?i[0].innerHTML:""}};new webSiteAdvantage.yotpoRichSnippets