GA quick edit & auto-hide

Быстрая установка АУ/АС и быстрое редактирование сообщений на форуме ГА.

  1. // ==UserScript==
  2. // @name GA quick edit & auto-hide
  3. // @namespace glav.su
  4. // @description Быстрая установка АУ/АС и быстрое редактирование сообщений на форуме ГА.
  5. // @include http://glav.su/*
  6. // @include https://glav.su/*
  7. // @version 1.1
  8. // @grant none
  9. // ==/UserScript==
  10. function addStyle(styleText) {
  11. var style = document.createElement('style');
  12. if (document.head) {
  13. document.head.appendChild(style);
  14. style.innerHTML = styleText;
  15. }
  16. }
  17. addStyle([
  18. '.qe_popup {',
  19. 'cursor: pointer;',
  20. 'display: none;',
  21. 'border: 1px solid #aaa;',
  22. 'padding: 5px 2px;',
  23. 'white-space: nowrap;',
  24. 'border-radius: 5px;',
  25. 'position: absolute;',
  26. 'box-shadow: 3px 3px 10px #444;',
  27. 'background-color: #fff;',
  28. '}',
  29. '.qe_option {',
  30. 'display: block;',
  31. 'text-decoration: none;',
  32. 'color: #000;',
  33. 'background-color: #fff;',
  34. 'padding: 2px 12px;',
  35. '}',
  36. '.qe_option:hover {',
  37. 'text-decoration: none;',
  38. 'color: #fff;',
  39. 'background-color: #369;',
  40. '}',
  41. '.qe_edit {',
  42. 'display: block;',
  43. 'color: #000;',
  44. 'background-color: #fff;',
  45. 'padding: 0px;',
  46. 'position: absolute;',
  47. '}',
  48. '.qe_edit_content {',
  49. 'margin: 2px 10px;',
  50. 'resize: none;',
  51. 'border: 1px solid #aaf;',
  52. '}',
  53. '.qe_quote {',
  54. 'display: block;',
  55. 'color: #000;',
  56. 'background-color: #fff;',
  57. 'padding: 0px;',
  58. 'position: absolute;',
  59. '}',
  60. '.qe_quote_content {',
  61. 'margin: 2px 10px;',
  62. 'resize: none;',
  63. 'border: 1px solid #aaf;',
  64. '}',
  65. '.icoLoading {',
  66. 'background-image: url(/files/messages/e2c6fce4a064ddfa31b8b49380206c88.gif);',
  67. '}',
  68. '.icoQuickAUAS {',
  69. 'background-image: url(/files/messages/2175f0127d70a9ed87c6f78a9b44ce98.gif);',
  70. '}',
  71. '.icoQuickEdit {',
  72. 'background-image: url(/files/messages/62084ab537f806e01833d9a9d417384b.png);',
  73. '}',
  74. '.icoQuote {',
  75. 'background-image: url(/files/messages/e482fdf83cd8dddd6712c23e9cc26e5a.png);',
  76. '}',
  77. ].join(''));
  78. var $sandbox = ((s=$("#sandbox")).length == 1)?
  79. s : $('<div id="sandbox" style="display: none;">').appendTo( $(document.body) );//песочница
  80. var $auas_box = $('<div id="auas_box" class="qe_popup">')
  81. .mouseenter(function(){ $auas_box.under_mouse = true; })
  82. .mouseleave(function(){ $auas_box.under_mouse = false; });
  83. $auas_box.under_mouse = false;
  84. var $edit_box = $('<div id="edit_box" class="qe_edit">')
  85. .append($('<div style="heigth: 24px; padding: 3px 15px; font-size: 14px;">')
  86. .append($('<a id="qe_save" class="cBlueButton" href="javascript: void(0);">Сохранить</a>')
  87. .click(function(){
  88. var button = $("#forumMessagesListMessage" + $edit_box.m_id + "QuickEditButton")[0];
  89. toggleElClass(button, ['icoQuickEdit', 'icoLoading'], 1);
  90. $(button).attr('state', "1");
  91. $edit_box.hide();
  92. $edit_box.post_data['content'] = $("#edit_box_content").val();
  93. $.ajax({
  94. method: 'POST',
  95. url: $edit_box.edit_url,
  96. data: $edit_box.post_data,
  97. dataType: 'html',
  98. success: function(res){
  99. toggleElClass(button, ['icoQuickEdit', 'icoLoading'], 0);
  100. $(button).attr('state', "0");
  101. $edit_box.detach();
  102. var mess = res.replace(mess_rg($edit_box.m_id), '$1');
  103. $('#forumMessagesListMessage'+$edit_box.m_id+'Content').html(mess);
  104. }
  105. })
  106. })
  107. )
  108. .append($('<a id="qe_cancel" class="cBlueButton" style="margin-left: 2px;" href="javascript: void(0);">Отменить</a>')
  109. .click(function(){
  110. var button = $("#forumMessagesListMessage" + $edit_box.m_id + "QuickEditButton")[0];
  111. toggleElClass(button, ['icoQuickEdit', 'icoLoading'], 0);
  112. $edit_box.hide();
  113. $(button).attr('state', "0");
  114. })
  115. )
  116. )
  117. .append('<textarea id="edit_box_content" class="qe_edit_content">');
  118. var $quote_box = $('<div id="quote_box" class="qe_quote">')
  119. .append($('<div style="heigth: 24px; padding: 3px 15px; font-size: 14px;">')
  120. .append($('<a id="qu_close" class="cBlueButton" style="margin-left: 2px;" href="javascript: void(0);">Закрыть</a>')
  121. .click(function(){
  122. var button = $("#forumMessagesListMessage" + $quote_box.m_id + "QuickQuoteButton")[0];
  123. toggleElClass(button, ['icoQuote', 'icoLoading'], 0);
  124. $quote_box.hide();
  125. $(button).attr('state', "0");
  126. })
  127. )
  128. )
  129. .append('<textarea id="quote_box_content" class="qe_quote_content">');
  130. $(document.body).click(function(){
  131. if(!$auas_box.under_mouse){
  132. $auas_box.hide().closest("td").find("a.cBlueButton").attr('state', "0");
  133. }
  134. });
  135.  
  136. function toggleElClass(el, clss, val){
  137. val = +val;
  138. $(el).removeClass(clss[+(!val)]).addClass(clss[val]);
  139. }
  140.  
  141. function mess_rg(m_id){
  142. return new RegExp('[\\s\\S]*<td id="forumMessagesListMessage'+m_id+'Content" class="fItem forumMessagesListMessageContent">([\\s\\S]+?)<\/td>[\n\r\\s\\t]*<\/tr>[\n\r\\s\\t]*<\/tbody>[\n\r\\s\\t]*<\/table>[\n\r\\s\\t]*<table class="f" width="100%">[\\s\\S]*', 'i');
  143. }
  144.  
  145. $(".forumMessagesListMessageArchiveButton").each(function(bidx, b_archive){
  146. var m_id = $(b_archive).attr("id").replace(/forumMessagesListMessage(\d+)ArchiveButton/, '$1');
  147. if( $(b_archive).closest("tr").find(".forumMessagesListMessageEditButton").length === 1 ){
  148. var b_edit = $(b_archive).closest("tr").find(".forumMessagesListMessageEditButton")[0],
  149. edit_url = $(b_edit).find("a").attr("href");
  150. $(b_edit).closest("tr")
  151. .append($('<td class="fItem">')
  152. .append($('<a href="javascript: void(0);">&nbsp;</a>')
  153. .attr('id', "forumMessagesListMessage" + m_id + "AutoHideButton")
  154. .attr('title', "Изменить АУ/АС")
  155. .attr('class', "cBlueButton icoButton icoQuickAUAS")
  156. .attr('state', "0")
  157. .click(function(){
  158. if( $(this).attr('state') == '0' ){
  159. if( $auas_box.is(":visible") ){
  160. var d_button = $auas_box.hide().closest("td").find("a.cBlueButton").attr('state', "0");
  161. toggleElClass(d_button, ['icoQuickAUAS', 'icoLoading'], 0);
  162. }
  163. toggleElClass(this, ['icoQuickAUAS', 'icoLoading'], 1);
  164. $(this).attr('state', "1");
  165. var _this = this;
  166. $.ajax({
  167. url: edit_url,
  168. dataType: 'html',
  169. success: function(data){
  170. if( $(_this).attr('state') !== '1' ) return true;
  171. var rg = /[\s\S]*(<form[\s\S]*?<\/form>)[\s\S]*/i;
  172. $sandbox.empty().html( data.toString().replace(rg, '$1') );
  173. $auas_box.empty().detach();
  174. var form_data = {};
  175. $("#sandbox").find(":input").each(function(ix, inp){
  176. if(typeof (key = $(inp).attr("name")) !== 'undefined'){
  177. form_data[key] = $(inp).val();
  178. }
  179. })
  180. var hide_opts;
  181. if(typeof form_data['isHidden'] !== 'undefined'){
  182. hide_opts = [{val: 0}, {val: 0}];
  183. delete form_data['isHidden'];
  184. }else if(typeof form_data['autoHide'] !== 'undefined'){
  185. hide_opts = $("#sandbox").find("select[name=autoHide] option");
  186. }
  187. $.each(hide_opts, function(ix, op){
  188. if(ix == 0) return true;
  189. var ah_v = (typeof op.val !== 'undefined')? op.val : $(op).val();
  190. var caption = (ah_v == 0)?
  191. 'Не скрывать' :
  192. ['Автоскрытие', 'Автоудаление'][+(ah_v<0)]+' через '+Math.abs(ah_v)+' ч.';
  193. $('<a class="qe_option" href="javascript: void(0);">'+caption+'</a>')
  194. .click(function(){
  195. toggleElClass(_this, ['icoQuickAUAS', 'icoLoading'], 1);
  196. $(_this).attr('state', "1");
  197. $auas_box.hide();
  198. var post_data = $.extend({}, form_data);
  199. post_data['autoHide'] = ah_v;
  200. $.ajax({
  201. method: 'POST',
  202. url: edit_url,
  203. data: post_data,
  204. dataType: 'html',
  205. success: function(res){
  206. toggleElClass(_this, ['icoQuickAUAS', 'icoLoading'], 0);
  207. $(_this).attr('state', "0");
  208. var mess = res.replace(mess_rg(m_id), '$1');
  209. $('#forumMessagesListMessage'+m_id+'Content').html(mess);
  210. }
  211. });
  212. })
  213. .appendTo($auas_box);
  214. });
  215. toggleElClass(_this, ['icoQuickAUAS', 'icoLoading'], 0);
  216. $(_this).attr('state', "2");
  217. $auas_box.appendTo($td=$(_this).closest("td"))
  218. .css({top: $td.offset().top + $td.outerHeight(),
  219. left: $td.offset().left - $auas_box.outerWidth() + $td.outerWidth()})
  220. .slideDown(100);
  221. }
  222. });
  223. }else if( $(this).attr('state') == '2' ){
  224. $auas_box.hide();
  225. $(this).attr('state', "0");
  226. }
  227. })
  228. .mouseenter(function(){ $auas_box.under_mouse = true; })
  229. .mouseleave(function(){ $auas_box.under_mouse = false; })
  230. )
  231. )
  232. .append($('<td class="fItem">')
  233. .append($('<a href="javascript: void(0);">&nbsp;</a>')
  234. .attr('id', "forumMessagesListMessage" + m_id + "QuickEditButton")
  235. .attr('title', "Быстрое редактирование")
  236. .attr('class', "cBlueButton icoButton icoQuickEdit")
  237. .attr('state', "0")
  238. .click(function(){
  239. if( $(this).attr('state') == '0' ){
  240. if( $edit_box.is(":visible") ){
  241. var button = $("#forumMessagesListMessage" + $edit_box.m_id + "QuickEditButton")[0];
  242. toggleElClass(button, ['icoQuickEdit', 'icoLoading'], 0);
  243. $edit_box.hide();
  244. $(button).attr('state', "0");
  245. }
  246. toggleElClass(this, ['icoQuickEdit', 'icoLoading'], 1);
  247. $(this).attr('state', "1");
  248. var _this = this;
  249. $.ajax({
  250. url: edit_url,
  251. dataType: 'html',
  252. success: function(data){
  253. if( $(_this).attr('state') !== '1' ) return true;
  254. var rg = /[\s\S]*(<form[\s\S]*?<\/form>)[\s\S]*/i;
  255. $sandbox.empty().html( data.toString().replace(rg, '$1') );
  256. var form_data = {};
  257. $("#sandbox").find(":input").each(function(ix, inp){
  258. if(typeof (key = $(inp).attr("name")) !== 'undefined'){
  259. form_data[key] = $(inp).val();
  260. }
  261. })
  262. toggleElClass(_this, ['icoQuickEdit', 'icoLoading'], 0);
  263. $(_this).attr('state', "2");
  264. $edit_box.detach().appendTo($td=$('#forumMessagesListMessage'+m_id+'Content'))
  265. .css({top: $td.offset().top, left: $td.offset().left,
  266. width: $td.width(), height: $td.height()});
  267. $edit_box.m_id = m_id;
  268. $edit_box.edit_url = edit_url;
  269. $edit_box.post_data = $.extend({}, form_data);
  270. $("#edit_box_content")
  271. .css({height: $edit_box.height() - 44, width: $edit_box.width() - 20})
  272. .val($edit_box.post_data['content']);
  273. $edit_box.show();
  274. }
  275. });
  276. }else if( $(this).attr('state') == '2' ){
  277. $edit_box.hide();
  278. $(this).attr('state', "0");
  279. }
  280. })
  281. )
  282. );
  283. }
  284. var reply_url = $(b_archive).closest("td").prev("td").find("a").attr("href");
  285. $(b_archive).closest("tr")
  286. .append($('<td class="fItem">')
  287. .append($('<a href="javascript: void(0);">&nbsp;</a>')
  288. .attr('id', "forumMessagesListMessage" + m_id + "QuickQuoteButton")
  289. .attr('title', "Цитировать пост")
  290. .attr('class', "cBlueButton icoButton icoQuote")
  291. .attr('state', "0")
  292. .click(function(){
  293. if( $(this).attr('state') == '0' ){
  294. if( $quote_box.is(":visible") ){
  295. var button = $("#forumMessagesListMessage" + $quote_box.m_id + "QuickQuoteButton")[0];
  296. toggleElClass(button, ['icoQuote', 'icoLoading'], 0);
  297. $quote_box.hide();
  298. $(button).attr('state', "0");
  299. }
  300. toggleElClass(this, ['icoQuote', 'icoLoading'], 1);
  301. $(this).attr('state', "1");
  302. var _this = this;
  303. $.ajax({
  304. url: reply_url,
  305. dataType: 'html',
  306. success: function(data){
  307. if( $(_this).attr('state') !== '1' ) return true;
  308. var rg = /[\s\S]*(<form[\s\S]*?<\/form>)[\s\S]*/i;
  309. $sandbox.empty().html( data.toString().replace(rg, '$1') );
  310. var form_data = {};
  311. $("#sandbox").find(":input").each(function(ix, inp){
  312. if(typeof (key = $(inp).attr("name")) !== 'undefined'){
  313. form_data[key] = $(inp).val();
  314. }
  315. })
  316. toggleElClass(_this, ['icoQuote', 'icoLoading'], 0);
  317. $(_this).attr('state', "2");
  318. $quote_box.detach().appendTo($td=$('#forumMessagesListMessage'+m_id+'Content'))
  319. .css({top: $td.offset().top, left: $td.offset().left,
  320. width: $td.width(), height: $td.height()});
  321. $quote_box.m_id = m_id;
  322. $quote_box.reply_url = reply_url;
  323. $quote_box.post_data = $.extend({}, form_data);
  324. $("#quote_box_content")
  325. .css({height: $quote_box.height() - 44, width: $quote_box.width() - 20})
  326. .val($quote_box.post_data['content']);
  327. $quote_box.show();
  328. }
  329. });
  330. }else if( $(this).attr('state') == '2' ){
  331. $quote_box.hide();
  332. $(this).attr('state', "0");
  333. }
  334. })
  335. )
  336. );
  337. });