Advertisement
Guest User

getDare Die

a guest
Sep 29th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        getDare Die
  3. // @namespace   getdare
  4. // @include     http://www.getdare.com/*
  5. // @version     1
  6. // @grant       none
  7. // ==/UserScript==
  8.  
  9. if (window !== window.top) {
  10.     return;
  11. }
  12.  
  13. var die = makeDie();
  14. var dieBox = make('div', {id: 'dieBox'}, [die]);
  15. dieBox.addEventListener('click', die.roll, false);
  16. document.querySelector('body').appendChild(make('div', [
  17.     makeStyle(
  18.         '#dieBox', {
  19.             position: 'fixed',
  20.             right: 0,
  21.             bottom: -180,
  22.             width: 100,
  23.             height: 200,
  24.             backgroundColor: 'rgba(255, 255, 2555, 0.5)',
  25.             borderLeft: '2px solid black',
  26.             borderTop: '2px solid black',
  27.             borderRadius: '10px 0 0',
  28.             transition: 'bottom 1s'
  29.         },
  30.         '#dieBox:hover', {
  31.             bottom: -100,
  32.             transition: 'bottom .5s cubic-bezier(0.3, 2, 0.3, 0.7)'
  33.         },
  34.         '#dieBox .die', {
  35.             position: 'absolute',
  36.             left: 25,
  37.             top: 25,
  38.             width: 50,
  39.             height: 50,
  40.             backgroundColor: 'rgb(255, 255, 255)',
  41.             border: '1px solid black',
  42.             borderRadius: 5,
  43.             pointerEvents: 'none'
  44.         },
  45.         '#dieBox .die .pip', {
  46.             position: 'absolute',
  47.             width: 7,
  48.             height: 7,
  49.             left: 21,
  50.             top: 21,
  51.             backgroundColor: 'rgb(0, 0, 0)',
  52.             borderRadius: 7,
  53.             pointerEvents: 'none',
  54.             display: 'none'
  55.         },
  56.         '#dieBox .die .pip.pipLeft', {
  57.             left: 7
  58.         },
  59.         '#dieBox .die .pip.pipRight', {
  60.             left: 35
  61.         },
  62.         '#dieBox .die .pip.pipTop', {
  63.             top: 7
  64.         },
  65.         '#dieBox .die .pip.pipBottom', {
  66.             top: 35
  67.         }
  68.     ),
  69.     dieBox
  70. ]));
  71.  
  72. function makeDie() {
  73.     var isRolling, rollUntil, value = Math.floor(1 + 6 * Math.random());
  74.     var die, pips = [];
  75.     for (var i = 0; i < 6; ++i) {
  76.         pips[i] = make('div', {className: 'pip'});
  77.     }
  78.     die = make('div', {className: 'die'}, pips);
  79.     updatePips();
  80.     die.roll = startRolling;
  81.     return die;
  82.     function startRolling() {
  83.         rollUntil = Date.now() + 500 + Math.random() * 3000;
  84.         if (!isRolling) {
  85.             keepRolling();
  86.         }
  87.     }
  88.     function keepRolling() {
  89.         isRolling = (Date.now() < rollUntil);
  90.         if (isRolling) {
  91.             rollOnce();
  92.             setTimeout(keepRolling, 100);
  93.         }
  94.     }
  95.     function rollOnce() {
  96.         value = nextValue(value);
  97.         updatePips();
  98.         rotateDie();
  99.     }
  100.     function nextValue(value) {
  101.         return 3.5 + Math.pow(-1, Math.round(Math.random())) * ((Math.round(Math.abs(value - 3.5) + Math.random() + 0.5) % 3) + 0.5);
  102.     }
  103.     function updatePips() {
  104.         var pos = [];
  105.         for (var x = 0; x < 3; ++x) {
  106.             [[], [x], [0, 2], [0, 1, 2]][(x & 1) ? (value & 1) : (value >> 1)].forEach(function(y) {
  107.                 pos.push({x: x, y: y});
  108.             });
  109.         }
  110.         pips.forEach(function(pip, i) {
  111.             if (i < value) {
  112.                 make(pip, {
  113.                     display: 'block',
  114.                     className: [
  115.                         'pip',
  116.                         ['pipLeft',, 'pipRight'][pos[i].x],
  117.                         ['pipTop',, 'pipBottom'][pos[i].y]
  118.                     ]
  119.                 });
  120.             } else {
  121.                 make(pip, {
  122.                     display: 'none'
  123.                 });
  124.             }
  125.         });
  126.     }
  127.     function rotateDie() {
  128.         make(die, {
  129.             transform: 'rotate(' + Math.floor(Math.random() * 360) + 'deg)'
  130.         });
  131.     }
  132. }
  133.  
  134. function makeStyle() {
  135.     var styles = make('style');
  136.     styles.type = 'text/css';
  137.     styles.setAttribute('scoped', 'scoped');
  138.     var rules = [];
  139.     for (var i = 0; i < arguments.length; i += 2) {
  140.         var selector = arguments[i],
  141.             rule = arguments[i + 1];
  142.         rules.push([
  143.             hyphenify(selector) + ' {',
  144.                 Object.keys(arguments[i + 1]).map(function(key) {
  145.                     var name = hyphenify(key);
  146.                     var value = rule[key];
  147.                     if (typeof value === 'number') {
  148.                         value = value + 'px';
  149.                     }
  150.                     return '    ' + name + ': ' + value + ';';
  151.                 }).join('\n'),
  152.             '}'
  153.         ].join('\n'));
  154.     }
  155.     styles.textContent = rules.join('\n');
  156.     return styles;
  157. }
  158.  
  159. function make(tag, style, children) {
  160.     var node;
  161.     if (typeof tag === 'string') {
  162.         node = document.createElement(tag);
  163.     } else {
  164.         node = tag;
  165.         tag = node.tagName;
  166.     }
  167.     if (style instanceof Array) {
  168.         children = style;
  169.         style = null;
  170.     }
  171.     if (style) {
  172.         Object.keys(style).forEach(function(key) {
  173.             if (key === 'id') {
  174.                 this.id = hyphenify(style.id);
  175.             } else if (key === 'className') {
  176.                 if (style.className instanceof Array) {
  177.                     this.className = hyphenify(style.className.filter(Boolean).join(' '));
  178.                 } else {
  179.                     this.className = hyphenify(style.className);
  180.                 }
  181.             } else {
  182.                 this.style[key] = style[key];
  183.             }
  184.         }, node);
  185.     }
  186.     if (children) {
  187.         children.forEach(function(child) {
  188.             node.appendChild(child);
  189.         });
  190.     }
  191.     return node;
  192. }
  193.  
  194. function hyphenify(name) {
  195.     return name.replace(/[A-Z]/g, function(a) {
  196.         return '-' + a.toLowerCase();
  197.     });
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement