ubuntu-it QuickReply Enhanced Editor

Aggiunge alcune delle funzionalità dell'editor completo alla risposta rapida

当前为 2020-01-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ubuntu-it QuickReply Enhanced Editor
  3. // @description Aggiunge alcune delle funzionalità dell'editor completo alla risposta rapida
  4. // @namespace ubuntu-it
  5. // @include http*://forum.ubuntu-it.org/viewtopic.php?*
  6. // @version 0.202001271130
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10.  
  11. (function($, window) {
  12. $.fn.selection = function() {
  13. var doc = window.doc;
  14. var element = this[0];
  15. var selection = {};
  16. if (window.getSelection) {
  17. /* except IE */
  18. selection.start = element.selectionStart;
  19. selection.end = element.selectionEnd;
  20. selection.text = element.value.slice(selection.start, selection.end);
  21. } else if (doc.selection) {
  22. /* for IE */
  23. element.focus();
  24. var range = doc.selection.createRange(),
  25. range2 = doc.body.createTextRange();
  26. selection.text = range.text;
  27. try {
  28. range2.moveToElementText(element);
  29. range2.setEndPoint('StartToStart', range);
  30. } catch (e) {
  31. range2 = element.createTextRange();
  32. range2.setEndPoint('StartToStart', range);
  33. }
  34. selection.start = element.value.length - range2.text.length;
  35. selection.end = selection.start + range.text.length;
  36. }
  37. return selection;
  38. }
  39. })(jQuery, window);
  40.  
  41. //Pulsanti da inserire nell'editor
  42. var buttons = [{
  43. code: 'b',
  44. name: 'B'
  45. }, {
  46. code: 'u',
  47. name: 'u'
  48. }, {
  49. code: 'i',
  50. name: 'i'
  51. }, {
  52. code: 'code',
  53. name: 'Codice'
  54. }, {
  55. code: 'img',
  56. name: 'IMG'
  57. }, {
  58. code: 'quote',
  59. name: 'Cita'
  60. }, {
  61. code: 'url',
  62. name: 'URL'
  63. }, {
  64. code: 'wiki',
  65. name: 'Wiki'
  66. }, {
  67. code: 'forum',
  68. name: 'Forum'
  69. }];
  70.  
  71. var previewBtn = $('<input class="button2" type="submit" value="Anteprima" name="preview" tabindex="8" accesskey="a">');
  72. previewBtn.click(function(){
  73. var action = $(this).parents('form').attr('action');
  74. $(this).parents('form').attr('action', action+'#preview');
  75. });
  76. jQuery('.submit-buttons').append(previewBtn);
  77. jQuery('#message-box').each(function() {
  78. var $buttons = $('<div id="buttons"></div>');
  79. $(this).prepend($buttons);
  80. var $textarea = $('.inputbox', this);
  81. $.each(buttons, function(i, button) {
  82. var $btn = $('<input type="button" class="button2" />');
  83. $btn.val(button.name);
  84. $buttons.append($btn);
  85. $btn.click(function(e) {
  86. e.preventDefault();
  87.  
  88. function exec($textarea,codeOpen, text,codeClose){
  89.  
  90. var textarea = $textarea[0];
  91. var scrollTop = textarea.scrollTop;
  92. var selection = $textarea.selection();
  93.  
  94. var val = $textarea.val();
  95. var pre = val.substring(0, selection.start);
  96.  
  97. var bbcode = '[' + codeOpen + ']' + text + '[/' + codeClose + ']';
  98. var post = val.substring(selection.end);
  99.  
  100. var newSelectionStart = selection.start + 2 + codeClose.length;
  101. var newSelectionStop = newSelectionStart + selection.text.length;
  102. $textarea.val(pre + bbcode + post);
  103. textarea.selectionStart = newSelectionStart;
  104. textarea.selectionEnd = newSelectionStop;
  105. textarea.focus();
  106. textarea.scrollTop = scrollTop;
  107. }
  108.  
  109. var codeOpen = button.code;
  110. var codeClose = button.code;
  111. var selection = $textarea.selection();
  112. var text = selection.text || '';
  113.  
  114. switch(codeOpen){
  115. case 'url':
  116. codeOpen += '=' + prompt("Inserire l'URL");
  117. if( text.length === 0 ){
  118. text = prompt("Inserire il testo");
  119. }
  120. exec($textarea,codeOpen,text,codeClose);
  121. break;
  122. case 'forum':
  123. var url = prompt("Inserire l'URL");
  124. var tid = 0;
  125. if( url.match(/t=[\d]+/) ){
  126. tid = url.match(/t=([\d]+)/)[1];
  127. }else if( url.match(/^[\d]+$/) ){
  128. tid = url;
  129. }
  130. if( tid>0 ){
  131. url = 'https://forum.ubuntu-it.org/viewtopic.php?t='+tid;
  132. }
  133. jQuery.get(url,function(res){
  134. var text = jQuery('h2:first',res).text()
  135. codeOpen = 'url='+url;
  136. codeClose = 'url';
  137. exec($textarea,codeOpen,text,codeClose);
  138. });
  139. break;
  140. default:
  141. exec($textarea,codeOpen,text,codeClose);
  142. break;
  143. }
  144.  
  145. });
  146. });
  147. });