Empeopled.com Random Topics Bar

Places links to random topics at the top of the frontpage

当前为 2015-09-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @id Emp3
  3. // @name Empeopled.com Random Topics Bar
  4. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  5. // @grant GM_addStyle
  6. // @version 1.2
  7. // @namespace
  8. // @author TrustyPatches
  9. // @description Places links to random topics at the top of the frontpage
  10. // @include https://empeopled.com/*
  11. // @run-at document-idle
  12. // ==/UserScript==
  13.  
  14.  
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. //////////// User Settings ///////////////////////////////////////////////////////////////////////////////////////
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. //
  19. //change number after "=" and save script to change settings //
  20. //reload page for settings to take effect //
  21. //
  22. var number_of_links = 5; //number of random links to display at a time (default is 5) //
  23. //
  24. var link_space = 60; //space between links in pixels (default is 60) //
  25. //
  26. var truncate_length = 0; //number of characters to allow for each link's text - set to 0 for no truncation //
  27. //
  28. var show_on_start = 0; //show links on load - 0 to hide until toggled, 1 to show automatically //
  29. //
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //////////// End Settings ////////////////////////////////////////////////////////////////////////////////////////
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33.  
  34.  
  35. GM_addStyle(' \
  36. .randBtn{ \
  37. width: 90px; \
  38. display: inline-block; \
  39. text-align: center; \
  40. margin-right: 10px; \
  41. background: #ECF0F1 none repeat scroll 0% 0%; \
  42. padding: 0px 5px; \
  43. border-radius: 4px; \
  44. font-size: 85%; \
  45. cursor: pointer \
  46. } \
  47. .randBtn:hover{ \
  48. background: #E1E6E7 none repeat scroll 0% 0%; \
  49. color: #101112; \
  50. } \
  51. .buttonDiv{ \
  52. -moz-user-select: -moz-none; \
  53. -khtml-user-select: none; \
  54. -webkit-user-select: none; \
  55. color: #7F8C8D; \
  56. height: 46px; \
  57. width: 350px; \
  58. position: absolute; \
  59. margin-top: -32px; \
  60. padding-left: 23px; \
  61. transition-property: margin-top; \
  62. transition-duration: .5s; \
  63. } \
  64. .linkDiv{ \
  65. position: absolute; \
  66. width: 100%; \
  67. height: 32px; \
  68. border-bottom: solid #ECF0F1 1px; \
  69. visibility: hidden; \
  70. display: block; \
  71. } \
  72. }');
  73.  
  74.  
  75. /*setup elements*/
  76. var linkDiv = document.createElement('div');
  77. linkDiv.style.zIndex = '1';
  78. $(linkDiv).addClass('linkDiv');
  79. document.body.insertBefore(linkDiv, document.body.firstChild);
  80.  
  81. var buttonDiv = document.createElement('div');
  82. buttonDiv.style.zIndex = '2';
  83. $(buttonDiv).addClass('buttonDiv');
  84. document.body.insertBefore(buttonDiv, document.body.firstChild);
  85.  
  86. var infoDiv = document.createElement('div');
  87. $(infoDiv).addClass('infoDiv');
  88. infoDiv.style.zIndex = '1031';
  89. $(infoDiv).css('position', 'fixed').css('margin-top', '-24px').css('left', '84px').css('color', '#7F8C8D');
  90. document.body.insertBefore(infoDiv, document.body.firstChild);
  91.  
  92. var start = document.createElement('a');
  93. $(start).addClass('randBtn');
  94. start.innerHTML = 'Generate';
  95.  
  96. var randomPage = document.createElement('a');
  97. $(randomPage).addClass('randBtn');
  98. randomPage.innerHTML = 'Surprise Me!';
  99. var newLinks = document.createElement('a');
  100. $(newLinks).addClass('randBtn');
  101. newLinks.innerHTML = 'Randomize';
  102.  
  103. var hide = document.createElement('a');
  104. $(hide).addClass('randBtn');
  105. hide.innerHTML = 'Toggle';
  106.  
  107.  
  108. String.prototype.trunc = String.prototype.trunc ||
  109. function (n) {
  110. if (truncate_length > 0) {
  111. return this.length > n ? this.substr(0, n - 1) + '…' : this;
  112. }
  113. else {
  114. return this;
  115. }
  116. };
  117.  
  118.  
  119. function divIndexOf(div, link) {
  120. for (var i = 1; i < div.childNodes.length; i++) {
  121. if (div.childNodes[i].href == link) {
  122. return i;
  123. }
  124. }
  125. return -1;
  126. }
  127.  
  128.  
  129. //check for presence of login link
  130. function checkLogin(){
  131. var check = document.getElementsByClassName('header-login');
  132. if (typeof check[0] == 'undefined'){
  133. return 1;
  134. }
  135. else {
  136. return 0;
  137. }
  138. }
  139.  
  140.  
  141. var docList, randomNum;
  142. var list = [];
  143. list[0] = [];
  144. list[1] = [];
  145.  
  146. //Check for topics list on page
  147. function initial(){
  148. if (window.location == 'https://empeopled.com/'){
  149. if (checkLogin() == 1){
  150. infoDiv.innerHTML = 'Waiting for topics list...';
  151. docList = document.getElementsByTagName('select') [0];
  152. if (typeof docList == 'undefined' || docList.length < 4){
  153. setTimeout(initial, 300);
  154. }
  155. else {
  156. var topicName;
  157. //populate array with topics
  158. for (i = 3; i < docList.length + 2; i++) {
  159. topicName = docList.childNodes[i].innerHTML;
  160. topicName = topicName.replace(/&amp;/g, '&');
  161. list[0][i - 3] = topicName.trunc(truncate_length);
  162. list[1][i - 3] = '/t/' + topicName.replace(/ /g, '_').toLowerCase();
  163. }
  164. infoDiv.innerHTML = list[0].length + ' Topics found';
  165. buttonDiv.innerHTML = '';
  166. buttonDiv.appendChild(hide);
  167. buttonDiv.appendChild(randomPage);
  168. buttonDiv.appendChild(newLinks);
  169. $(infoDiv).css('padding', '0');
  170. GM_addStyle('.buttonDiv:hover{margin-top: 2px;}');
  171. if (show_on_start > 0){
  172. linkDiv.style.visibility = 'visible';
  173. }
  174. generate();
  175. }
  176. }
  177. else{
  178. infoDiv.innerHTML = 'Login to use random topics bar';
  179. setTimeout(function(){
  180. infoDiv.style.display = 'none';
  181. }, 5000);
  182. }
  183. }
  184. else {
  185. infoDiv.innerHTML = 'No topics found';
  186. buttonDiv.innerHTML = 'Return to frontpage to generate random topics';
  187. setTimeout(function(){
  188. buttonDiv.innerHTML = '';
  189. buttonDiv.appendChild(start);
  190. }, 5000);
  191. GM_addStyle('.buttonDiv:hover{margin-top: 2px;}');
  192. }
  193. }
  194.  
  195.  
  196. //Link generator function
  197. function generate(){
  198. linkDiv.innerHTML = '';
  199. var windowWidth, pad;
  200. var linkWidth = 0;
  201. var x = 0;
  202. //pick random topics to display
  203. while (x < number_of_links) {
  204. randomNum = Math.floor(Math.random() * list[0].length);
  205. if (linkDiv.childNodes.length < 1 || divIndexOf(linkDiv, list[1][randomNum]) < 0) {
  206. var randomLink = document.createElement('a');
  207. $(randomLink).css('margin', '0 ' + link_space + 'px 0 0');
  208. randomLink.innerHTML = list[0][randomNum];
  209. randomLink.href = list[1][randomNum];
  210. linkDiv.appendChild(randomLink);
  211. linkWidth += ($(linkDiv.childNodes[x]).width());
  212. x++;
  213. }
  214. }
  215. windowWidth = $(window).width();
  216. pad = ((windowWidth / 2) - Math.floor((linkWidth + (link_space * (number_of_links - 1))) / 2)) - 21;
  217. $(linkDiv).css('padding-left', pad + 'px');
  218. }
  219.  
  220. setTimeout(initial, 1500);
  221.  
  222. //Randomize Button
  223. newLinks.addEventListener('click', generate);
  224.  
  225. //Surprise Me! button
  226. randomPage.addEventListener('click', function () {
  227. window.location = list[1][Math.floor((Math.random() * list[0].length))].replace('&amp;', '&');
  228. });
  229.  
  230. //Toggle button
  231. hide.addEventListener('click', function () {
  232. linkDiv.style.position = 'relative';
  233. if (linkDiv.style.visibility == 'visible'){
  234. linkDiv.style.visibility = 'hidden';
  235. }
  236. else {
  237. linkDiv.style.visibility = 'visible';
  238. }
  239. });
  240.  
  241. //Generate button
  242. start.addEventListener('click', initial);