Created
May 23, 2019 22:19
-
-
Save lpappone/b3422b8fa68de031f0ea3593f4531397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function(){"use strict";function e(){n("info",[].slice.call(arguments))}function t(){n("warn",[].slice.call(arguments))}function r(){n("error",[].slice.call(arguments))}const n=(e,t)=>{var r,n="[Microsnippet] ";switch(e){case"debug":r=console.debug;break;case"warn":r=console.warn;break;case"error":r=console.error;break;default:r=console.log,e="info"}n=e.toUpperCase()+" : "+n,r.apply(null,[n].concat(t))};var s={error:r,info:e,warn:t};const a=(e,t)=>{for(var r in e)e.hasOwnProperty(r)&&t(e[r],r)},i=e=>"function"==typeof e,o=e=>!!e&&"object"==typeof e,c=e=>void 0===e,p=(e,t)=>{var r=document.querySelector("head"),n=document.createElement("script");n.type="text/javascript",t?n.async=!1:n.defer=!0,n.src=e,r.insertBefore(n,r.firstChild)};let l=0;const d=()=>l++,u=e=>Array.prototype.slice.call(e.querySelectorAll("*"));var y={pushToClient:e=>{var t=window.optimizely.push.bind(window.optimizely);e.errorData&&t({type:"microsnippetError",engine:"microsnippet",errorData:e.errorData}),e.visitorId&&t({type:"user",visitorId:e.visitorId}),e.decisions&&e.decisions.forEach(e=>{t({type:"event",eventType:"decision",eventData:{layerId:e.layerId,experimentId:e.experimentId,variationId:e.variationId}})}),e.activeViews&&e.activeViews.forEach(e=>{t({type:"event",eventType:"pageview",eventData:{id:e.id,apiName:e.apiName}})})}};let h={};function E(e){return Object.freeze(Object.assign({},h))[e]}function b(e,t){Object.assign(h,{[e]:t})}function f(){h={}}var g={get:E,set:b,reset:f};function T({message:e,err:t=null,args:r=[]}){let n={errorData:{code:e,metadata:{},msVersion:g.get("msVersion"),requestId:g.get("requestId"),projectId:g.get("projectId"),snippetKey:g.get("snippetKey")}},a=[e].concat(r);t&&(n.errorData.metadata.err=t,a=a.concat(t)),r.length&&(n.errorData.args=r),s.error.apply(this,a),y.pushToClient(n)}var v={apply:e=>{try{i(e)?e():T({message:"2.0"})}catch(e){T({message:"2.1",err:e})}}},m={DOMInsertionType:{AFTER:"after",APPEND:"append",BEFORE:"before",PREPEND:"prepend"},insertAdjacentHTMLType:{AFTER_BEGIN:"afterbegin",AFTER_END:"afterend",BEFORE_BEGIN:"beforebegin",BEFORE_END:"beforeend"},selectorChangeType:{CLASS:"class",HREF:"href",HTML:"html",SRC:"src",STYLE:"style",TEXT:"text",REMOVE:"remove",HIDE:"hide"}};const A=(e,t)=>{if(0===Object.keys(t.css))return t.attributes.style;var r="",n=t.attributes&&t.attributes.style||"";return a(t.css,(e,t)=>{new RegExp(t+"\\s?:").test(n)||(r+=t+":"+e+";")}),c(t.attributes.style)?(e||"")+r:r+n};var I,S,O=document,N={attributes:!0,childList:!0,subtree:!0,characterData:!0};const D=()=>{I={},S=new MutationObserver(function(){this.disconnect(),Object.keys(I).forEach(R),this.observe(O,N)})},w=()=>{S.observe(O,N)},M=(e,t)=>{S||D();var r=d();return I[r]={selector:e,callback:t},setTimeout(R.bind(null,r),0),w(),()=>{delete I[r],0===Object.keys(I).length&&S.disconnect()}},R=e=>{var t=I[e];if(!t||!t.selector)return delete I[e],void s.warn("MutationObserver - Tried to match observer with selector missing");var r=[];try{r=document.querySelectorAll(t.selector)}catch(r){T({err:r,message:"3.0",args:[t.selector]}),delete I[e]}r.forEach(r=>{r.__optimizelyObserveSelectorIds&&r.__optimizelyObserveSelectorIds[e]||(r.__optimizelyObserveSelectorIds=r.__optimizelyObserveSelectorIds||{},r.__optimizelyObserveSelectorIds[e]=!0,t.callback(r))})};var _={init:D,observeSelector:M},C="optly_change_",k="CHANGE_APPLIER",L=e=>{new j(e.changes).apply()};function j(e){this.changes=e,this.cancelQueue=[]}j.prototype.apply=function(){return this.changes.forEach(e=>{switch(e.type){case"attribute":this.cancelQueue.push(_.observeSelector(e.selector,this.applyAttribute.bind(this,e,d())));break;case"append":this.cancelQueue.push(_.observeSelector(e.selector,this.applyAppend.bind(this,e,d())));break;case"custom_code":this.applyCustomCode(e);break;case"rearrange":Promise.all([new Promise(t=>{this.cancelQueue.push(_.observeSelector(e.selector,t))}),new Promise(t=>{this.cancelQueue.push(_.observeSelector(e.insertSelector,t))})]).then(this.applyRearrange.bind(this,e));break;default:T({message:"1.0",args:[e.type]})}}),this.cancel.bind(this)},j.prototype.applyAttribute=function(e,t,r){var n=C+t;if(r.hasAttribute(n))s.info(k,"AttributeChange not being applied. Element already changed",r);else{s.info(k,"Applying attribute change for: ",e.selector),a(e.attributes,(e,t)=>{switch(t){case m.selectorChangeType.CLASS:c(r.className)||(r.className=e);break;case m.selectorChangeType.HREF:c(r.href)||(r.href=e);break;case m.selectorChangeType.HTML:c(r.innerHTML)||(r.innerHTML=e,u(r).forEach(e=>{e.setAttribute(n,"")}));break;case m.selectorChangeType.SRC:c(r.src)||(r.src=e);break;case m.selectorChangeType.STYLE:break;case m.selectorChangeType.TEXT:c(r.textContent)||(r.textContent=e);break;default:T({message:"1.1",args:[t]})}});var i=A(r.style.cssText,e);"string"==typeof i&&(r.style.cssText=i)}},j.prototype.applyAppend=function(e,t,r){var n=C+t;if(r.hasAttribute(n))return void s.info(k,"AppendChange not being applied. Element already changed",r);function a(e){var t=document.createElement("div");return t.innerHTML=e,t.childNodes.length}function i(e,t,r){for(var n=e,s=[],a=0;a<t;a++)r?(s.push(n.nextSibling),n=n.nextSibling):(s.push(n.previousSibling),n=n.previousSibling);return s}function o(e){for(var t=e.parentNode;t.nodeType!==Node.ELEMENT_NODE;)t=t.parentNode;return t}let c;switch(s.info(k,"Applying append change for: ",e.selector),e.operator){case m.DOMInsertionType.AFTER:c=m.insertAdjacentHTMLType.AFTER_END;break;case m.DOMInsertionType.APPEND:c=m.insertAdjacentHTMLType.BEFORE_END;break;case m.DOMInsertionType.BEFORE:c=m.insertAdjacentHTMLType.BEFORE_BEGIN;break;case m.DOMInsertionType.PREPEND:c=m.insertAdjacentHTMLType.AFTER_BEGIN;break;default:c=m.insertAdjacentHTMLType.BEFORE_END}r.insertAdjacentHTML(c,e.value);var p,l,d=a(e.value)-1;c===m.insertAdjacentHTMLType.BEFORE_END?l=i(p=r.lastChild,d,!1):c===m.insertAdjacentHTMLType.AFTER_BEGIN?l=i(p=r.firstChild,d,!0):c===m.insertAdjacentHTMLType.BEFORE_BEGIN?l=i(p=r.previousSibling,d,!1):c===m.insertAdjacentHTMLType.AFTER_END&&(l=i(p=r.nextSibling,d,!0)),l.unshift(p),l.forEach(e=>{var t=e.nodeType===Node.ELEMENT_NODE?e:o(e);t.setAttribute(n,""),u(t).forEach(e=>{e.setAttribute(n,"")})})},j.prototype.applyRearrange=function(e){document.querySelectorAll(e.selector).forEach(t=>{var r=t.parentNode.removeChild(t),n=document.querySelector(e.insertSelector),s=n.parentNode;switch(e.operator){case m.DOMInsertionType.BEFORE:s.insertBefore(r,n);break;case m.DOMInsertionType.AFTER:s.insertBefore(r,n.nextSibling);break;case m.DOMInsertionType.PREPEND:var a=n.firstChild;n.insertBefore(r,a);break;case m.DOMInsertionType.APPEND:n.appendChild(r)}})},j.prototype.applyCustomCode=function(e){try{e.value()}catch(e){T({message:"1.2",err:e})}},j.prototype.cancel=function(){for(;this.cancelQueue.length;)this.cancelQueue.shift()()};const F=()=>window&&document&&window.MutationObserver;var B=e=>{o(e)?F()?(window.optimizely=window.optimizely||[],s.info("MAIN - Initializing with:",e),"projectJS"in e&&v.apply(e.projectJS),y.pushToClient(e),"changeSets"in e&&e.changeSets.forEach(L),e.trackingSnippetURL&&p(e.trackingSnippetURL)):T({message:"0.2"}):T({message:"0.1"})},H=__DATA__;try{g.set("msVersion","0.0.13-alpha.0"),g.set("requestId",H.requestId),g.set("projectId",H.projectId),g.set("snippetKey",H.snippetKey),B(H)}catch(e){T({message:"0.0",err:e})}}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment