Posted By

Garciat on 01/02/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

s0laris


pun_quote scripts.js FIXED


 / Published in: JavaScript
 

  1. /***********************************************************************
  2.  
  3. Copyright (C) 2008 PunBB
  4.  
  5. PunBB is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published
  7. by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9.  
  10. PunBB is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. MA 02111-1307 USA
  19.  
  20. ***********************************************************************/
  21.  
  22. document.onmouseup = SetSelected;
  23.  
  24. function getSelectedText()
  25. {
  26. var result = '';
  27. if (document.selection)
  28. result = document.selection.createRange().text;
  29. else if (document.getSelection)
  30. result = document.getSelection();
  31. else if (window.getSelection)
  32. result = window.getSelection();
  33. else
  34. return result;
  35. return result;
  36. }
  37.  
  38. function TrimString(param)
  39. {
  40. param = param.replace(/ /g,' ');
  41. return param.replace(/(^\s+)|(\s+$)/g, '');
  42. }
  43.  
  44. function Reply(tid_param, qid_param)
  45. {
  46. var element = document.getElementsByTagName('div');
  47. for (var i=0; i < element.length; i++)
  48. {
  49. if(element[i].className.match(/^post\s.*/ig))
  50. {
  51.  
  52. var post = new String(element[i].innerHTML);
  53.  
  54. if(post.search('Reply[(]' + tid_param + ',' + qid_param + '[)]') != -1)
  55. {
  56. post=ChangePost(post);
  57. var post_new = RemoveSymbols(post);
  58. var selected_text = (window.selected_text_first == '')?window.selected_text_second:window.selected_text_first;//getSelectedText();
  59.  
  60. if((selected_text != undefined)&&(selected_text!=''))
  61. {
  62. //this is for Chrome browser. Text, selected by user, has 'Range' type, not 'String'. And in some cases, when there is no text selected, Chrome returns one symbol of 'Caret' type
  63. if((selected_text.type=='Range')||(selected_text.type=='Caret'))
  64. selected_text=selected_text.toString();
  65.  
  66. selected_text = RemoveSymbols(selected_text);
  67.  
  68. post = TrimString(post);
  69.  
  70. if((post_new.indexOf(selected_text) != -1) && (selected_text.charAt(0) != ''))
  71. {
  72. var form = document.getElementById('qq');
  73. form.action='post.php?tid=' + tid_param + '&qid=' + qid_param;
  74. element = document.getElementById('post_msg');
  75. element.value =(window.selected_text_first == '')?window.selected_text_second:window.selected_text_first;//getSelectedText();
  76. form.submit();
  77. break;
  78. }
  79. }
  80. location = 'post.php?tid=' + tid_param + '&qid=' + qid_param;
  81. }
  82. }
  83. }
  84. }
  85.  
  86. function QuickQuote(tid_param, qid_param)
  87. {
  88. var element = document.getElementsByTagName('div');
  89.  
  90. for (var i=0; i < element.length; i++)
  91. {
  92.  
  93. if(element[i].className.match(/^post\s.*/ig))
  94. {
  95.  
  96. var post = new String(element[i].innerHTML);
  97. post = post.replace(/[\s]*<p[\s]*class[\s]*=[\s]*["]*[\s]*lastedit[\s]*["]*[\s]*>[\s]*(.*?)[\s]*<\/p>[\s]*/ig,'');
  98. post = post.replace(/[\s]*<div[\s]*class[\s]*=[\s]*["]*[\s]*sig-content[\s]*["]*[\s]*>[\s]*(.*?)[\s]*<\/div>[\s]*/ig,'');
  99. if(post.search('QuickQuote[(]' + tid_param + ',' + qid_param + '[)]') != -1)
  100. {
  101. //get quoted author name from the post
  102. //var RegExp = /<cite>.*\sby\s(.*?):/ig; old markup compatibility
  103. var RegExp =/<span class="*post-byline"*>(?:.*?)<a(?:.*?)>(.*?)<\/a>/ig;
  104. var result = RegExp.exec(post);
  105. RegExp.lastIndex=0;
  106. var author_name;
  107.  
  108. if(result!=null)
  109. author_name=result[1];
  110.  
  111.  
  112. post=ChangePost(post);
  113. var post_new = RemoveSymbols(post);
  114. var selected_text = (window.selected_text_first == '')?window.selected_text_second:window.selected_text_first;//getSelectedText();
  115.  
  116.  
  117. post = TrimString(post);
  118.  
  119. if((selected_text != undefined)&&(selected_text!=''))
  120. {
  121. //this is for Chrome browser. Text, selected by user, has 'Range' type, not 'String'. And in some cases, when there is no text selected, Chrome returns one symbol of 'Caret' type
  122. if((selected_text.type=='Range')||(selected_text.type=='Caret'))
  123. selected_text=selected_text.toString();
  124.  
  125. selected_text = RemoveSymbols(selected_text);
  126.  
  127. if((post_new.indexOf(selected_text) != -1) && (selected_text.charAt(0) != ''))
  128. {
  129. element = document.getElementById('fld1');
  130. element.value +=(window.selected_text_first == '')?'[quote='+author_name+']'+window.selected_text_second+'[/quote]'+'\n':'[quote='+author_name+']'+window.selected_text_first+'[/quote]'+'\n';//getSelectedText();
  131. break;
  132. }
  133. }
  134. element = document.getElementById('fld1');
  135. element.value+='[quote='+author_name+']'+post+'[/quote]'+'\n';
  136. }
  137. }
  138. }
  139.  
  140. }
  141.  
  142. function RemoveSymbols(string)
  143. {
  144. string = string.replace(/\r*/gi,'');
  145. string = string.replace(/\n*/gi,'');
  146. string = string.replace(/\s*/gi,'');
  147. string = string.replace(/\u00A0/g,' ');
  148. string = string.replace(/&nbsp;/g,' ');
  149. string = string.replace(/&lt;/g,'<');
  150. string = string.replace(/&gt;/g,'>');
  151. string = string.replace(/<BR>/ig,'');
  152. return string;
  153. }
  154. function ChangePost(post)
  155. {
  156. var reg = new RegExp('<DIV[\\s]*class[\\s]*=[\\s]*["]*[\\s]*entry\\-content[\\s]*["]*[\\s]*>[\\s\\S]*<DIV[\\s]*class[\\s]*=[\\s]*["]*[\\s]*postfoot[\\s]*["]*[\\s]*>','ig');
  157.  
  158. var post = new String(reg.exec(post));
  159.  
  160. var browse = navigator.userAgent.toLowerCase();
  161.  
  162. post = post.replace(/((<BR>)(<\/P>))|((<BR\/>)(<\/P>))/ig,'$2$4');
  163.  
  164. if(browse.indexOf('opera') == -1)
  165. post = post.replace(/((<BR>)(<P>))|((<BR\/>)(<P>))/ig,'$2$4');
  166.  
  167. post = post.replace(/(:?<BR>)|(:?<BR\/>)/ig,'\n');
  168.  
  169. //</p><p> = \n\n - Opera FF
  170. //</p><p> = /n - IE 7.0
  171. if(browse.indexOf('opera') != -1 || browse.indexOf('gecko') != -1)
  172. post = post.replace(/(:?<\/p>)|(:?<p>)/ig,'\n');
  173. else
  174. post = post.replace(/<\/p>[\s]*<p>/ig,'\n');
  175.  
  176. post = post.replace(/>[\s]*</,'><');
  177.  
  178. //Make [quote="name"]...[/quote]
  179. post = post.replace(/<div[\s]*class[\s]*=[\s]*["]*[\s]*quotebox[\s]*["]*[\s]*>[\s]*<cite>[\s]*/ig,'[quote=');
  180. post = post.replace(/[\s]*wrote:/g,"]");
  181. post = post.replace(/<div[\s]*class[\s]*=[\s]*["]*[\s]*quotebox[\s]*["]*[\s]*>[\s]*/ig,'[quote]');
  182. post = post.replace(/[\s]*<\/blockquote>[\s]*/ig,"[/quote]");
  183.  
  184. //Remove tags
  185. post = post.replace(/<(:?.*?)>/gi,'');
  186.  
  187. //Replace quote = name name on quote = "name name"
  188. post = post.replace(/\[quote=(["][-a-zA-Z0-9]*)[\s]+([-"a-zA-Z0-9]*)\]/g,'[quote=\"$1 $2\"]');
  189.  
  190. //Insert \n before [/quote]
  191. post = post.replace(/\[\/quote\]/g,'\n[/quote]\n');
  192.  
  193. //exotic symbols =)
  194. post = post.replace(/\u00A0/g,' ');
  195. post = post.replace(/&nbsp;/g,' ');
  196. post = post.replace(/&lt;/g,'<');
  197. post = post.replace(/&gt;/g,'>');
  198. return post;
  199. }
  200.  
  201. function SetSelected()
  202. {
  203. switch(window.selected_text_pointer)
  204. {
  205. case 0:
  206. window.selected_text_pointer = 1;
  207. window.selected_text_first = getSelectedText();
  208. break;
  209. case 1:
  210. window.selected_text_pointer = 0;
  211. window.selected_text_second = getSelectedText();
  212. break;
  213. case undefined:
  214. window.selected_text_pointer = 0;
  215. window.selected_text_second = getSelectedText();
  216. break;
  217. }
  218. }

Report this snippet  

You need to login to post a comment.