xfaqs Rotating Sigs

Rotating Sigs

  1. // ==UserScript==
  2. // @name xfaqs Rotating Sigs
  3. // @version 1.0.0
  4. // @author Kraust
  5. // @namespace Kraust
  6. // @description Rotating Sigs
  7. // @include http://*.gamefaqs.com/*
  8. // @grant none
  9. // @require //cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js
  10. // ==/UserScript==
  11.  
  12.  
  13. /*
  14. This is a mirror of the rotating sig code used in xfaqs
  15. created by Kraust/Judgmenl
  16. If there are any problems please let me know. I only partially looked this over.
  17. I would not use it with xfaqs as xfaqs already has it present.
  18. */
  19.  
  20.  
  21. /*
  22. This uses localStorage to keep Sigs in JSON data
  23. */
  24.  
  25. if( localStorage.getItem("sigList") != null ) {
  26. var sigList = JSON.parse(localStorage.getItem("sigList"));
  27.  
  28. } else {
  29.  
  30. var sigList =
  31. {
  32. "signatures": [
  33. ]
  34. };
  35. localStorage.setItem("sigList", JSON.stringify(sigList));
  36.  
  37.  
  38. }
  39.  
  40.  
  41. /*
  42. There are two callback functions needed for the dynamic click handlers
  43. */
  44.  
  45. function sigClickCallback(i) {
  46. return function() {
  47. var sigText = $("#signature-" + i).val();
  48. var sigLines = (sigText.match(/\n/g)||[]).length;
  49. var sigCharacters = sigText.length + sigLines;
  50. if((sigLines <= 1) && (sigCharacters <= 160)) {
  51.  
  52. $(".btn").attr("disabled", "disabled");
  53. sigList.signatures.splice((i-1), 1);
  54. var boardNameArray = $.csv.toArray($("#boards-" + i).val());
  55. var accountNameArray = $.csv.toArray($("#accounts-" + i).val());
  56. sigList.signatures.push(
  57. {
  58. "boards": boardNameArray,
  59. "accounts": accountNameArray,
  60. "signature": $("#signature-" + i).val()
  61. });
  62.  
  63.  
  64. localStorage.setItem("sigList", JSON.stringify(sigList));
  65. document.location = "/boards/user.php?settings=1#tabs-5";
  66. location.reload(true);
  67. } else {
  68. alert("Signature is too long. " + sigLines + " breaks and " + sigCharacters + " characters.");
  69. }
  70. }
  71. }
  72.  
  73. function sigDeleteCallback(i) {
  74. return function() {
  75. $("#sigTable-" + i).remove();
  76. $(".btn").attr("disabled", "disabled");
  77. sigList.signatures.splice((i-1), 1);
  78. localStorage.setItem("sigList", JSON.stringify(sigList));
  79.  
  80. document.location = "/boards/user.php?settings=1#tabs-5";
  81. location.reload(true);
  82.  
  83. }
  84. }
  85.  
  86.  
  87. /*
  88. This renders the options page
  89. */
  90.  
  91. var sigBody = "<span style='float:right;'><input type='file' class='btn' id='importSigFiles' name='files[]'> <button class='btn' id='importSigs' disabled>Import</button> <button class='btn' id='exportSigs'>Export</button></span><p>1 line break and 160 characters allowed. Just like with regular sigs.<br> If you want a signature to apply to all boards or accounts leave the field blank.<br>Multiple boards and accounts are separated by commas.</p>";
  92. var sigNumber = 0;
  93.  
  94. for( sigNumber; sigNumber < sigList.signatures.length; sigNumber++) {
  95.  
  96. sigBody += "<table id='sigTable-" + (sigNumber + 1) + "'>" +
  97. "<tr><th colspan='2'>Signature " + (sigNumber + 1) + " <input type='submit' class='btn' id='sigBtn-" + (sigNumber + 1) + "' style='float:right; margin-left:10px;' value='Update'><input type='submit' class='btn' id='sigDeleteBtn-" + (sigNumber + 1) + "' style='float:right' value='Delete'></th></tr>" +
  98. "<tr><td>Board Names</td><td><input id='boards-" + (sigNumber + 1) + "' style='width:100%' value=\"" + sigList.signatures[sigNumber].boards + "\"></td></tr>" +
  99. "<tr><td>Accounts</td><td><input id='accounts-" + (sigNumber + 1) + "' style='width:100%' value=\"" + sigList.signatures[sigNumber].accounts + "\"></td></tr>" +
  100. "<tr><td>Signature</td><td><textarea id='signature-" + (sigNumber + 1) + "' style='width:100%'>" + sigList.signatures[sigNumber].signature + "</textarea></td></tr>" +
  101. "</table>";
  102.  
  103. }
  104.  
  105. sigBody += "<table id='sigTable-'" + (sigNumber + 1) + ">" +
  106. "<tr><th colspan='2'> New Signature <input type='submit' class='btn' id='sigBtn-" + (sigNumber + 1) + "' style='float:right' value='Add'></th></tr>" +
  107. "<tr><td>Board Names</td><td><input id='boards-" + (sigNumber + 1) + "' style='width:100%' value=\"" + "" + "\"></td></tr>" +
  108. "<tr><td>Accounts</td><td><input id='accounts-" + (sigNumber + 1) + "' style='width:100%' value=\"" + "" + "\"></td></tr>" +
  109. "<tr><td>Signature</td><td><textarea id='signature-" + (sigNumber + 1) + "' style='width:100%' value=\"" + "" + "\"></textarea></td></tr>" +
  110. "</table>";
  111.  
  112.  
  113. $(".masthead_user").prepend("<span class='masthead_mygames_drop'><a href='/boards/user.php?rotatingsigs=1'>Rotating Signatures <i class='icon icon-cog'></i></a></span> ");
  114.  
  115.  
  116. //Renders the menu
  117. if((decodeURIComponent((new RegExp('[?|&]' + "rotatingsigs" + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20')) == "1") && (location.pathname == "/boards/user.php")) {
  118.  
  119. var user = $("html.js body.wf-active div.wrapper div#mantle_skin div#content.container div.main_content div.span8 div.body table.board tbody tr td").eq(0).text();
  120. $(".span4").remove();
  121. $(".span8").css("width", "100%");
  122. // GameWeasel Fix
  123. if( user == "") {
  124. var user = $("#content > div > div > div.body > table > tbody > tr:nth-child(1) > td").text();
  125. }
  126. var upload_user = user + " ";
  127.  
  128. $(".page-title").html("Rotating Signatures");
  129. $(".userinfo").css("border", "none");
  130. $(".title").remove();
  131. $(".head").remove();
  132. // Preparing for the UI
  133. $("tbody").empty();
  134. // Renders the Upload UI
  135. if( user ) {
  136. $("tbody").append(
  137. "<div id='rs-div'>" +
  138. sigBody +
  139. "</div>"
  140. );
  141. }
  142.  
  143. for(var i = 0; i < sigNumber; i++) {
  144. $("#sigBtn-" + (i + 1)).button();
  145. $("#sigBtn-" + (i + 1)).click(sigClickCallback(i + 1));
  146.  
  147. $("#sigDeleteBtn-" + (i + 1)).button();
  148. $("#sigDeleteBtn-" + (i + 1)).click(sigDeleteCallback(i + 1));
  149.  
  150. }
  151. $("#sigBtn-" + (sigNumber + 1)).button();
  152. $("#sigBtn-" + (sigNumber + 1)).click(function() {
  153. var sigText = $("#signature-" + (sigNumber + 1)).val();
  154. var sigLines = (sigText.match(/\n/g)||[]).length;
  155. var sigCharacters = sigText.length + sigLines;
  156. if((sigLines <= 1) && (sigCharacters <= 160)) {
  157. $(".btn").attr("disabled", "disabled");
  158. var boardNameArray = $.csv.toArray($("#boards-" + (sigNumber + 1)).val());
  159. var accountNameArray = $.csv.toArray($("#accounts-" + (sigNumber + 1)).val());
  160. sigList.signatures.push(
  161. {
  162. "boards": boardNameArray,
  163. "accounts": accountNameArray,
  164. "signature": sigText
  165. });
  166. localStorage.setItem("sigList", JSON.stringify(sigList));
  167. document.location = "/boards/user.php?settings=1#tabs-5";
  168. location.reload(true);
  169. } else {
  170. alert("Signature is too long. " + sigLines + " breaks and " + sigCharacters + " characters.");
  171. }
  172. });
  173.  
  174. $("#exportSigs").click(function() {
  175. var oMyBlob = new Blob([localStorage.sigList], {type : 'application/octet-stream'});
  176. var url = URL.createObjectURL(oMyBlob);
  177. window.open(url, '_blank');
  178. });
  179. // This is off of SO: http://stackoverflow.com/questions/11046919/how-do-i-read-a-text-file-on-my-local-disk-into-a-variable-in-javascript
  180. function handleFileSelect(evt) {
  181. var files = evt.target.files; // FileList object
  182.  
  183. // Loop through the FileList
  184. for (var i = 0, f; f = files[i]; i++) {
  185.  
  186. var reader = new FileReader();
  187.  
  188. // Closure to capture the file information.
  189. reader.onload = (function(theFile) {
  190. return function(e) {
  191. importSigList = e.target.result;
  192. $("#importSigs").removeAttr("disabled");
  193. };
  194. })(f);
  195.  
  196. // Read in the file
  197. //reader.readAsDataText(f,UTF-8);
  198. //reader.readAsDataURL(f);
  199.  
  200. reader.readAsText(f);
  201. }
  202. }
  203. document.getElementById('importSigFiles').addEventListener('change', handleFileSelect, false);
  204. // This is off of SO
  205. $("#importSigs").click(function() {
  206. localStorage.setItem("sigList", importSigList);
  207. document.location = "/boards/user.php?settings=1#tabs-5";
  208. location.reload(true);
  209. });
  210.  
  211.  
  212.  
  213. }
  214.  
  215.  
  216. /*
  217. This uses handles rendering the sigs.
  218. */
  219.  
  220. var sigListLength = sigList.signatures.length;
  221. var randomSig = Math.floor(Math.random() * sigListLength - 1) + 1;
  222. var board = $(".page-title").text();
  223. var randomSignature = sigList.signatures[randomSig].signature;
  224. var validSig = false;
  225. var randomCounter = 0;
  226.  
  227. while(validSig != true) {
  228. randomSig = Math.floor(Math.random() * sigListLength - 1) + 1;
  229. randomSignature = sigList.signatures[randomSig].signature;
  230. for(var j = 1; j <= sigList.signatures[randomSig].accounts.length; j++) {
  231. if((sigList.signatures[randomSig].accounts[0] == "") || (sigList.signatures[randomSig].accounts[j-1] == $(".welcome").text().slice(0, - 1))) {
  232. for(var i = 1; i <= sigList.signatures[randomSig].boards.length; i++) {
  233. if(sigList.signatures[randomSig].boards[0] === "") {
  234. $("input[name='custom_sig']").after("<div class='head'><h2 class='title'>Custom Signature</h2></div>" +
  235. "<textarea name='custom_sig' rows='2' cols='100' style='width:100%;'></textarea>");
  236. $("input[name='custom_sig']").remove();
  237. $("textarea[name='custom_sig']").val(randomSignature);
  238. validSig = true;
  239. break;
  240. } else if(board.toLowerCase() === sigList.signatures[randomSig].boards[i-1].toLowerCase()) {
  241. $("input[name='custom_sig']").after("<div class='head'><h2 class='title'>Custom Signature</h2></div>" +
  242. "<textarea name='custom_sig' rows='2' cols='100'></textarea>");
  243. $("input[name='custom_sig']").remove();
  244. $("textarea[name='custom_sig']").val(randomSignature);
  245. validSig = true;
  246. break;
  247. }
  248. }
  249. }
  250. }
  251. randomCounter++;
  252. if(randomCounter > 100) {
  253. $("input[name='custom_sig']").after("<div class='head'><h2 class='title'>Custom Signature</h2></div>" +
  254. "<textarea name='custom_sig' rows='2' cols='100'></textarea>");
  255. $("input[name='custom_sig']").remove();
  256. $("textarea[name='custom_sig']").val();
  257. validSig = true;
  258. break;
  259. }
  260. }