Copy Tieba Link

复制贴吧的贴子标题与链接

  1. // ==UserScript==
  2. // @name Copy Tieba Link
  3. // @version 1.1
  4. // @description 复制贴吧的贴子标题与链接
  5. // @match *://tieba.baidu.com/*
  6. // @include *://tieba.baidu.com/*
  7. // @author 864907600cc
  8. // @icon https://secure.gravatar.com/avatar/147834caf9ccb0a66b2505c753747867
  9. // @run-at document-start
  10. // @grant GM_setClipboard
  11. // @grant GM_addStyle
  12. // @namespace http://ext.ccloli.com
  13. // ==/UserScript==
  14.  
  15. var setting = {
  16. title: true, // 是否复制标题,默认为 true
  17. author: false, // 是否复制作者(复制楼中楼时则为楼中楼作者),默认为 false
  18. with_at: true, // 若复制作者,则是否需要添加 @,默认为 true
  19. link: true, // 是否复制链接,默认为 true
  20. split: "\n", // 分隔符,默认为换行符 \n
  21. tips: true, // 是否显示提示信息,默认为 true
  22. tips_time: 5 // 提示显示时间,默认为 5(秒)
  23. };
  24.  
  25. var linkAnchor = document.createElement('a');
  26. linkAnchor.className = 'tieba-link-anchor';
  27. linkAnchor.textContent = '[复制链接]';
  28.  
  29. var linkPath = 'http://tieba.baidu.com/p/';
  30.  
  31.  
  32. function copyLink(){
  33. var textGroup = [];
  34. var text;
  35. var parent = this.parentElement;
  36.  
  37. if (this.dataset.linkText) text = this.dataset.linkText;
  38. else {
  39. switch (this.dataset.anchorType) {
  40. case '0': // 贴子内页获取贴子链接
  41. if (setting.title) textGroup.push(unsafeWindow.PageData.thread.title);
  42. if (setting.author) textGroup.push((setting.with_at ? '@' : '') + unsafeWindow.PageData.thread.author + ' ');
  43. if (setting.link) textGroup.push(linkPath + unsafeWindow.PageData.thread.thread_id);
  44. break;
  45.  
  46. case '1': // 贴吧首页获取贴子链接
  47. if (setting.title) textGroup.push(parent.getElementsByClassName('j_th_tit')[0].getAttribute('title'));
  48. if (setting.author) textGroup.push((setting.with_at ? '@' : '') + parent.nextElementSibling.getElementsByClassName('j_user_card')[0].textContent + ' ');
  49. if (setting.link) textGroup.push(parent.getElementsByClassName('j_th_tit')[0].href);
  50. break;
  51.  
  52. case '2': // 贴子内页获取楼层链接
  53. var floorData = JSON.parse(parent.parentElement.parentElement.parentElement.dataset.field);
  54. if (setting.title) textGroup.push(unsafeWindow.PageData.thread.title + ' #' + floorData.content.post_no);
  55. if (setting.author) textGroup.push((setting.with_at ? '@' : '') + floorData.author.user_name + ' ');
  56. if (setting.link) textGroup.push(linkPath + unsafeWindow.PageData.thread.thread_id + '?pid=' + floorData.content.post_id + '#' + floorData.content.post_id);
  57. break;
  58. }
  59.  
  60. console.log(textGroup);
  61.  
  62. text = textGroup.join(setting.split);
  63. this.setAttribute('data-link-text', text);
  64. }
  65.  
  66. GM_setClipboard(text);
  67. if (setting.tips) showTips('以下内容已复制到剪贴板:\n\n' + text);
  68. }
  69.  
  70. function showTips(text) {
  71. var text = text.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br>');
  72.  
  73. var node = document.createElement('div');
  74. node.className = 'tieba-link-tips';
  75. node.innerHTML = text;
  76. document.body.appendChild(node);
  77.  
  78. setTimeout(function(){
  79. document.body.removeChild(node);
  80. }, setting.tips_time * 1000);
  81. }
  82.  
  83. function catchLinkTarget(event) {
  84. if (event.animationName !== 'tiebaLinkTarget') return;
  85.  
  86. var target = event.target;
  87. var classList = target.classList;
  88.  
  89. var curAnchor = linkAnchor.cloneNode(true);
  90. curAnchor.addEventListener('click', copyLink);
  91.  
  92. if (classList.contains('threadlist_title')) {
  93. curAnchor.setAttribute('data-anchor-type', '1');
  94. target.insertBefore(curAnchor, target.getElementsByClassName('j_th_tit')[0]);
  95. }
  96. else if (classList.contains('core_title')) {
  97. curAnchor.setAttribute('data-anchor-type', '0');
  98. target.appendChild(curAnchor);
  99. }
  100. else if (classList.contains('core_reply_tail')) {
  101. curAnchor.setAttribute('data-anchor-type', '2');
  102. target.appendChild(curAnchor);
  103. }
  104.  
  105. }
  106.  
  107. // 使用 animation 事件,方便处理贴吧 ajax 加载数据
  108. document.addEventListener('animationstart', catchLinkTarget, false);
  109. document.addEventListener('MSAnimationStart', catchLinkTarget, false);
  110. document.addEventListener('webkitAnimationStart', catchLinkTarget, false);
  111.  
  112. GM_addStyle(`
  113. @-webkit-keyframes tiebaLinkTarget {}
  114. @-moz-keyframes tiebaLinkTarget {}
  115. @keyframes tiebaLinkTarget {}
  116.  
  117. @-webkit-keyframes tiebaLinkTips {
  118. from {
  119. opacity: 0;
  120. bottom: -75px;
  121. }
  122. 20% {
  123. opacity: 1;
  124. bottom: 10px;
  125. }
  126. 80% {
  127. opacity: 1;
  128. bottom: 10px;
  129. }
  130. to {
  131. opacity: 0;
  132. bottom: -75px;
  133. }
  134. }
  135. @-moz-keyframes tiebaLinkTips {
  136. from {
  137. opacity: 0;
  138. bottom: -75px;
  139. }
  140. 20% {
  141. opacity: 1;
  142. bottom: 10px;
  143. }
  144. 80% {
  145. opacity: 1;
  146. bottom: 10px;
  147. }
  148. to {
  149. opacity: 0;
  150. bottom: -75px;
  151. }
  152. }
  153. @keyframes tiebaLinkTips {
  154. from {
  155. opacity: 0;
  156. bottom: -75px;
  157. }
  158. 20% {
  159. opacity: 1;
  160. bottom: 10px;
  161. }
  162. 80% {
  163. opacity: 1;
  164. bottom: 10px;
  165. }
  166. to {
  167. opacity: 0;
  168. bottom: -75px;
  169. }
  170. }
  171. .tieba-link-anchor {
  172. display: none;
  173. color: #319630 !important;
  174. cursor: pointer;
  175. float: right;
  176. }
  177. .j_thread_list:hover .tieba-link-anchor,
  178. .l_post:hover .tieba-link-anchor,
  179. .core_title:hover .tieba-link-anchor,
  180. .tieba-link-anchor:hover {
  181. display: inline-block;
  182. }
  183.  
  184. .threadlist_title,
  185. .core_reply_tail,
  186. .core_title {
  187. -webkit-animation: tiebaLinkTarget;
  188. -moz-animation: tiebaLinkTarget;
  189. animation: tiebaLinkTarget;
  190. }
  191.  
  192. .core_title:hover .core_title_txt {
  193. width: 420px !important;
  194. }
  195.  
  196. .tieba-link-tips {
  197. background: #ff7f3e;
  198. font-size: 14px;
  199. padding: 10px;
  200. border-radius: 3s;
  201. position: fixed;
  202. right: 10px;
  203. color: #ffffff;
  204. z-index: 99999999;
  205. pointer-events: none;
  206. -webkit-animation: tiebaLinkTips ` + setting.tips_time + `s;
  207. -moz-animation: tiebaLinkTips ` + setting.tips_time + `s;
  208. animation: tiebaLinkTips ` + setting.tips_time + `s;
  209. }
  210. `);