Advertisement
Guest User

Eddy_Em

a guest
Oct 2nd, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name LORCode Tools
  3. // @description Кнопка цитирования выделенного и панель тегов для LORCode
  4. // @author Алексей Соловьев aka moscwich
  5. // @license Creative Commons Attribution 3.0 Unported
  6. // @version 0.21.1
  7. // @namespace http://www.linux.org.ru/*
  8. // @namespace https://www.linux.org.ru/*
  9. // @include http://www.linux.org.ru/*
  10. // @include https://www.linux.org.ru/*
  11. // ==/UserScript==
  12.  
  13. // Based on MultiCodePanel 2.2 (v. 0.22)
  14. // http://al-moscwich.tk/tag/multicodepanel
  15.  
  16. // if (/https?:\/\/(www\.)?linux.org.ru/.test(u))
  17.  
  18. //alert("TID="+TopicID);
  19. function memories_add(event, id, w) {
  20.     event.preventDefault();
  21.     var target = document.getElementById(id);
  22.     var evt = document.createEvent("MouseEvents");
  23.     evt.initEvent("click", true, true);
  24.     target.dispatchEvent(evt);
  25.     memories_form_setup(w,0);
  26.     //setTimeout(initForms, 2000);
  27. }
  28. var memcntr = Number(document.getElementById("memories_count").innerHTML);
  29. var favcntr = Number(document.getElementById("favs_count").innerHTML);
  30. function memories_remove(event, id, w){
  31.     event.preventDefault();
  32.     var target = document.getElementById(id);
  33.     var evt = document.createEvent("MouseEvents");
  34.     evt.initEvent("click", true, true);
  35.     target.dispatchEvent(evt);
  36.     memories_form_setup(w,0);
  37.     //setTimeout(initForms, 2000);
  38. }
  39.  
  40. function memories_form_setup(watch, clear) {
  41.     var el, Id, ParentId, text;
  42.     var memcntr = Number(document.getElementById("memories_count").innerHTML);
  43.     var favcntr = Number(document.getElementById("favs_count").innerHTML);
  44.     if (watch){
  45.         Id = 'memories0_button';
  46.         ParentId = 'memories_button';
  47.     }else{
  48.         Id = 'favs0_button';
  49.         ParentId = 'favs_button';
  50.     }
  51.     el = document.getElementById(Id);
  52.     if(typeof(clear) != "undefined"){
  53.         text = "Подождите, пожалуйста";
  54.     }else{
  55.         parCls = document.getElementById(ParentId).className;
  56.         if (parCls=="") {
  57.             text = watch?"Отслеживать ("+memcntr+")":"В избранное ("+favcntr+")";
  58.             el.onclick = function(evt){ memories_add(evt, ParentId,watch);};
  59.         } else {
  60.             text = watch?"Не отслеживать ("+memcntr+")":"Удалить из избранного ("+favcntr+")";
  61.             el.onclick = function(evt){ memories_remove(evt, ParentId,watch);};
  62.         }
  63.     }
  64.     el.title = text;
  65.     el.innerHTML = text;
  66. }
  67.  
  68. var TM = document.getElementById("topicMenu");
  69. var favs = document.createElement("li");
  70. var mems = document.createElement("li");
  71. favs.innerHTML = "<a id='favs0_button' href='#'></a>";
  72. mems.innerHTML = "<a id='memories0_button' href='#'></a>";
  73. TM.appendChild(favs);
  74. TM.appendChild(mems);
  75.  
  76. var Itimeout = setInterval(initForms, 300);
  77. function initForms(){
  78.     clearInterval(Itimeout);
  79.     memories_form_setup(false);
  80.     memories_form_setup(true);
  81.     Itimeout = setInterval(initForms, 3000);
  82. }
  83.  
  84. var NonShown = document.getElementsByClassName("fav-buttons");
  85. for(var i=0,j=NonShown.length; i<j; i++) NonShown[i].style.display = "none";
  86.  
  87. function removeElements () {
  88.     for (i = 0; i < arguments.length; i++) {
  89.         var p = arguments[i].parentNode;
  90.         if (p) p.removeChild (arguments[i]);
  91.     }
  92. }
  93. function set (p, z) {
  94.     for (i = 0; i < arguments.length && (arguments[i] === undefined); i++) {}
  95.     return arguments[i];
  96. }
  97.  
  98. i = j = undefined;
  99. a = b = undefined;
  100.  
  101. form = document.getElementById ("commentForm") || document.getElementById ("messageForm") || document.getElementById ("changeForm").getElementsByTagName ("label")[7];
  102. msg = document.getElementById ("msg") || document.getElementById ("form_msg") || document.getElementById ("info");
  103. var u = window.location.href;
  104.  
  105. // Panel
  106. var panel = document.createElement ("div");
  107. panel.id = 'atag';
  108. panel.createBlock =
  109.     function () {
  110.         block = document.createElement ("span");
  111.         for (i = 0; i < arguments.length; i++) {
  112.             link = document.createElement ("a");
  113.             link.textContent = arguments[i][0];
  114.             link.title = arguments[i][1];
  115.             link.exec = arguments[i][2];
  116.             link.onclick = function () {
  117.                 eval (this.exec);
  118.                 return false;
  119.             }
  120.             block.appendChild (link);
  121.         }
  122.         return this.appendChild (block);
  123.     }
  124. panel.createBlock (
  125.     ["[b]", "Полужирный", 'intag ("b");'],
  126.     ["[i]", "Курсив", 'intag ("i");'],
  127.     ["[s]", "Зачеркнутый", 'intag ("s");'],
  128.     ["[u]", "Подчеркнутый", 'intag ("u");']
  129. );
  130. panel.createBlock (
  131.     ["[quote]", "Цитата", 'intag ("quote"); msg.parentNode.mode.selectedIndex = 0;'],
  132.     ["[code]", "Код", 'intag ("code"); msg.parentNode.mode.selectedIndex = 0;']
  133. );
  134. panel.createBlock (
  135.     ["[url]", "URL", 'url ();'],
  136.     ["[user]", "Участник", 'intag ("user");']
  137. );
  138. panel.createBlock (
  139.     ["[list]", "Список", 'intag ("list");'],
  140.     ["[*]", "Элемент списка", 'msg.wrtSel ("[*]", "");']
  141. );
  142. panel.createBlock (
  143.     ["«»", "Кавычки", 'msg.wrtSel ("«", "»");'],
  144.     ["„“", "Кавычки", 'msg.wrtSel ("„", "“");'],
  145.     ["[br]", "Перевод строки", 'msg.wrtSel ("[br]", "");']
  146. );
  147. panel.createBlock (
  148.     [" fix ", "Превратить знаки и обозначения в соответствующие спец. символы", 'fix();'],
  149.     [" deltags-in ", "Удалить крайнее входящие обрамление тегами", 'deltagsin ();'],
  150.     [" brs ", "Добавить [br] к переводам строк", 'brs ();']
  151. );
  152.  
  153. msg.parentNode.insertBefore (panel, msg);
  154. msg.cols = 100;
  155. msg.rows = 20;
  156.  
  157. // Styles
  158. obj = document.createElement ("style");
  159. obj.innerHTML =
  160.     '#atag a {\
  161.         padding:2px 3px; margin:2px; cursor: pointer;\
  162.         text-decoration: none; color: #FFF !important;\
  163.         background-color:#004; border: #888 outset 1px;\
  164.     }\
  165.     #atag a:hover {background-color:#008; border-color:#888;}\
  166.     #atag {\
  167.         margin-top: 5px; margin-bottom: 5px;\
  168.         padding: 3px 1px; font-size: 0.9em;\
  169.     }\
  170.     #atag > span {margin-right: 4px;}\
  171.     label[for="msg"] {display: inline-block; margin-top: 5px;}\
  172.     #msg {width: 50em !important;}\
  173.     label[for="title"], label[for="form_mode"] {display: inline-block; margin: 5px 0 3px 0;\
  174.     .msg_body p {margin: 0.3em 0 !important;}\
  175.     .quote > p {margin: 0.5em 0 0.3em 0 !important;}';
  176. document.getElementsByTagName ("head")[0].appendChild (obj);
  177.  
  178. // Remove formating tips
  179. if (u.indexOf ("add.jsp") <= -1 &&
  180.         u.indexOf ("edit.jsp") <= -1 &&
  181.         u.indexOf ("register.jsp") <= -1)
  182.     removeElements (form.getElementsByTagName ('font')[0],
  183.         (i = form.getElementsByTagName ('br'))[5],
  184.         i[6]
  185.     );
  186.  
  187. // Add quote links
  188. var t = document.getElementsByClassName ("title");
  189. t.createQlink = function () {
  190.     for (i = 0; i < this.length; i++)
  191.         for (j in arguments) {
  192.             var clink = this[i].getElementsByTagName ("a")[0];
  193.             if (clink){
  194.                 var qlink = document.createElement ("a");
  195.                 qlink.onclick = arguments[j][1];
  196.                 qlink.href = u;
  197.                 qlink.textContent = arguments[j][0];
  198.                 var d = this[i].insertBefore (qlink, clink);
  199.                 this[i].insertBefore (clink, d);
  200.                 this[i].insertBefore (document.createTextNode ("] ["), d);
  201.             }
  202.         }
  203. }
  204. t.createQlink (['цитата', q], ['блок-цитата', qb], ['юзер', user]);
  205.  
  206. // Add \n to <br>
  207. var mbs = document.getElementsByClassName ("msg_body");
  208. for (j in mbs) if (!isNaN (j)) {
  209.     var mps = mbs[j].getElementsByTagName ("p");
  210.     for (i in mps)
  211.         if (!isNaN (i))
  212.             mps[i].innerHTML = mps[i].innerHTML.replace (/<br\/?>(?![\n\r])/g, "<br>\n");
  213. }
  214.  
  215.  
  216.     /*      Main        */
  217.  
  218. //  Auxiliary functions
  219. msg.wrtSel = function (subj, offset, before, after, zset) { //Also msg.wrtSel (before, after, offset)
  220.     if (typeof offset == "string")
  221.         var
  222.             after = offset, offset = before,
  223.             before = subj, subj = undefined;
  224.     var
  225.         before = before || "", after = after || "",
  226.         offset = set (offset, before.length), zset = zset || 0;
  227.     var
  228.         startSel = set (a, this.selectionStart), endSel = set (b, this.selectionEnd),
  229.         subj = before + set (subj, this.value.substring (startSel, endSel)) + after;
  230.  
  231.     this.value = this.value.substring (0, startSel) + subj + this.value.substring (endSel);
  232.     this.focus (); this.setSelectionRange (startSel + offset, endSel + offset + zset);
  233.     a = b = undefined;
  234. }
  235. function addbr (c) {
  236.     return c.replace (/^((?:(?!\[\/?(?:quote|code|list|br)(?:=.*)?\]$)[^\n\r])+)(\r?\n)(?!\n|\[\/?(?:br|quote(?:=.*)?|code(?:=.*)?)\])/gm, "$1[br]$2");
  237. }
  238. function getTextContent (post) {
  239.     var text = "";
  240.     var pTags = post.getElementsByClassName ("msg_body")[0].getElementsByTagName ("p");
  241.     for (i = 0; i < pTags.length; i++)
  242.         if (pTags[i].parentNode.className.indexOf ('msg_body') > -1) {
  243.             text += pTags[i].textContent;
  244.             if (i != pTags.length - 1) text += "\n\n";
  245.         }
  246.     return text;
  247. }
  248. function getUserName (post){
  249.     if (i = post.getElementsByClassName ("sign")[0].getElementsByTagName ("a")[0])
  250.         return i.innerHTML;
  251.     else return "anonymous";
  252. }
  253.  
  254. // Functions to run
  255. function intag (tag, arg) {
  256.     var arg = arg || "";
  257.     msg.wrtSel (
  258.         undefined,
  259.         tag.length + arg.length + 2,
  260.         "[" + tag + arg + "]",
  261.         "[/" + tag + "]"
  262.     );
  263. }
  264. function fix () {
  265.     var a = msg.selectionStart, b = msg.selectionEnd;
  266.     var repc = function (c) {
  267.         c = c.replace (/\(c\)/gi, "©");    c = c.replace (/\([rр]\)/gi, "®");
  268.         c = c.replace (/\(f\)/gi, "£");    c = c.replace (/\(e\)/gi, "€");
  269.         c = c.replace (/%\/10/g, "‰");    c = c.replace (/%\/100/g, "‱");
  270.         c = c.replace (/\(V\)/g, "✓");    c = c.replace (/\(V\+\)/g, "✔");
  271.         c = c.replace (/\(x\)/g, "✗");    c = c.replace (/\(x\+\)/g, "✘");
  272.         c = c.replace (/`/g, "&#769;"); c = c.replace (/\(p\)/gi, "§");
  273.         c = c.replace (/(^| )- /g, "$1— ");   c = c.replace (/\.\.\./g, "…");
  274.         c = c.replace (/\(\*\+?\)/g, "★");    c = c.replace (/\(\*-\)/g, "☆");
  275.         c = c.replace (/\([tт][mм]\)/gi, "™");
  276.         c = c.replace (/-->/g, "→");
  277.         return c;
  278.     }
  279.    
  280.     if (a != b) {
  281.         var c = msg.value.substring (a, b);
  282.         var z = repc (c);
  283.         msg.wrtSel (z, 0, "", "", z.length - c.length);
  284.     }
  285.     else
  286.         msg.value = repc (msg.value);
  287. }
  288. function url () {
  289.     a = msg.selectionStart; b = msg.selectionEnd;
  290.     z = msg.value.substring (a, b);
  291.     if (/((ftp|http|https):\/\/)[\.\w- ]{2,}\.[A-Za-z]{2,4}(\/?$|\/.*)/.test(z) || z.length == 0) {
  292.         msg.wrtSel (z, 5,
  293.             "[url]", "[/url]"
  294.         );
  295.     }
  296.     else if (/[\.\w- ]{2,}\.[A-Za-z]{2,4}(\/?$|\/.*)/.test(z)) {
  297.         msg.wrtSel (
  298.             "http://"+z, 5,
  299.             "[url]", "[/url]", 7
  300.         );
  301.     }
  302.     else {
  303.         msg.wrtSel (z, 5,
  304.             "[url=]", "[/url]",
  305.             -z.length
  306.         );
  307.     }
  308. }
  309. function deltagsin () {
  310.     z = msg.value.substring (a = msg.selectionStart, b = msg.selectionEnd);
  311.     c = z.replace (/\[\w+\](.*)\[\/\w+\]/, "$1");
  312.     msg.wrtSel (c, 0, "", "", - z.length + c.length);
  313. }
  314. function brs () {
  315.     var a = msg.selectionStart, b = msg.selectionEnd;
  316.     if (a != b) {
  317.         var c = msg.value.substring (a, b);
  318.         var z = addbr (c);
  319.         msg.wrtSel (z, 0, "", "", z.length - c.length);
  320.     }
  321.     else {
  322.         msg.value = addbr (msg.value);
  323.     }
  324. }
  325. function qb () {
  326.     var seltxt = getSelection ();
  327.     var getQuoteSrc =
  328.         function (sel, post, prnt) {
  329.             return (
  330.                 "[quote" + (
  331.                     prnt.parentNode.parentNode != msg.parentNode.parentNode.parentNode.parentNode.parentNode
  332.                     ? "=" + getUserName (post)
  333.                     : ""
  334.                 ) + "]"
  335.                 + sel.toString ().replace (
  336.                     /(?:>>-----Цитата---->>|^)(.*)<<-----Цитата----<</,
  337.                     function (str, p) {
  338.                         if (p!="") return "[quote]" + p + "[/quote]";
  339.                         else return "";
  340.                     })
  341.                 + "[/quote]");
  342.         }
  343.  
  344.     if (seltxt != "") {
  345.         var post = seltxt.getRangeAt (0).commonAncestorContainer;
  346.         while (post.className != "msg")
  347.             post = post.parentNode;
  348.         msg.wrtSel (i = addbr (getQuoteSrc (seltxt, post, this)), i.length);
  349.     }
  350.     else {
  351.         var post = this.parentNode.parentNode;
  352.         msg.wrtSel (i = addbr (getQuoteSrc (getTextContent (post), post, this)), i.length);
  353.     }
  354.     msg.parentNode.mode.selectedIndex = 0;
  355.     return false;
  356. }
  357. function q () {
  358.     var seltxt = getSelection ();
  359.     if (seltxt != "") {
  360.         var post = seltxt.getRangeAt (0).commonAncestorContainer;
  361.         while (post.className != "msg")
  362.             post = post.parentNode;
  363.         msg.wrtSel (i = seltxt.toString ().replace (/(\n\r?|^)(?:\n\r?)?/g, "$1> ") + "\r\n", i.length);
  364.     }
  365.     else {
  366.         post = this.parentNode.parentNode;
  367.         msg.wrtSel (i = getTextContent (post).replace (/(\n\r?|^)(?:\n\r?)?/g, "$1> ")  + "\r\n", i.length);
  368.     }
  369.     msg.parentNode.mode.selectedIndex = 1;
  370.     return false;
  371. }
  372. function user () {
  373.     if ((i = getUserName (this.parentNode.parentNode)) != "anonymous")
  374.         msg.wrtSel (i = "[user]" + i + "[/user], ", i.length);
  375.     else msg.wrtSel (i = "[strong]Михаил[/strong], ", i.length);
  376.     return false;
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement