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