Xiuno BBS 钩子系统工作机制

# Xiuno BBS 钩子系统工作机制
## 1. 基本概念
### 1.1 钩子(Hook)
- 钩子是在模板文件中预定义的插入点
- 使用特殊注释语法: `<!--{hook hook_name.htm}-->`
- 钩子名称通常反映其位置和用途,如 `thread_subject_before.htm`
### 1.2 插件结构
plugin/
├─ my_plugin/ # 插件目录
│ ├─ hook/ # 钩子文件目录
│ │ ├─ thread_subject_before.htm # 对应钩子的实现文件
│ │ └─ thread_plugin_after.htm
│ ├─ view/ # 插件视图文件
│ ├─ route/ # 路由处理文件
│ └─ conf.json # 插件配置文件
## 2. 工作流程
1. **钩子定义**
- 系统核心文件(如 thread.htm)中定义钩子位置
- 示例:`<!--{hook thread_subject_before.htm}-->`
2. **插件实现**
- 插件在其 hook 目录下创建同名文件
- 文件内容会被自动注入到对应钩子位置
3. **自动加载**
- 系统启动时扫描所有启用的插件
- 查找匹配的钩子文件
- 在渲染页面时将钩子文件内容插入到对应位置
## 3. 常用钩子位置
### 3.1 帖子页面 (thread.htm)
- thread_subject_before.htm - 帖子标题前
- thread_subject_after.htm - 帖子标题后
- thread_plugin_after.htm - 帖子插件区域
- thread_js.htm - JavaScript 区域
### 3.2 用户页面 (user.htm)
- user_profile_end.htm - 用户资料末尾
- user_nav_end.htm - 导航菜单末尾
## 4. 注意事项
- 钩子文件名必须与钩子定义完全匹配
- 一个钩子位置可以被多个插件使用
- 插件之间的加载顺序由插件优先级决定
# Xiuno BBS 插件开发解决方案模板
## 1. 添加新功能
### 1.1 选择合适的钩子位置
php
<!-- 在帖子标题前添加内容 -->
<!--{hook thread_subject_before.htm}-->
<!-- 在用户菜单后添加内容 -->
<!--{hook user_nav_end.htm}-->
### 1.2 创建钩子文件
php
// plugin/my_plugin/hook/thread_subject_before.htm
<div class="my-plugin-content">
<!-- 插件内容 -->
</div>
### 1.3 添加样式
php
// 方案1:通过样式钩子注入
<!--{hook thread_css.htm}-->
<style>
.my-plugin-content {
/ 样式定义 /
}
</style>
// 方案2:创建独立的CSS文件并在插件中引入
<link rel="stylesheet" href="plugin/my_plugin/view/css/style.css">
## 2. 修改现有功能
### 2.1 定位原始钩子
1. 在核心模板中找到相关功能的钩子位置
2. 检查现有插件中的实现
### 2.2 修改插件代码
php
// 1. 找到对应的钩子文件
// plugin/my_plugin/hook/thread_subject_before.htm
// 2. 修改实现
<div class="modified-content">
<!-- 修改后的内容 -->
</div>
## 3. 调试技巧
1. 临时禁用其他插件,确保修改效果
2. 使用浏览器开发者工具查看最终渲染结果
3. 检查系统日志排查问题
## 4. 常见问题
1. 钩子未生效
- 检查文件名是否匹配
- 确认插件是否启用
- 检查文件权限
2. 样式冲突
- 使用特定的类名前缀
- 适当提高 CSS 选择器优先级

Jeffer.Z.Gustav

This is my note-taking platform. Welcome to visit. I will put some recorded notes here in daily life. You can see some fragments of my thinking and notes.