the_button_helper

Colourise THE BUTTON with the flair you'd get when pressing it right this instance. Also makes it possible to check for your desired colour and press the button when it's (probably) hit.

  1. // ==UserScript==
  2. // @name the_button_helper
  3. // @namespace de.moep.private.reddit
  4. // @author moep (selucram/marcules)
  5. // @description Colourise THE BUTTON with the flair you'd get when pressing it right this instance. Also makes it possible to check for your desired colour and press the button when it's (probably) hit.
  6. // @version 0.0.2
  7. // @match http://*.reddit.com/r/thebutton/*
  8. // @match https://*.reddit.com/r/thebutton/*
  9. // @run-at document-end
  10. // @grant none
  11. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3wQMDjsB0cHvhgAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAA4ElEQVQ4y62VsQ3CQAxF3x0LINFRwQZIFGmTAZgBFgAxQWTRQE0WgBlSUCYLpMgEpMocNAYBIhASv+4s3ZNPsv85GtghS2ABzIGxlmugANIYOX+65xpEAkz4TgXIu9i9yY7Amv9IYmRzPwx6ygCCiHCUkV8eQn3mnu4EEWGVkZdeC0J/BMBpdydsWHkdDSsWXufMirl/GloLxh5jvK6TFbXX3bSi8EBqKEydbsq1RRj8ooqRqfmmDAAy8jIiHAFBR1kSI4eXtMnILx2ln+PrSVoBM2DYImC3984aE7vvF3ADLmo/PKTWbAUAAAAASUVORK5CYII=
  12. // ==/UserScript==
  13.  
  14. function tbh() {
  15. if (typeof(r) === "undefined")
  16. return;
  17.  
  18. // colours
  19. // never click automatically === null
  20. // purple === 6
  21. // blue === 5
  22. // green === 4
  23. // yellow === 3
  24. // orange === 2
  25. // red === 1
  26. var wantedFlairColour = null;
  27.  
  28. var flairColourMapping = {};
  29. var clicked = false;
  30. var buttonButton = null;
  31. var buttonTimer = null;
  32. var buttonPressed = false;
  33.  
  34. buttonButton = document.getElementById('thebutton');
  35. buttonTimer = document.getElementById('thebutton-timer');
  36.  
  37. if (buttonButton) {
  38. buttonPressed = buttonButton.parentNode.classList.contains('pressed');
  39. }
  40.  
  41. flairColourMapping.colourNotPressed = getColourFor('flair-no-press');
  42. flairColourMapping['6'] = getColourFor('flair-press-6');
  43. flairColourMapping['5'] = getColourFor('flair-press-5');
  44. flairColourMapping['4'] = getColourFor('flair-press-4');
  45. flairColourMapping['3'] = getColourFor('flair-press-3');
  46. flairColourMapping['2'] = getColourFor('flair-press-2');
  47. flairColourMapping['1'] = getColourFor('flair-press-1');
  48.  
  49. r.thebutton._origDraw = r.thebutton._drawPie;
  50. r.thebutton._drawPie = function(e, t) {
  51. var currentColour = flairColourMapping.colourNotPressed;
  52. if (e) {
  53. var currentSecond = Math.ceil(e / 10000);
  54. if (currentSecond) {
  55. currentColour = flairColourMapping[("" + currentSecond)];
  56. }
  57.  
  58. if (currentSecond === wantedFlairColour && clicked === false) {
  59. buttonButton.click();
  60. clicked = true;
  61. }
  62. }
  63.  
  64. var n = t - e,
  65. r = google.visualization.arrayToDataTable([
  66. ["", ""],
  67. ["gone", n],
  68. ["remaining", e]
  69. ]),
  70. i = {
  71. chartArea: {
  72. top: 0,
  73. left: 0,
  74. width: 70,
  75. height: 70
  76. },
  77. pieSliceBorderColor: "transparent",
  78. legend: "none",
  79. pieSliceText: "none",
  80. slices: {
  81. 0: {
  82. color: "#C8C8C8"
  83. },
  84. 1: {
  85. color: currentColour
  86. }
  87. },
  88. enableInteractivity: !1
  89. };
  90.  
  91. buttonButton.style.backgroundColor = buttonPressed ? 'transparent' : currentColour;
  92. buttonButton.style.border = buttonPressed ? buttonButton.style.border : "1px solid " + currentColour;
  93. buttonButton.style.boxShadow = buttonPressed ? buttonButton.style.boxShadow : "0px 4px 0px 0px " + currentColour + ",0px 0px 0px 0px rgba(255,255,255,0.6),0px 6px 4px 0px rgba(0,0,0,0.3),inset 0px 1px 0px 0px rgba(255,255,255,0.2)";
  94.  
  95. buttonTimer.style.color = currentColour;
  96. this._chart.draw(r, i);
  97. };
  98. }
  99.  
  100. if (document.body) {
  101. if (typeof(r) === "undefined") {
  102. var script = document.createElement('script');
  103. script.type = "text/javascript";
  104. script.appendChild(document.createTextNode('(' + tbh + ')();'));
  105. document.body.appendChild(script);
  106. }
  107. else // Operate normally.
  108. tbh();
  109. }
  110.  
  111. function getColourFor(className) {
  112. var pseudoElement = document.createElement('div');
  113. pseudoElement.className = className;
  114. document.body.appendChild(pseudoElement);
  115. var color = window.getComputedStyle(pseudoElement, null).getPropertyValue('background-color');
  116. document.body.removeChild(pseudoElement);
  117. return color;
  118. }