查看: 1|回复: 0

Discuz X5.0手机版于标题后添加复制链接

[复制链接]

275

主题

25

回帖

3561

威望

管理员

UID
1

发表于 1 小时前 | 显示全部楼层 |阅读模式

马上注册,解锁更多功能,轻松玩转日记网 riji.cn

您需要 登录 才可以下载或查看,没有账号?立即注册

×
找到:
\template\default\touch\forum\viewthread.php


查找
  1.   $_G['forum_thread']['subject']
复制代码
修改为:
  1.    <!-- 添加复制链接功能,原生标题输出,已修复样式继承问题,字体颜色与原标题一致 -->
  2.                         <span id="thread_subject" style="font: inherit; color: inherit; font-size: inherit; font-weight: inherit;">$_G['forum_thread']['subject']</span>
  3.                         
  4.                         <!-- 复制按钮 -->
  5.                         <a href="javascript:;" onclick="copyThreadLink()" class="copy-url-btn" style="font-size:12px; color:#999; margin-left:8px; vertical-align:middle; text-decoration:none; white-space:nowrap;">[复制链接]</a>
复制代码


查找:
  1. </script>
  2. <a href="javascript:;" class="scrolltop bottom"></a>
复制代码


在其上方插入:
  1. // 【新增】 修复后的复制链接功能
  2.         function copyThreadLink() {
  3.                 var subjectEl = document.getElementById('thread_subject');
  4.                 if (!subjectEl) {
  5.                         showToast('错误:未找到标题元素');
  6.                         return;
  7.                 }
  8.                
  9.                 var subject = subjectEl.innerText || subjectEl.textContent;
  10.                 // 清理标题中的多余空格和换行
  11.                 subject = subject.replace(/^\s+|\s+$/g, '');
  12.                
  13.                 var currentUrl = window.location.href.split('#')[0];
  14.                 var bbname = '{$_G[setting][bbname]}';
  15.                
  16.                 var textToCopy = subject + '\n' + currentUrl + '\n(出处: ' + bbname + ')';
  17.                
  18.                 // 优先使用现代 Clipboard API
  19.                 if (navigator.clipboard && navigator.clipboard.writeText) {
  20.                         navigator.clipboard.writeText(textToCopy).then(function() {
  21.                                 showToast('链接已复制到剪贴板');
  22.                         }).catch(function(err) {
  23.                                 console.error('复制失败: ', err);
  24.                                 fallbackCopy(textToCopy);
  25.                         });
  26.                 } else {
  27.                         // 降级方案
  28.                         fallbackCopy(textToCopy);
  29.                 }
  30.         }

  31.         function fallbackCopy(text) {
  32.                 var textArea = document.createElement("textarea");
  33.                 textArea.value = text;
  34.                 textArea.style.position = "fixed";  // 避免滚动到底部
  35.                 document.body.appendChild(textArea);
  36.                 textArea.focus();
  37.                 textArea.select();
  38.                 try {
  39.                         var successful = document.execCommand('copy');
  40.                         if (successful) {
  41.                                 showToast('链接已复制到剪贴板');
  42.                         } else {
  43.                                 showToast('复制失败,请手动复制');
  44.                         }
  45.                 } catch (err) {
  46.                         showToast('浏览器不支持自动复制');
  47.                 }
  48.                 document.body.removeChild(textArea);
  49.         }

  50.         // 【新增】 仿PC版橙色Toast提示
  51.         function showToast(message) {
  52.                 // 移除已存在的toast
  53.                 var oldToast = document.getElementById('custom_toast');
  54.                 if (oldToast) {
  55.                         document.body.removeChild(oldToast);
  56.                 }

  57.                 var toast = document.createElement('div');
  58.                 toast.id = 'custom_toast';
  59.                 toast.innerText = message;
  60.                
  61.                 // 样式设置:橙色底,白字,圆角,居中
  62.                 toast.style.position = 'fixed';
  63.                 toast.style.top = '50%';
  64.                 toast.style.left = '50%';
  65.                 toast.style.transform = 'translate(-50%, -50%)';
  66.                 toast.style.backgroundColor = '#FF9900'; // 橙色背景
  67.                 toast.style.color = '#FFFFFF'; // 白色文字
  68.                 toast.style.padding = '10px 20px';
  69.                 toast.style.borderRadius = '5px';
  70.                 toast.style.fontSize = '14px';
  71.                 toast.style.zIndex = '99999';
  72.                 toast.style.boxShadow = '0 2px 10px rgba(0,0,0,0.2)';
  73.                 toast.style.opacity = '0';
  74.                 toast.style.transition = 'opacity 0.3s ease-in-out';
  75.                
  76.                 document.body.appendChild(toast);
  77.                
  78.                 // 淡入效果
  79.                 setTimeout(function() {
  80.                         toast.style.opacity = '1';
  81.                 }, 10);
  82.                
  83.                 // 1.5秒后淡出并移除
  84.                 setTimeout(function() {
  85.                         toast.style.opacity = '0';
  86.                         setTimeout(function() {
  87.                                 if (document.body.contains(toast)) {
  88.                                         document.body.removeChild(toast);
  89.                                 }
  90.                         }, 300);
  91.                 }, 1500);
  92.         }
  93.         // 添加复制链接功能结束
复制代码


手机版|小黑屋|日记网

相关侵权、举报、投诉及建议等,请联系客服:kf@riji.cn

© 2021-2026 . 日记网已运行: 000 小时 00 Powered by Discuz! X5.0 |苏ICP备17042338号|苏公网安备32021302002657号

在本版发帖返回顶部