Tieba Quotor

贴吧引用

  1. // ==UserScript==
  2. // @name Tieba Quotor
  3. // @namespace http://gera2ld.blog.163.com/
  4. // @author Gerald <gera2ld@163.com>
  5. // @icon http://cn.gravatar.com/avatar/a0ad718d86d21262ccd6ff271ece08a3?s=80
  6. // @version 1.5.1
  7. // @description 贴吧引用
  8. // @homepageURL http://gerald.top/code/TiebaQuotor
  9. // @include http://tieba.baidu.com/*
  10. // @exclude http://tieba.baidu.com/tb/*
  11. // @require https://greasyfork.org/scripts/144/code.user.js
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. function quote(e) {
  16. e.preventDefault();
  17. e.stopPropagation(); // otherwise utils.unminify may fail
  18. var o=e.target,m=$(o).parent().hasClass('p_mtail'),p=$(o).parents('.l_post'),l=m?p:$(o).parents('.lzl_single_post'),
  19. info=p.data('field'),u=m?info.author.user_name:l.data('field').user_name;
  20. p=[];
  21. o=l.find(m?'div.d_post_content':'span.lzl_content_main').contents(':not(blockquote)').each(function(i,e){p.push(e);});
  22. while(p.length) // 去掉开头空行
  23. if(p[0].tagName=='BR'||!/\S/.test(p[0])) p.shift(); else break;
  24. for(l=p.length-1,o=[];l>=0&&p.length;l--) { // 屏蔽尾巴,去掉末尾空行
  25. if(p[l].tagName=='BR') {
  26. if(/^\s*($|——|>>)/.test(o.join(''))) {p.splice(l);o.splice(0);}
  27. else break;
  28. } else o.unshift(p[l].innerText||p[l].textContent||p[l].data); // return undefined for IMG
  29. }
  30. o=[];p.forEach(function(i){o.push(i.outerHTML||i.data||'');});
  31. o=o.join('')
  32. .replace(/^\s+|\s+$/gi,'') // 去掉开头和末尾的空白
  33. .replace(/\s?<a [^>]*>@(.*?)<\/a>\s?/gi,' $1 ') // 屏蔽点名
  34. .replace(/<a [^>]*?href="(.*?)"[^>]*>(.*?)<\/a>/,function(v,g1,g2){ // 保留蓝字,屏蔽链接
  35. return g1==g2?g2:'<font color="#261cdc">'+g2+'</font>';
  36. });
  37. o='引用 @'+u+' ('+info.content.post_no+'楼'+(m?'':'之楼中楼')+')<br>'+o+'<br>————————————————————————————————<br><br>';
  38. if(e=utils.unminify) e();
  39. unsafeWindow.test_editor.execCommand('inserthtml',o);
  40. }
  41. function initLinks(o){
  42. var p;
  43. if(o.hasClass('core_reply_tail')) { // 主楼层
  44. var e=o.find('.p_tail').children().children();
  45. if(e.length&&e[0].innerHTML!='沙发') {
  46. p=e.parents('.l_post').data('field').content.post_id;
  47. e[1].outerHTML='<a href="/p/'+PageData.thread.thread_id+'?pid='+p+'#'+p+'" title="精确定位链接">'+e[1].innerHTML+'</a>';
  48. }
  49. if(can_quote) {
  50. $('<a href=#>引用</a>').css({'float':'right','margin-left':'5px'}).appendTo(o.find('.p_mtail')).click(quote);
  51. }
  52. } else if(o.hasClass('core_reply_wrapper') // 楼中楼
  53. ||o.hasClass('lzl_single_post')) { // 楼中楼翻页
  54. p=o.find('span.lzl_time').html(function(i,h) {
  55. var o=$(this).parents('.lzl_single_post').data('field'),
  56. i=$(this).parents('.l_post').data('field').content.post_id;
  57. if(typeof o=='string') o=JSON.parse(o.replace(/'/g,'"'));
  58. return '<a href="/p/'+PageData.thread.thread_id+'?pid='+i+'&cid='+o.spid+'#'+o.spid+'" title="精确定位链接">'+h+'</a>';
  59. });
  60. if(can_quote) {
  61. $('<a href=#>引用</a>').css('margin-right','10px').insertBefore(p).click(quote);
  62. }
  63. }
  64. }
  65. function initQuotationFormat() {
  66. // 修改引用文字格式
  67. utils.addStyle('\
  68. fieldset.d_quote{border:1px solid silver;padding:.5em;}\
  69. fieldset.d_quote .BDE_Image{height:auto !important; width:auto !important; max-height:200px; max-width:560px !important;}\
  70. ');
  71. $('div.d_post_content').each(function(i,e) {
  72. var c=-1,n=$(e).contents(),j,f;
  73. for(j=0;j<n.length;j++) {
  74. f=n[j];
  75. if(c>=0&&f.nodeName=='#text'&&/^\s*—{27,36}\s*$/.test(f.data)) {
  76. n.slice(c,c+3).wrapAll('<legend>').parent().add(
  77. n.slice(c+4,j).wrapAll('<p class=quote_content>').parent()
  78. ).wrapAll('<fieldset class=d_quote>');
  79. $(f).add(n[c+3]).remove();
  80. if((f=n[j+1])&&f.nodeName=='BR') {$(f).remove();j++;}
  81. c=-1;
  82. } else if(f.nodeName=='#text'&&/^\s*引用(\s|&nbsp;?)+$/.test(f.data)
  83. &&n[j+1]&&n[j+1].nodeName=='A'&&n[j+1].innerHTML[0]=='@'
  84. &&n[j+2]&&n[j+2].nodeName=='#text'&&/^\s+\(.*?楼\)$/.test(n[j+2].data)
  85. &&n[j+3].nodeName=='BR') {
  86. c=j;j+=3;
  87. }
  88. }
  89. });
  90. }
  91.  
  92. if(PageData&&PageData.user&&PageData.thread) {
  93. initQuotationFormat(); // 引用格式
  94. var lzl_update=[],
  95. can_quote=PageData.user.is_login&&unsafeWindow.PosterContext&&unsafeWindow.PosterContext.isPostAllowed(),
  96. mo=new MutationObserver(function(changes){
  97. $.each(changes,function(i,e){
  98. $.each(e.addedNodes,function(i,o){initLinks($(o));});
  99. });
  100. });
  101. mo.observe($('#j_p_postlist')[0],{
  102. childList:true,
  103. subtree:true,
  104. });
  105. }