1. // ==UserScript==
  2. // @name Uzdarbis Topic Bump
  3. // @namespace Uzdarbis
  4. // @include https://uzdarbis.lt/*
  5. // @exclude https://uzdarbis.lt/top
  6. // @exclude https://uzdarbis.lt/ips.topics.php
  7. // @version 0.1.6
  8. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
  9. // @grant GM_xmlhttpRequest
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // ==/UserScript==
  13. //0.1.6
  14. //Pop-up alert langas, bandant pakelti temą panaikintas.
  15. //
  16. jQuery.noConflict();
  17. var myTopics_str = GM_getValue("UTB_myTopics","t207796,t280802");
  18. var cachedTime = GM_getValue("UTB_cachedTime", "0");
  19. var myTopics = myTopics_str.split(",");
  20. var totalCount = myTopics.length;
  21. var parsedCount = 0;
  22. function insertVisuals() {
  23. jQuery("<div id='UTB_menu_hider'>UTB nustatymai</div>").appendTo("#ipboard_body")
  24. .css({
  25. "position": "absolute",
  26. "top": "0",
  27. "right": "0",
  28. "z-index": "2000",
  29. "width": "150px",
  30. "height": "20px",
  31. "background-color": "black",
  32. "color":"white",
  33. "text-align":"center",
  34. "font-family":"Calibri",
  35. });
  36. jQuery("<div id='UTB_info'></div>").appendTo("#ipboard_body")
  37. .css({
  38. "position": "absolute",
  39. "top": "0",
  40. "right": "150px",
  41. "width": "200px",
  42. "height": "20px",
  43. "color":"white",
  44. "background-color": "#2E9F9F",
  45. "text-align":"center",
  46. "font-size":"10px",
  47. "font-family":"Calibri",
  48. "line-height": "18px",
  49. });
  50. jQuery("#UTB_menu_hider").css("cursor","pointer");
  51. jQuery("<div id='UTB_menu'></div>").appendTo("#ipboard_body")
  52. .css({
  53. "position": "absolute",
  54. "top": "100px",
  55. "right": "0px",
  56. "padding": "5px 15px",
  57. "background-color": "rgba(255,255,255,0.5)",
  58. "-webkit-border-bottom-left-radius": "5px",
  59. "-moz-border-radius-bottomleft": "5px",
  60. "border-bottom-left-radius": "5px",
  61. "z-index": "5000",
  62. "font-family":"Calibri",
  63. });
  64. jQuery("<div id='UTB_settings'></div>").appendTo("#ipboard_body")
  65. .css({
  66. "display": "none",
  67. "position": "absolute",
  68. "top": "20px",
  69. "right": "0px",
  70. "padding": "5px 15px",
  71. "background-color": "rgba(255,255,255,0.5)",
  72. "-webkit-border-bottom-left-radius": "5px",
  73. "-moz-border-radius-bottomleft": "5px",
  74. "border-bottom-left-radius": "5px",
  75. "z-index": "2000",
  76. "font-family":"Calibri",
  77. "text-align": "center",
  78. });
  79. }
  80. function fillSettings() {
  81. jQuery("#UTB_settings").append("T: <input type='text' style='font-size:11px;padding:4px;margin-bottom:2px;' id='UTB_settings_topic_list' value="+myTopics_str+"> <span title='Įveskite temos pavadinimą, atskirkite kableliais'>?</span><br/><button style='background-color:white; border:0;'>Atnaujinti sąrašą</button>");
  82. }
  83. function updateTopics() {
  84. myTopics_str = jQuery("#UTB_settings_topic_list").val();
  85. myTopics = myTopics_str.split(",");
  86. GM_setValue("UTB_myTopics",myTopics_str);
  87. reDraw();
  88. }
  89. function getMyTopics(topics) {
  90. jQuery.each(topics,function(index,value) {
  91. jQuery("<div id='UTB_"+value+"'>"+value+"</div>").appendTo("#UTB_menu");
  92. getSingleTopic(value);
  93. });
  94. }
  95. function setTopicInfo(Topic) {
  96. cutTopicName = Topic.Name.substr(0, 15) + "...";
  97. TopicContent = "<a style='text-decoration:none; display: inline-block; width:140px;' title='"+Topic.Name+"' target='_blank' href='http://uzdarbis.lt/"+Topic.Id+"/'>"+cutTopicName+"</a>"
  98. if(Topic.Error.length !== 0) {
  99. TopicContent += "<span title='"+Topic.Error+"'style='color:red;'>Err</span>";
  100. } else {
  101. if(Topic.Bumpable) {
  102. TopicContent += "<span class='bump_button' style='color:green;cursor:pointer;'>BUMP</span>";
  103. } else {
  104. TopicContent += "<span style='color:#AEB404; '>"+Topic.BumpTime+" min.</span>";
  105. }
  106. }
  107. jQuery("#UTB_"+Topic.Id).html(TopicContent);
  108. parsedCount++;
  109. }
  110. function getSingleTopic(name) {
  111. GM_xmlhttpRequest({
  112. method: "GET",
  113. url: "http://uzdarbis.lt/"+name+"/",
  114. dataType: "text",
  115. synchronous: false,
  116. onload: function(data) {
  117. var topicId = name;
  118. var topicName;
  119. var topicBumpable = false;
  120. var topicBumpTime = 0;
  121. var topicError = "";
  122. parsed = jQuery(data.responseText);
  123. topicName = jQuery(".firstpost_title h2 strong", parsed).html();
  124. Bumpable = jQuery(".topic_controls .disabled", parsed);
  125. if(Bumpable.length) {
  126. topicBumpable = false;
  127. topicBumpTime = Bumpable.attr("title").split(" ")[4];
  128. } else {
  129. searchForButton = jQuery(".topic_buttons", parsed);
  130. if(searchForButton.html().indexOf('Pakelti ')==-1) {
  131. topicError = "Temos pakelti negalima";
  132. } else {
  133. topicBumpable = true;
  134. }
  135. }
  136. Topic = {"Id": topicId,"Name":topicName,"Bumpable":topicBumpable,"BumpTime":topicBumpTime,"Error":topicError};
  137. setTopicInfo(Topic);
  138. }
  139. });
  140. }
  141. function updateInfo(info) {
  142. jQuery("#UTB_info").html(info);
  143. }
  144. function checkCache() {
  145. if(cachedTime != "0") {
  146. //cached_Time_obj = new Date(cachedTime.slice(0,-6).slice(1,cachedTime.length).replace(/-/g,'/').replace(/T/,' ') +" +0000");
  147. time_diff = Math.abs(new Date() - new Date(cachedTime))/60000;
  148. if(time_diff > 5) {
  149. GM_setValue("UTB_cachedTime", new Date()+"");
  150. return 1;
  151. } else {
  152. return 0;
  153. }
  154. }
  155. GM_setValue("UTB_cachedTime", new Date()+"");
  156. return 1;
  157. }
  158. function saveToCache() {
  159. ccc = jQuery("#UTB_menu").html();
  160. GM_setValue("UTB_cachedContent", ccc);
  161. updateInfo("Cache turinys atnaujintas");
  162. }
  163. function loadFromCache() {
  164. ccc = GM_getValue("UTB_cachedContent");
  165. jQuery("#UTB_menu").html(ccc);
  166. updateInfo("Cache užkrautas");
  167. }
  168. function getSyncedAndCache() {
  169. if(totalCount!=parsedCount) {
  170. setTimeout(function() {
  171. getSyncedAndCache();
  172. } , 500);
  173. } else {
  174. saveToCache();
  175. }
  176. return
  177. }
  178. function bumpThread(id) {
  179. link = "http://uzdarbis.lt/"+id+"/page__bumpTopic__1";
  180. GM_xmlhttpRequest({
  181. method: "GET",
  182. url: link,
  183. dataType: "text",
  184. synchronous: false,
  185. onload: function(data) {
  186. reDraw();
  187. }
  188. });
  189. }
  190. function reDraw() {
  191. jQuery("#UTB_menu").remove();
  192. jQuery("#UTB_menu_hider").remove();
  193. jQuery("#UTB_info").remove();
  194. jQuery("#UTB_settings").remove();
  195. insertVisuals();
  196. fillSettings();
  197. parsedCount = 0;
  198. getMyTopics(myTopics);
  199. getSyncedAndCache();
  200. updateInfo("Informacija atnaujinta");
  201. }
  202. jQuery(document).ready(function() {
  203. insertVisuals();
  204. fillSettings();
  205. //Setting up menu actions
  206. jQuery("body").on('click',"#UTB_menu_hider",function(){jQuery("#UTB_settings").toggle();});
  207. jQuery("body").on('click',"#UTB_settings button",function(){updateTopics()});
  208. jQuery("body").on('click','.bump_button',function() {
  209. r_id = jQuery(this).parent().attr("id")
  210. r_id = r_id.slice(4,r_id.length);
  211. bumpThread(r_id);
  212. });
  213. if(checkCache()) {
  214. getMyTopics(myTopics);
  215. getSyncedAndCache();
  216. } else {
  217. loadFromCache();
  218. }
  219. });