UNPKG

use-cornify

Version:

🦄 Put unicorns and rainbows on any website on the Internet with a single React Hook!

3 lines (2 loc) • 6.93 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("react"),n=(t=require("react-use-konami"))&&"object"==typeof t&&"default"in t?t.default:t,o=function(){function t(t){var e=this;void 0===t&&(t={}),this.options={},this.count=0,this.magicalWords=["Happy","Sparkly","Glittery","Fun","Magical","Lovely","Cute","Charming","Amazing","Wonderful"],this.addKeyboardEvent=function(t){return e.add()},this.options=t,this.initCounter()}var e=t.prototype;return e.start=function(){this.bindEvents(),this.options.addCupcakeButton&&this.addCupcakeButton(),this.add()},e.initCounter=function(){this.count=parseInt(this.getCookie("cornify")),isNaN(this.count)&&(this.count=0)},e.bindEvents=function(){window.addEventListener("keydown",this.addKeyboardEvent)},e.removeEvents=function(){window.removeEventListener("keydown",this.addKeyboardEvent)},e.createUnicornContainer=function(){var t=this,e=document.createElement("div");return e.style.position="fixed",e.className="__cornify_unicorn",e.style.zIndex="143143",e.style.outline="0",e.onclick=function(){return t.add()},e},e.showGrandUnicorn=function(t){t.style.top="50%",t.style.left="50%",t.style.zIndex="143143143"},e.randomizeUnicornPosition=function(t){t.style.top=Math.round(100*Math.random())+"%",t.style.left=Math.round(100*Math.random())+"%"},e.createUnicornImage=function(){var t=document.createElement("img");return t.style.opacity="0",t.style.transition="all .1s linear",t.alt="A lovely unicorn or rainbow",t.onload=function(){t.style.opacity="1"},t},e.onUnicornMouseOver=function(t){var e=1+Math.round(10*Math.random())/100,n="rotate("+Math.round(20*Math.random()-10)+"deg) scale("+e+","+e+")";t.style.transform=n,t.style.webkitTransform=n},e.onUnicornMouseOut=function(t){var e=.9+Math.round(10*Math.random())/100,n="rotate("+Math.round(6*Math.random()-3)+"deg) scale("+e+","+e+")";t.style.transform=n,t.style.webkitTransform=n},e.add=function(){var t,e=this;this.count+=1;var n=15===this.count,o="translate(-50%, -50%)",i=this.createUnicornContainer();n?this.showGrandUnicorn(i):(this.randomizeUnicornPosition(i),o+=" rotate("+Math.round(10*Math.random()-5)+"deg)");var r=this.createUnicornImage(),a=(new Date).getTime();n&&(a=0);var s="https://www.cornify.com/getacorn.php?r="+a+"&url="+(null==(t=document)?void 0:t.location.href);this.options&&(this.options.y||this.options.younicorns)&&(s+="&y="+(this.options.y?this.options.y:this.options.younicorns),Math.random()>.5&&o.length>0&&(o+=" scaleX(-1)")),i.style.transform=o,i.style.webkitTransform=o,r.setAttribute("src",s),i.onmouseover=function(){return e.onUnicornMouseOver(r)},i.onmouseout=function(){return e.onUnicornMouseOut(r)},document.getElementsByTagName("body")[0].appendChild(i),i.appendChild(r),this.count>5&&(this.addCornifyCss(),this.options.addMagicalWords&&this.addMagicalWords()),this.updateCount(),this.dispatchAddEvent()},e.dispatchAddEvent=function(){var t=new Event("cornify");document.dispatchEvent(t)},e.createCountElement=function(t){var e=document.createElement("p");return e.id=t,e.style.position="fixed",e.style.bottom="5px",e.style.left="0px",e.style.right="0px",e.style.zIndex="1000000000",e.style.color="#ff00ff",e.style.textAlign="center",e.style.fontSize="24px",e.style.fontFamily="'Comic Sans MS', 'Comic Sans', 'Marker Felt', serif",e.style.textTransform="uppercase",e},e.updateCount=function(){var t="__this.count",e=document.getElementById(t);null===e&&(e=this.createCountElement(t),document.getElementsByTagName("body")[0].appendChild(e)),e.innerHTML=1===this.count?"You cornified!":"You cornified "+this.count+" times!",this.setCookie("cornify",""+this.count,1e3)},e.setCookie=function(t,e,n){if("undefined"!=typeof window){var o=new Date;o.setTime(o.getTime()+24*n*60*60*1e3);var i="expires="+o.toUTCString();document.cookie=t+"="+e+"; "+i}},e.getCookie=function(t){if("undefined"==typeof window)return"";for(var e=t+"=",n=document.cookie.split(";"),o=0;o<n.length;o++){var i=n[o].trim();if(0===i.indexOf(e))return i.substring(e.length,i.length)}return""},e.remove=function(){this.removeAllUnicorns(),this.removeCounter(),this.removeCupcakeButton(),this.removeCornifyCss(),this.options.addMagicalWords&&this.removeMagicalWords(),this.removeEvents()},e.removeAllUnicorns=function(){var t=Array.from(document.querySelectorAll(".__cornify_unicorn"));t&&t.forEach((function(t){var e;null==(e=t.parentNode)||e.removeChild(t)}))},e.removeCounter=function(){var t=document.getElementById("__this.count");t&&t.parentNode&&t.parentNode.removeChild(t)},e.removeCupcakeButton=function(){var t=document.getElementById("__cornify_cupcake_button");t&&t.parentNode&&t.parentNode.removeChild(t)},e.addCornifyCss=function(){if(!document.getElementById("__cornify_css")){var t=document.getElementsByTagName("head")[0],e=document.createElement("link");e.id="__cornify_css",e.type="text/css",e.rel="stylesheet",e.href="https://www.cornify.com/css/cornify.css",e.media="screen",t.appendChild(e)}},e.removeCornifyCss=function(){var t=document.getElementById("__cornify_css");t&&t.parentNode&&t.parentNode.removeChild(t)},e.getHeadings=function(){return Array.from(document.querySelectorAll("h1, h2, h3, h4, h5, h6"))},e.addMagicalWords=function(){var t=this;this.getHeadings().forEach((function(e){e.innerHTML=t.magicalWords[Math.floor(Math.random()*t.magicalWords.length)]+" "+e.innerHTML}))},e.removeMagicalWords=function(){var t=this;this.getHeadings().forEach((function(e){t.magicalWords.forEach((function(t){e.innerHTML=e.innerHTML.split(t).join("").trim()}))}))},e.createCupcakeButton=function(t){var e=this,n=document.createElement("div");return n.id=t,n.onclick=function(){return e.remove()},n.style.position="fixed",n.style.top="10px",n.style.right="10px",n.style.zIndex="2147483640",n.setAttribute("aria-label","Hide unicorns and rainbows"),n},e.createCupcakeImage=function(){var t=document.createElement("img");return t.src="https://www.cornify.com/assets/cornify-cupcake-button.png",t.alt="Cupcake button",t.width=50,t.height=50,t.style.width="50px !important",t.style.height="50px !important",t.style.display="block !important",t.style.cursor="pointer !important",t.style.margin="0 !important",t.style.padding="0 !important",t},e.addCupcakeButton=function(){var t="__cornify_cupcake_button",e=document.getElementById(t);if(!e){e=this.createCupcakeButton(t);var n=this.createCupcakeImage();e.appendChild(n),document.getElementsByTagName("body")[0].appendChild(e)}},t}(),i=["ArrowUp","ArrowUp","ArrowDown","ArrowDown","ArrowLeft","ArrowRight","ArrowLeft","ArrowRight","b","a"];exports.useCornify=function(t){var r=void 0===t?{}:t,a=r.addMagicalWords,s=void 0===a||a,c=r.addCupcakeButton,d=void 0===c||c,u=r.younicorns,l=e.useState(r.keys||i)[0],h=new o({younicorns:u,addMagicalWords:s,addCupcakeButton:d}),m=e.useCallback((function(){h.start()}),[h]);return n(m,{code:l}),{remove:function(){return h.remove()}}}; //# sourceMappingURL=use-cornify.cjs.production.min.js.map