后台
发表于 : 2026年 6月 14日 13:53
代码: 复制代码
#!/bin/bash
# ============================================================
# phpBB 模板功能增强脚本 (完全重写版)
# 功能: 添加名言、外链新窗口、代码复制、本地 Emoji
# 设计目标: 幂等执行,与原脚本效果100%一致
# ============================================================
set -e # 遇到错误时停止执行
# --- 配置区 ---
PHPBB_ROOT="/var/www/www.swedyna.com/forum"
TEMPLATE_DIR="${PHPBB_ROOT}/styles/prosilver/template"
OVERALL_FOOTER="${TEMPLATE_DIR}/overall_footer.html"
BBCODE_FILE="${TEMPLATE_DIR}/bbcode.html"
VIEWTOPIC_FILE="${TEMPLATE_DIR}/viewtopic_body.html"
# --- 辅助函数:检查文件是否存在且未标记 ---
function is_not_modified() {
local file="$1"
local marker="$2"
[[ -f "$file" ]] && ! grep -q "$marker" "$file"
}
# ============================================================
# 1. 添加 Franklin 名言 (在版权信息之前)
# ============================================================
if is_not_modified "$OVERALL_FOOTER" "Benjamin Franklin"; then
sed -i '/<!-- EVENT overall_footer_copyright_prepend -->/a\
\t\t<p class="footer-row">\
\t\t\t<span class="footer-copyright">As we enjoy great advantages from the inventions of others, we should be glad of an opportunity to serve others by any invention of ours, and this we should do freely and generously. —Benjamin Franklin, Autobiography</span>\
\t\t</p>' "$OVERALL_FOOTER"
echo "✅ Franklin 名言: 已添加"
else
echo "⏭️ Franklin 名言: 已存在或文件不可用,跳过"
fi
# ============================================================
# 2. 外链自动在新窗口打开 (添加 JavaScript)
# ============================================================
if is_not_modified "$OVERALL_FOOTER" "外链开新窗"; then
sed -i '/{\$SCRIPTS}/a\
<!--外链开新窗-->\
<script>document.querySelectorAll('\''a[href]'\'').forEach(a=>a.href&&a.host&&a.host!==location.host&&a.setAttribute('\''target'\'','\''_blank'\'')&&a.setAttribute('\''rel'\'','\''noopener'\''));</script>' "$OVERALL_FOOTER"
echo "✅ 外链开新窗: 已添加"
else
echo "⏭️ 外链开新窗: 已存在或文件不可用,跳过"
fi
# ============================================================
# 3. 代码框添加“复制”按钮 (bbcode.html)
# ============================================================
if is_not_modified "$BBCODE_FILE" "copyCode"; then
sed -i 's|<div class="codebox"><p>{L_CODE}{L_COLON} <a href="#" onclick="selectCode(this); return false;">{L_SELECT_ALL_CODE}</a></p><pre><code>|<div class="codebox"><p>{L_CODE}{L_COLON} <a href="#" onclick="copyCode(this);return false;" style="text-decoration:none;cursor:pointer;display: inline-block;float: right">复制代码</a></p><pre><code>|' "$BBCODE_FILE"
echo "✅ bbcode.html 复制按钮: 已添加"
else
echo "⏭️ bbcode.html 复制按钮: 已存在或文件不可用,跳过"
fi
# ============================================================
# 4. 复制功能脚本 (viewtopic_body.html)
# ============================================================
if is_not_modified "$VIEWTOPIC_FILE" "copyCode"; then
sed -i '/<!-- INCLUDE overall_footer.html -->/i\
<!-- 复制代码 -->\
<script>\
function copyCode(t){if(t.disabled)return;t.disabled=!0;var o=t.textContent,c=t.closest('\''.codebox'\'').querySelector('\''pre code'\'').textContent||t.closest('\''.codebox'\'').querySelector('\''pre code'\'').innerText,n=function(){t.textContent='\''复制成功!'\'',t.style.color='\''#1e7e34'\'',setTimeout(()=>{t.textContent=o,t.style.color='\'''\'',t.disabled=!1},2000)};function r(){var e=document.createElement('\''textarea'\'');e.value=c;e.style.position='\''fixed'\'';e.style.opacity=0;document.body.appendChild(e);e.select();e.setSelectionRange&&e.setSelectionRange(0,e.value.length);var s=document.execCommand('\''copy'\'');document.body.removeChild(e);s?n():(t.textContent='\''复制失败'\'',t.style.color='\''#d9534f'\'',setTimeout(()=>{t.textContent=o,t.style.color='\'''\'',t.disabled=!1},2000))}navigator.clipboard&&window.isSecureContext?navigator.clipboard.writeText(c).then(n).catch(r):r()}\
</script>' "$VIEWTOPIC_FILE"
echo "✅ viewtopic_body.html 复制脚本: 已添加"
else
echo "⏭️ viewtopic_body.html 复制脚本: 已存在或文件不可用,跳过"
fi
# ============================================================
# 5. Emoji 本地化 (移除远程 twemoji)
# ============================================================
if is_not_modified "$OVERALL_FOOTER" "emoji表情改为本地"; then
sed -i '/{\$SCRIPTS}/a\
<!--emoji表情改为本地-->\
<script>document.addEventListener('\''DOMContentLoaded'\'',()=>{document.querySelectorAll('\''img.emoji[src*="jsdelivr.net"],img.emoji[src*="twemoji"],img.emoji[src*="maxcdn"]'\'').forEach(img=>img.outerHTML='\''<span class="emoji">'\''+(img.alt||'\'''\'')+'\''</span>'\'');document.querySelectorAll('\''script[src*="twemoji"]'\'').forEach(s=>s.remove());});</script>' "$OVERALL_FOOTER"
echo "✅ Emoji本地化: 已添加"
else
echo "⏭️ Emoji本地化: 已存在或文件不可用,跳过"
fi
echo "🎉 所有操作已完成!"