-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.min.js
1 lines (1 loc) · 2.01 KB
/
index.min.js
1
function pickPointer(){var e=document.getElementsByTagName("rev-share"),t=Math.floor(Math.random()*e.length);const r=e[t]&&Object.values(e[t]).reduce((e,t)=>e+t,0);let n=Math.random()*r;if(void 0!==e[t]){var o=JSON.parse(e[t].getAttribute("pointers"));for(const e in o){const t=o[e];if((n-=t)<=0)return e}}}function genMeta(){var e=document.querySelector("[name='monetization']");e&&e.remove();const t=document.createElement("meta");t.name="monetization",t.content=pickPointer(),document.head.appendChild(t)}class RevShare extends HTMLElement{connectedCallback(){this.style.margin="0",this.style.padding="0",this.hasAttribute("exclusive")&&this.classList.add("rs-excl-hid")}attributeChangedCallback(){if(!this.hasAttribute("pointers"))throw"RevSharePointerError: revshare pointer list cannot be found. Check that it exists and is correctly formatted.";this.checkParsedPointers(),genMeta()}disconnectedCallback(){0==document.getElementsByTagName("rev-share").length?console.warn("RevShareNoElementsWarning: no revshare elements found. Check this is the intended behaviour, and reset the monetization pointer."):genMeta()}static get observedAttributes(){return["pointers"]}checkParsedPointers(){var e=JSON.parse(this.getAttribute("pointers"));Object.values(e).forEach(function(e){if("number"!=typeof e)throw"RevShareProportionTypeError: revshare proportions are of incorrect type. Check that your pointers attribute is correctly formatted."}),100!=Object.values(e).reduce((e,t)=>e+t)&&console.warn("RevShareProportionSumWarning: revshare proportions do not sum to 100. Check that these proportions are correct.")}}"customElements"in window&&customElements.define("rev-share",RevShare);var element=document.createElement("style");element.innerHTML=".rs-excl-hid {display: none;}",document.head.appendChild(element),window.addEventListener("load",()=>{genMeta()}),document.monetization&&document.monetization.addEventListener("monetizationstart",()=>{Array.from(document.querySelectorAll("rev-share[exclusive]")).forEach(e=>e.classList.remove("rs-excl-hid"))});