Posted By

Kerrick on 08/09/11


Tagged


Versions (?)

Generated easyFlair.user.js


 / Published in: JavaScript
 

  1. // ==UserScript==
  2. // @name Easy Flair
  3. // @namespace http://www.reddit.com/r/Assistance/
  4. // @description Modify a user's flair easily.
  5. // @author RogueDarkJedi
  6. // @homepage http://gamespotting.net/sandbox/easyFlairGen.php
  7. // @version 1.8
  8. // @include http://www.reddit.com/r/Assistance/comments/*
  9. // ==/UserScript==
  10.  
  11. function getUserHash()
  12. {
  13. var saveHash = document.getElementById("saveHash");
  14. saveHash.innerHTML = reddit.modhash;
  15. }
  16.  
  17. var easyFlair = {
  18. user: "",
  19. isOpen: false,
  20. style: "",
  21. modHash: "",
  22. userElement: null,
  23. config: {
  24. /* Subreddit name */
  25. thisSub: "Assistance",
  26. individualFlairs: false,
  27. width: 32,
  28. height: 32,
  29. flairSheet: "",
  30. flairs: [],
  31. },
  32.  
  33. addGlobalRule : function(css) { easyFlair.style += css + "\n"; },
  34.  
  35. injectStyle : function()
  36. {
  37. var style, head = document.getElementsByTagName('head')[0];
  38. if (!head) return;
  39.  
  40. style = document.createElement('style');
  41. style.type = 'text/css';
  42. style.innerHTML = easyFlair.style;
  43. head.appendChild(style);
  44. },
  45.  
  46.  
  47. closePanel : function()
  48. {
  49. var flairPanel = document.getElementById("flairPanel");
  50. flairPanel.setAttribute("class", "easyFlair hideFlair");
  51. easyFlair.isOpen = false;
  52. easyFlair.user = "";
  53. easyFlair.userElement = null;
  54. },
  55.  
  56. setUserFlair: function(event)
  57. {
  58. event.preventDefault();
  59. var cssType = event.target.getAttribute("class").toString().replace("flair-", "");
  60. var theUser = easyFlair.user;
  61. var userRequested = easyFlair.userElement;
  62.  
  63. //alert(cssType + " " + easyFlair.user + " " + easyFlair.modHash);
  64. GM_xmlhttpRequest({
  65. method: "POST",
  66. url: "http://www.reddit.com/api/flair",
  67. data: "css_class=" + cssType + "&id=%23flair-4xc05&name="+ easyFlair.user +"&r="+ easyFlair.thisSub +"&renderstyle=html&uh=" + easyFlair.modHash + "",
  68. headers: {
  69. "Content-Type": "application/x-www-form-urlencoded"
  70. },
  71. onerror: function(response) {
  72. alert("Error! Could not give flair to " + theUser + "! Reddit didn't respond!");
  73. easyFlair.closePanel();
  74. },
  75. onload: function(response) {
  76. if(response.status == 404)
  77. alert("Error! Could not give flair to " + theUser + "! Reddit didn't respond!");
  78. else
  79. {
  80. var flairResponse = document.createElement("span");
  81. flairResponse.setAttribute("class", "flair flair-" + cssType);
  82. userRequested.appendChild(flairResponse);
  83. }
  84. easyFlair.closePanel();
  85. }
  86. });
  87. },
  88.  
  89. mouseOutHide : function(event)
  90. {
  91.  
  92. event.stopPropagation();
  93. event.cancelBubble = true;
  94. if(easyFlair.isOpen == false || (event.relatedTarget == null || event.relatedTarget.parentNode.id == "flairPanel" || event.relatedTarget.parentNode.id == "flairOptions" || event.relatedTarget.id == "flairPanel"))
  95. return;
  96.  
  97. easyFlair.closePanel();
  98.  
  99. },
  100. openPanel: function(event)
  101. {
  102. easyFlair.isOpen = true;
  103. easyFlair.userElement = event.target;
  104. /* Set object position to where the cursor is in this event */
  105. var flairPanelGrab = document.getElementById("flairPanel");
  106. flairPanelGrab.setAttribute("class", "easyFlair");
  107. var xpos = event.pageX, ypos = event.pageY;
  108. flairPanelGrab.setAttribute("style", "top: " + ypos + "px; left: " + xpos + "px;");
  109. },
  110.  
  111.  
  112.  
  113. rollOverUser : function(event)
  114. {
  115. if(event.target.nodeName == "A" && easyFlair.isOpen == false)
  116. {
  117. /* Check to see if it is a user */
  118. var link = event.target.href;
  119. if(((/reddit.com\/user\/*/i)).exec(link) != null)
  120. {
  121. /* Save username */
  122. easyFlair.user = link.toString().replace((/^http:\/\/www.reddit.com\/user\/+/), "", "i").replace("/", "");
  123. easyFlair.openPanel(event);
  124. }
  125. }
  126.  
  127. },
  128.  
  129. inject : function()
  130. {
  131. /* Pure hax */
  132. var saveHash = document.createElement("div");
  133. saveHash.setAttribute("class","hideFlair");
  134. saveHash.setAttribute("id", "saveHash");
  135. document.body.appendChild(saveHash);
  136.  
  137. var script = document.createElement('script');
  138. script.appendChild(document.createTextNode('('+ getUserHash +')();'));
  139. (document.body || document.head || document.documentElement).appendChild(script);
  140.  
  141. /* Create the flair panel */
  142. var panel = document.createElement("div");
  143. panel.setAttribute("id", "flairPanel");
  144. panel.setAttribute("class","easyFlair hideFlair");
  145. var panelFlairs = "<div id='flairOptions'>";
  146.  
  147. for(var i = 0; i < easyFlair.config.flairs.length; ++i)
  148. panelFlairs += "<a class='flair-" + easyFlair.config.flairs[i] + "'>&nbsp;</a>";
  149.  
  150. panel.innerHTML = panelFlairs + "</div>";
  151. document.body.appendChild(panel);
  152.  
  153. easyFlair.addGlobalRule(".easyFlair {border: 1px solid #E1B000; background-color: #FFFDCC; position: absolute; text-align: center; z-index: 999; }");
  154. easyFlair.addGlobalRule(".hideFlair { display: none !important; }");
  155. if(!easyFlair.config.individualFlairs)
  156. {
  157. easyFlair.addGlobalRule("#flairOptions { padding: 0 !important; text-indent: 0px !important; vertical-align: inherit !important; }");
  158. easyFlair.addGlobalRule("#flairOptions a { background-image: url("+ easyFlair.config.flairSheet +"); display: inline-block !important; width: "+easyFlair.config.width+"px !important; height: "+easyFlair.config.height+"px !important;}");
  159. }
  160. easyFlair.injectStyle();
  161.  
  162. easyFlair.modHash = document.getElementById("saveHash").innerHTML;
  163.  
  164. document.getElementById("flairOptions").addEventListener("click", easyFlair.setUserFlair, false);
  165. document.addEventListener("mouseover", easyFlair.rollOverUser, false);
  166. document.getElementById("flairPanel").addEventListener("mouseout", easyFlair.mouseOutHide, false);
  167. },
  168.  
  169. getFlair : function()
  170. {
  171. var styleSheet = "http://www.reddit.com/r/"+ easyFlair.config.thisSub +"/stylesheet.css";
  172. GM_xmlhttpRequest({
  173. method: "GET",
  174. url: styleSheet,
  175. onerror: function(response) {
  176. alert("Error! Could not get flair! Reddit didn't respond!");
  177. },
  178. onload: function(response) {
  179. var cssData = response.responseText;
  180. /* The following was derived from http://userscripts.org/scripts/review/94898 */
  181.  
  182. var pos = cssData.indexOf(".flair", end), end = 0, i = 0;
  183.  
  184. /* Getting image */
  185. if(pos != -1)
  186. {
  187. end = cssData.indexOf(")", pos);
  188. var bgcode = cssData.slice(pos, end).toString();
  189. pos = bgcode.indexOf("htt");
  190. easyFlair.config.flairSheet = bgcode.slice(pos).toString();
  191.  
  192. /* Seeing if each .flair- has a background or if it's done in just .flair */
  193. if(easyFlair.config.flairSheet.length <= 10)
  194. easyFlair.config.individualFlairs = true;
  195. }
  196. pos = end = 0;
  197.  
  198. /* Getting flairs */
  199. while ((pos = cssData.indexOf(".flair-", end)) != -1)
  200. {
  201. end = cssData.indexOf(" {", pos);
  202. var flairRule = cssData.slice(pos, end);
  203. var removeOtherRules = flairRule.indexOf(" ");
  204. if(removeOtherRules != -1)
  205. flairRule = flairRule.slice(0, removeOtherRules);
  206.  
  207. var newFlairRule = flairRule.replace(".flair-", "").replace(" ", "").replace(",", "").toString();
  208.  
  209. /* Check for duplicates */
  210. if(easyFlair.config.flairs.indexOf(newFlairRule) == -1)
  211. easyFlair.config.flairs[i++] = newFlairRule;
  212. }
  213. easyFlair.inject();
  214. }
  215. });
  216. },
  217.  
  218. init : function()
  219. {
  220. /* Only execute if we are logged in */
  221. if(!document.getElementById("mail"))
  222. return;
  223.  
  224. easyFlair.getFlair();
  225. }
  226.  
  227. };
  228.  
  229. easyFlair.init();

Report this snippet  

You need to login to post a comment.