Advertisement
PastebinHTMLboy

Sheep.js

Sep 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* reference me and all of your worries about making everything perfect goes away! ;) */
  2. try {
  3.   window.cookie=localStorage;
  4. } catch (o) {
  5.   window.cookie={
  6.     getItem(o){
  7.       return cookie[o];
  8.     },setItem (o,e) {
  9.       cookie[o]=e;
  10.     },removeItem (o) {
  11.       delete cookie[o];
  12.     }
  13.   };
  14. }
  15. (function() {
  16.   var thisscript=document.querySelector('script[src$="sheep.js"]'),
  17.   style=document.createElement("link");
  18.   style.type="text/css";
  19.   style.rel="stylesheet";
  20.   if (thisscript) style.href=thisscript.getAttribute('src').replace('.js','.css');
  21.   else style.href="https://sheeptester.github.io/sheep.css";
  22.   document.head.insertBefore(style,document.head.firstChild);
  23.   var el=document.createElement("sheepmenu");
  24.   el.style.display='none';
  25.   style.onload=e=>el.style.display='block';
  26.   if (!cookie.sheepmenuposition) cookie.sheepmenuposition='10-10';
  27.   el.style.right=cookie.sheepmenuposition.slice(0,cookie.sheepmenuposition.indexOf(','))+'px';
  28.   el.style.bottom=cookie.sheepmenuposition.slice(cookie.sheepmenuposition.indexOf(',')+1)+'px';
  29.   var svg=document.createElementNS("http://www.w3.org/2000/svg","svg"); // http://stackoverflow.com/questions/8215021/create-svg-tag-with-javascript
  30.   svg.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xlink","http://www.w3.org/1999/xlink");
  31.   svg.setAttributeNS(null,'viewBox','0 0 480 480');
  32.   svg.draggable=false;
  33.   var path=document.createElementNS("http://www.w3.org/2000/svg","path"); // http://stackoverflow.com/questions/16488884/add-svg-element-to-existing-svg-using-dom
  34.   path.setAttributeNS(null,'d',`M90 90a50 50 0 0 0 0 100H98.579A150 150 0 1 0 381.421 190H390a50 50 0 0 0 0 -100a50 50 0 1 1 -100 0a50 50 0 1 1 -100 0a50 50 0 1 1 -100 0z`);
  35.   var sequence=[38,38,40,40,37,39,37,39,66,65],pos=0;
  36.   document.addEventListener("keydown",e=>{
  37.     if (e.keyCode===sequence[pos]) pos++;
  38.     else pos=0;
  39.     if (pos===sequence.length) {
  40.       el.classList.add('SHEEPGLARE');
  41.       var p=document.createElementNS("http://www.w3.org/2000/svg","path");
  42.       p.style.fill='red';
  43.       p.style.stroke='none';
  44.       p.setAttributeNS(null,'d',`M180 220a15 15 0 0 0 30 0a15 15 0 0 0 -30 0zM300 220a15 15 0 0 0 -30 0a15 15 0 0 0 30 0z`);
  45.       svg.appendChild(p);
  46.     }
  47.   },false);
  48.   svg.appendChild(path);
  49.   el.appendChild(svg);
  50.   document.body.appendChild(el);
  51.   var touchedAlready=false;
  52.   el.onmousedown=function(e){
  53.     if (touchedAlready||e.button===2||e.target.tagName==='SHEEPMENU-MENU'||e.target.tagName==='SHEEPMENU-MENUITEM') return true;
  54.     var mousemoved=false,
  55.     merp=document.createElement("sheepmenu-dragcover"),
  56.     merpies=el.getBoundingClientRect(),
  57.     offset={x:e.clientX-merpies.left,y:e.clientY-merpies.top},
  58.     mouseup=e=>{
  59.       document.removeEventListener("mousemove",mousemove,false);
  60.       document.removeEventListener("mouseup",mouseup,false);
  61.       document.body.classList.remove('SHEEPDRAGGING');
  62.       setTimeout(_=>document.body.removeChild(merp),200);
  63.       if (mousemoved) cookie.sheepmenuposition=el.style.right.slice(0,-2)+','+el.style.bottom.slice(0,-2);
  64.       else if (el.classList.contains('SHEEPGLARE')) {
  65.         alert('Do you like the red room?');
  66.         window.location='/?js';
  67.       } else {
  68.         el.id="SHEEPANIMATING";
  69.         document.body.style.overflow = 'hidden';
  70.         window.setTimeout(_=>window.location="/?from=sheep",300);
  71.       }
  72.       e.preventDefault();
  73.       return false;
  74.     },
  75.     mousemove=e=>{
  76.       mousemoved=true;
  77.       document.body.classList.add('SHEEPDRAGGING');
  78.       el.style.right=(innerWidth-merpies.width-e.clientX+offset.x)+'px';
  79.       el.style.bottom=(innerHeight-merpies.height-e.clientY+offset.y)+'px';
  80.       e.preventDefault();
  81.       return false;
  82.     };
  83.     document.addEventListener("mousemove",mousemove,false);
  84.     document.addEventListener("mouseup",mouseup,false);
  85.     document.body.appendChild(merp);
  86.     e.preventDefault();
  87.     return false;
  88.   };
  89.   el.addEventListener("touchstart",function(e){
  90.     if (e.target.tagName==='SHEEPMENU-MENU'||e.target.tagName==='SHEEPMENU-MENUITEM') return true;
  91.     touchedAlready=true;
  92.     var mousemoved=false,
  93.     merp=document.createElement("sheepmenu-dragcover"),
  94.     merpies=el.getBoundingClientRect(),
  95.     offset={x:e.changedTouches[0].clientX-merpies.left,y:e.changedTouches[0].clientY-merpies.top,lx:0,ly:0,ox:e.changedTouches[0].clientX,oy:e.changedTouches[0].clientY},
  96.     pressstart=+new Date(),
  97.     timeout=setTimeout(_=>{if (!el.classList.contains('SHEEPREADYFORMENU')) el.classList.add('SHEEPREADYFORMENU');},700),
  98.     mouseup=e=>{
  99.       document.removeEventListener("touchmove",mousemove,{passive:false});
  100.       document.removeEventListener("touchend",mouseup,{passive:false});
  101.       document.body.classList.remove('SHEEPDRAGGING');
  102.       setTimeout(_=>document.body.removeChild(merp),200);
  103.       touchedAlready=false;
  104.       clearTimeout(timeout);
  105.       if (el.classList.contains('SHEEPREADYFORMENU')) el.classList.remove('SHEEPREADYFORMENU');
  106.       if (mousemoved) cookie.sheepmenuposition=el.style.right.slice(0,-2)+','+el.style.bottom.slice(0,-2);
  107.       else if (+new Date()-pressstart<700) {
  108.         el.id="SHEEPANIMATING";
  109.         document.body.style.overflow = 'hidden';
  110.         window.setTimeout(_=>window.location="/?from=sheep",300);
  111.       }
  112.       if (+new Date()-pressstart>=700&&offset.lx<10&&offset.ly<10) {
  113.         el.classList.add('SHEEPMENUDONTACTIVE');
  114.         var menu=document.createElement("sheepmenu-menu"),
  115.         location=el.getBoundingClientRect();
  116.         menu.classList.add('SHEEPMENUTOUCH');
  117.         if (location.left<innerWidth/2) menu.style.right='auto';
  118.         else menu.style.left='auto';
  119.         if (location.top<innerHeight/2) menu.style.bottom='auto';
  120.         else menu.style.top='auto';
  121.         for (var span in SHEEP.menu) {
  122.           var t=document.createElement("sheepmenu-menuitem");
  123.           t.textContent=span;
  124.           t.onclick=SHEEP.menu[span];
  125.           menu.appendChild(t);
  126.         }
  127.         el.appendChild(menu);
  128.         var remove=e=>{
  129.           if (e.target.tagName!=='SHEEPMENU-MENU'&&e.target.tagName!=='SHEEPMENU-MENUITEM') {
  130.             contexted=false;
  131.             el.classList.remove('SHEEPMENUDONTACTIVE');
  132.             el.removeChild(menu);
  133.             document.removeEventListener("touchstart",remove,{passive:false});
  134.           }
  135.           if (e.button===2) contexted=true;
  136.         };
  137.         document.addEventListener("touchstart",remove,{passive:false});
  138.       }
  139.       e.preventDefault();
  140.       return false;
  141.     },
  142.     mousemove=e=>{
  143.       mousemoved=true;
  144.       document.body.classList.add('SHEEPDRAGGING');
  145.       el.style.right=(innerWidth-merpies.width-e.changedTouches[0].clientX+offset.x)+'px';
  146.       el.style.bottom=(innerHeight-merpies.height-e.changedTouches[0].clientY+offset.y)+'px';
  147.       offset.lx=Math.abs(offset.ox-e.changedTouches[0].clientX);
  148.       offset.ly=Math.abs(offset.oy-e.changedTouches[0].clientY);
  149.       if (offset.lx<10&&offset.ly<10) {if (!el.classList.contains('SHEEPREADYFORMENU')) el.classList.add('SHEEPREADYFORMENU');}
  150.       else {if (el.classList.contains('SHEEPREADYFORMENU')) el.classList.remove('SHEEPREADYFORMENU');}
  151.       e.preventDefault();
  152.       return false;
  153.     };
  154.     document.addEventListener("touchmove",mousemove,{passive:false});
  155.     document.addEventListener("touchend",mouseup,{passive:false});
  156.     document.body.appendChild(merp);
  157.     e.preventDefault();
  158.     return false;
  159.   },{passive:false});
  160.   if (!window.cookie.dismissed) {
  161.     window.cookie.dismissed='{}';
  162.   }
  163. })();
  164. var SHEEP={
  165.   notify(message,link) {
  166.     var s=document.createElement("sheepnotify"),link;
  167.     s.innerHTML=message;
  168.     if (link) {
  169.       s.classList.add('SHEEPLINKY');
  170.       s.href=link;
  171.       setTimeout(function(){
  172.         s.classList.add('SHEEPDISAPPEAR');
  173.         setTimeout(function(){
  174.           document.body.removeChild(s);
  175.         },300);
  176.       },3000);
  177.       s.onclick=function(e){
  178.         window.location.href=link;
  179.       };
  180.     } else {
  181.       s.onclick=function(e){
  182.         e.target.classList.add('SHEEPDISAPPEAR');
  183.         setTimeout(function(){
  184.           document.body.removeChild(e.target);
  185.         },300);
  186.       };
  187.     }
  188.     document.body.appendChild(s);
  189.   },
  190.   dismissed:JSON.parse(window.cookie.dismissed),
  191.   dismiss(name) {
  192.     SHEEP.dismissed[name]=1;
  193.     window.cookie.dismissed=JSON.stringify(SHEEP.dismissed);
  194.   },
  195.   undismiss(name) {
  196.     SHEEP.dismissed[name]=0;
  197.     window.cookie.dismissed=JSON.stringify(SHEEP.dismissed);
  198.   },
  199.   textwidth(elem,text) {
  200.     /*
  201.       would
  202.       (document.querySelector('input')) gets value of input using input's styling
  203.       (document.querySelector('input'),'lol') uses input's style but a with custom value
  204.       (document.querySelector('p')) uses text content of element using its styling
  205.       (document.querySelector('p'),'lol') uses element's style but with custom text
  206.       ('lol') uses text with inherit styling
  207.       ('lol','15px monospace') uses text with custom styling
  208.     */
  209.     var text,font;
  210.     if (typeof elem==='object') {
  211.       font=document.defaultView.getComputedStyle(elem).font; // if its an element use its font
  212.       if (!text) {
  213.         if (elem.value) text=elem.value;
  214.         else text=elem.textContent;
  215.       }
  216.     } else if (typeof elem==='string') {
  217.       if (text) font=text;
  218.       else {
  219.         font='inherit';
  220.         text=elem;
  221.       }
  222.     }
  223.     let clone=document.createElement('sheepgettextwidth');
  224.     clone.appendChild(document.createTextNode(text));
  225.     clone.style.font=font;
  226.     document.body.appendChild(clone);
  227.     let smth=clone.offsetWidth;
  228.     document.body.removeChild(clone);
  229.     return smth;
  230.   },
  231.   search(input) {
  232.     if (typeof input==='string') {
  233.       var result={},search=input;
  234.       for (var i=1;i<search.length;i++) {
  235.         var nextAmpersand=search.indexOf('&',i);
  236.         var name;
  237.         if (nextAmpersand>-1) {
  238.           name=search.slice(i,nextAmpersand);
  239.           i=nextAmpersand;
  240.         } else {
  241.           name=search.slice(i);
  242.           i=search.length;
  243.         }
  244.         if (name.indexOf('=')>-1) {
  245.           var value=name.slice(name.indexOf('=')+1);
  246.           name=name.slice(0,name.indexOf('='));
  247.           if (!isNaN(Number(value))) value=Number(value);
  248.           else if (value=='true'||value=='✔') value=true;
  249.           else if (value=='false'||value=='✖') value=false;
  250.           result[name]=value;
  251.         } else {
  252.           result[name]=true;
  253.         }
  254.       }
  255.       return result;
  256.     } else if (typeof input==='object') {
  257.       var result='?',object=input;
  258.       for (var key in object) {
  259.         if (result[result.length-1]=='?') result+=key+'='+object[key];
  260.         else result+='&'+key+'='+object[key];
  261.       }
  262.       return result;
  263.     }
  264.     else return undefined;
  265.   },
  266.   ajax(url,callback,error) {
  267.     var xmlHttp=new XMLHttpRequest(),error;
  268.     xmlHttp.onreadystatechange=function(){
  269.       if (xmlHttp.readyState===4) {
  270.         if (xmlHttp.status===200) callback(xmlHttp.responseText);
  271.         else if (error) error(xmlHttp.status);
  272.       }
  273.     };
  274.     xmlHttp.open("GET",url,true); // true for asynchronous
  275.     xmlHttp.send(null);
  276.   },
  277.   draggable(elem,xwise,ywise,options) {
  278.     /* OPTIONS: x y minx miny maxx maxy onchange parentdrag min max fitparent*/
  279.     var drag={},
  280.     x,y,min,max,
  281.     idenifydrag=e=>{
  282.       var x,y;
  283.       if (xwise) x=Number(elem.style.left.slice(0,-2));
  284.       if (ywise) y=Number(elem.style.top.slice(0,-2));
  285.       if (xwise) x=e.clientX-drag.offx;
  286.       if (ywise) y=e.clientY-drag.offy;
  287.       if (min) {
  288.         if (xwise) {
  289.           var m=options.min||options.minx;
  290.           if (m&&x<m) {drag.offx+=x-m;x=m;}
  291.           else if (options.fitparent&&x<0) {drag.offx+=x;x=0;}
  292.         }
  293.         if (ywise) {
  294.           var m=options.min||options.miny;
  295.           if (m&&y<m) {drag.offy+=y-m;y=m;}
  296.           else if (options.fitparent&&y<0) {drag.offy+=y;y=0;}
  297.         }
  298.       }
  299.       if (max) {
  300.         if (xwise) {
  301.           var m=options.max||options.maxx||(options.fitparent?elem.parentNode.offsetWidth-elem.offsetWidth:0);
  302.           if (m&&x>m) {drag.offx+=x-m;x=m;}
  303.         }
  304.         if (ywise) {
  305.           var m=options.max||options.maxy||(options.fitparent?elem.parentNode.offsetHeight-elem.offsetHeight:0);
  306.           if (m&&y>m) {drag.offy+=y-m;y=m;}
  307.         }
  308.       }
  309.       if (xwise) elem.style.left=x+'px';
  310.       if (ywise) elem.style.top=y+'px';
  311.       if (options.onchange) {
  312.         if (xwise) options.onchange(x,y);
  313.         else options.onchange(y);
  314.       }
  315.     },
  316.     mousedown=(touch,e)=>{
  317.       if (!drag.dragging) {
  318.         drag.dragging=true;
  319.         if (xwise) drag.offx=e.clientX-Number(elem.style.left.slice(0,-2));
  320.         if (ywise) drag.offy=e.clientY-Number(elem.style.top.slice(0,-2));
  321.         if (touch) {
  322.           move=e=>{
  323.             if (drag.dragging) {
  324.               idenifydrag(e.changedTouches[0]);
  325.               e.preventDefault();
  326.               return false;
  327.             }
  328.           };
  329.           end=e=>{
  330.             if (drag.dragging) {
  331.               drag.dragging=false;
  332.               document.removeEventListener("touchmove",move,{passive:false});
  333.               document.removeEventListener("touchend",end,{passive:false});
  334.             }
  335.           };
  336.           document.addEventListener("touchmove",move,{passive:false});
  337.           document.addEventListener("touchend",end,{passive:false});
  338.         } else {
  339.           move=e=>{
  340.             if (drag.dragging) {
  341.               idenifydrag(e);
  342.             }
  343.           };
  344.           up=e=>{
  345.             if (drag.dragging) {
  346.               idenifydrag(e);
  347.               drag.dragging=false;
  348.               document.removeEventListener("mousemove",move,false);
  349.               document.removeEventListener("mouseup",up,false);
  350.             }
  351.           };
  352.           document.addEventListener("mousemove",move,false);
  353.           document.addEventListener("mouseup",up,false);
  354.         }
  355.       }
  356.     };
  357.     if (options) {
  358.       if (xwise) elem.style.left=(options.x||0)+'px';
  359.       if (ywise) elem.style.top=(options.y||0)+'px';
  360.       if (options.min||options.minx||options.miny||options.fitparent) min=true;
  361.       if (options.max||options.maxx||options.maxy||options.fitparent) max=true;
  362.     }
  363.     if (document.defaultView.getComputedStyle(elem).position!='absolute') elem.style.position='absolute';
  364.     if (elem.parentNode!==document.body&&document.defaultView.getComputedStyle(elem.parentNode).position=='static')
  365.       elem.parentNode.style.position='relative';
  366.     if (options.parentdrag) {
  367.       elem.parentNode.addEventListener("mousedown",e=>mousedown(false,e),false);
  368.       elem.parentNode.addEventListener("touchstart",e=>mousedown(true,e.changedTouches[0]),{passive:false});
  369.     } else {
  370.       elem.addEventListener("mousedown",e=>mousedown(false,e),false);
  371.       elem.addEventListener("touchstart",e=>mousedown(true,e.changedTouches[0]),{passive:false});
  372.     }
  373.   },
  374.   pixelratio() {
  375.     // http://stackoverflow.com/questions/15661339/how-do-i-fix-blurry-text-in-my-html5-canvas
  376.     var ctx=document.createElement("canvas").getContext("2d"),
  377.     dpr=window.devicePixelRatio||1,
  378.     bsr=ctx.webkitBackingStorePixelRatio||
  379.       ctx.mozBackingStorePixelRatio||
  380.       ctx.msBackingStorePixelRatio||
  381.       ctx.oBackingStorePixelRatio||
  382.       ctx.backingStorePixelRatio||1;
  383.     return dpr/bsr;
  384.   },
  385.   menu:{
  386.     'go to index page':_=>window.location='/?from=sheep',
  387.     'reset little sheep position':_=>{
  388.       cookie.sheepmenuposition='10-10';
  389.       var t=document.querySelector('sheepmenu');
  390.       t.style.right='10px';
  391.       t.style.bottom='10px';
  392.     },
  393.     'see page description':_=>{
  394.       var t=document.querySelector('meta[name=description]');
  395.       if (t) SHEEP.notify('<strong>Page description</strong><br>'+t.content);
  396.       else SHEEP.notify('This page has no description.');
  397.     },
  398.     'eval.js':_=>{
  399.       window.location="javascript:var s=document.createElement('script');s.type='text/javascript';s.src='https://sheeptester.github.io/javascripts/eval.js';document.body.appendChild(s);void(0);";
  400.     }
  401.   }
  402. };
  403. (function(){
  404.   var el=document.querySelector('sheepmenu'),
  405.   contexted=false;
  406.   el.oncontextmenu=e=>{
  407.     if (contexted) contexted=false;
  408.     else {
  409.       contexted=true;
  410.       el.classList.add('SHEEPMENUDONTACTIVE');
  411.       var menu=document.createElement("sheepmenu-menu"),
  412.       location=el.getBoundingClientRect();
  413.       if (location.left<innerWidth/2) menu.style.right='auto';
  414.       else menu.style.left='auto';
  415.       if (location.top<innerHeight/2) menu.style.bottom='auto';
  416.       else menu.style.top='auto';
  417.       for (var span in SHEEP.menu) {
  418.         var t=document.createElement("sheepmenu-menuitem");
  419.         t.textContent=span;
  420.         t.onclick=SHEEP.menu[span];
  421.         menu.appendChild(t);
  422.       }
  423.       el.appendChild(menu);
  424.       var remove=e=>{
  425.         if (e.target.tagName!=='SHEEPMENU-MENU'&&e.target.tagName!=='SHEEPMENU-MENUITEM') {
  426.           contexted=false;
  427.           el.classList.remove('SHEEPMENUDONTACTIVE');
  428.           el.removeChild(menu);
  429.           document.removeEventListener("mousedown",remove,false);
  430.         }
  431.         if (e.button===2) contexted=true;
  432.       };
  433.       document.addEventListener("mousedown",remove,false);
  434.       e.preventDefault();
  435.       return false;
  436.     }
  437.   };
  438.   if (!SHEEP.dismissed.eucookies) {
  439.     SHEEP.dismiss('eucookies');
  440.     SHEEP.notify(
  441.       '<strong>This site uses localStorage.</strong><br>I don\'t care about your privacy.',
  442.       'https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage'
  443.     );
  444.   }
  445.   /*
  446.                  _
  447.         /\      | |
  448.        /  \   __| |___
  449.       / /\ \ / _` / __|
  450.      / ____ \ (_| \__ \
  451.     /_/    \_\__,_|___/
  452.                            */
  453.   else if (!Math.floor(Math.random()*5)) {
  454.     switch (true) {
  455.       case !SHEEP.dismissed.ugwa:
  456.         SHEEP.dismiss('ugwa');
  457.         SHEEP.notify('See our Gunn Web App if you go to Gunn :)','https://orbiit.github.io/gunn-web-app/');
  458.         break;
  459.     }
  460.   }
  461.   SHEEP.ajax('https://sheeptester.github.io/showads.js?v=' + Date.now(),e=>{},e=>{
  462.     return; // TEMP
  463.     const style = document.createElement('style');
  464.     setInterval(() => {
  465.       style.innerHTML = `
  466.         html {
  467.           filter: blur(0.5px) !important;
  468.           animation: test 1s infinite !important;
  469.         }
  470.         @keyframes {
  471.           from { filter: blur(0.5px); }
  472.           to { filter: blur(0.5px); }
  473.         }
  474.       `;
  475.       if (!style.parentNode) document.documentElement.appendChild(style);
  476.       // document.documentElement.style.filter=`blur(0.5px)`;
  477.       // document.documentElement.style.animation = `test 1s infinite`;
  478.       document.documentElement.style.cssText = `filter: blur(0.5px) !important;
  479.       animation: test 1s infinite !important;`;
  480.     }, 0);
  481.   });
  482. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement