Files
insightflow/chrome-extension/popup.js
OpenClaw Bot 797ca58e8e Phase 7 Task 7: 插件与集成系统
- 创建 plugin_manager.py 模块
  - PluginManager: 插件管理主类
  - ChromeExtensionHandler: Chrome 插件处理
  - BotHandler: 飞书/钉钉/Slack 机器人处理
  - WebhookIntegration: Zapier/Make Webhook 集成
  - WebDAVSync: WebDAV 同步管理

- 创建完整的 Chrome 扩展代码
  - manifest.json, background.js, content.js, content.css
  - popup.html/js: 弹出窗口界面
  - options.html/js: 设置页面
  - 支持网页剪藏、选中文本保存、项目选择

- 更新 schema.sql 添加插件相关数据库表
  - plugins: 插件配置表
  - bot_sessions: 机器人会话表
  - webhook_endpoints: Webhook 端点表
  - webdav_syncs: WebDAV 同步配置表
  - plugin_activity_logs: 插件活动日志表

- 更新 main.py 添加插件相关 API 端点
  - GET/POST /api/v1/plugins - 插件管理
  - POST /api/v1/plugins/chrome/clip - Chrome 插件保存网页
  - POST /api/v1/bots/webhook/{platform} - 接收机器人消息
  - GET /api/v1/bots/sessions - 机器人会话列表
  - POST /api/v1/webhook-endpoints - 创建 Webhook 端点
  - POST /webhook/{type}/{token} - 接收外部 Webhook
  - POST /api/v1/webdav-syncs - WebDAV 同步配置
  - POST /api/v1/webdav-syncs/{id}/test - 测试 WebDAV 连接
  - POST /api/v1/webdav-syncs/{id}/sync - 触发 WebDAV 同步

- 更新 requirements.txt 添加插件依赖
  - beautifulsoup4: HTML 解析
  - webdavclient3: WebDAV 客户端

- 更新 STATUS.md 和 README.md 开发进度
2026-02-23 12:09:15 +08:00

195 lines
5.4 KiB
JavaScript

// InsightFlow Chrome Extension - Popup Script
document.addEventListener('DOMContentLoaded', async () => {
const clipBtn = document.getElementById('clipBtn');
const settingsBtn = document.getElementById('settingsBtn');
const projectSelect = document.getElementById('projectSelect');
const statusDot = document.getElementById('statusDot');
const statusText = document.getElementById('statusText');
const messageEl = document.getElementById('message');
const openDashboard = document.getElementById('openDashboard');
// 加载配置和项目列表
await loadConfig();
// 保存当前页面按钮
clipBtn.addEventListener('click', async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
// 更新按钮状态
clipBtn.disabled = true;
clipBtn.innerHTML = '<span class="loading"></span> 保存中...';
// 保存选中的项目
const projectId = projectSelect.value;
if (projectId) {
const config = await getConfig();
config.defaultProjectId = projectId;
await saveConfig(config);
}
// 发送剪藏请求
chrome.runtime.sendMessage({
action: 'clipPage'
}, (response) => {
clipBtn.disabled = false;
clipBtn.innerHTML = `
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M12 5v14M5 12h14"/>
</svg>
保存当前页面
`;
if (response && response.success) {
showMessage('保存成功!', 'success');
updateStats();
} else {
showMessage(response?.error || '保存失败', 'error');
}
});
});
// 设置按钮
settingsBtn.addEventListener('click', () => {
chrome.runtime.openOptionsPage();
});
// 打开控制台
openDashboard.addEventListener('click', async (e) => {
e.preventDefault();
const config = await getConfig();
chrome.tabs.create({ url: config.serverUrl });
});
});
// 加载配置
async function loadConfig() {
const config = await getConfig();
// 检查连接状态
checkConnection(config);
// 加载项目列表
loadProjects(config);
// 更新统计
updateStats();
}
// 检查连接状态
async function checkConnection(config) {
const statusDot = document.getElementById('statusDot');
const statusText = document.getElementById('statusText');
if (!config.apiKey) {
statusDot.classList.add('error');
statusText.textContent = '未配置 API Key';
return;
}
try {
const response = await fetch(`${config.serverUrl}/api/v1/projects`, {
headers: { 'X-API-Key': config.apiKey }
});
if (response.ok) {
statusText.textContent = '已连接';
} else {
statusDot.classList.add('error');
statusText.textContent = '连接失败';
}
} catch (error) {
statusDot.classList.add('error');
statusText.textContent = '连接错误';
}
}
// 加载项目列表
async function loadProjects(config) {
const projectSelect = document.getElementById('projectSelect');
if (!config.apiKey) {
projectSelect.innerHTML = '<option>请先配置 API Key</option>';
return;
}
try {
const response = await fetch(`${config.serverUrl}/api/v1/projects`, {
headers: { 'X-API-Key': config.apiKey }
});
if (response.ok) {
const data = await response.json();
const projects = data.projects || [];
// 更新项目数统计
document.getElementById('projectCount').textContent = projects.length;
// 填充下拉框
let html = '<option value="">选择保存项目...</option>';
projects.forEach(project => {
const selected = project.id === config.defaultProjectId ? 'selected' : '';
html += `<option value="${project.id}" ${selected}>${escapeHtml(project.name)}</option>`;
});
projectSelect.innerHTML = html;
}
} catch (error) {
console.error('Failed to load projects:', error);
}
}
// 更新统计
async function updateStats() {
// 从存储中获取统计数据
const result = await chrome.storage.local.get(['clipStats']);
const stats = result.clipStats || { total: 0, today: 0, lastDate: null };
// 检查是否需要重置今日计数
const today = new Date().toDateString();
if (stats.lastDate !== today) {
stats.today = 0;
stats.lastDate = today;
await chrome.storage.local.set({ clipStats: stats });
}
document.getElementById('clipCount').textContent = stats.total;
document.getElementById('todayCount').textContent = stats.today;
}
// 显示消息
function showMessage(text, type) {
const messageEl = document.getElementById('message');
messageEl.textContent = text;
messageEl.className = `message ${type}`;
setTimeout(() => {
messageEl.className = 'message';
}, 3000);
}
// 获取配置
function getConfig() {
return new Promise((resolve) => {
chrome.storage.sync.get(['insightflowConfig'], (result) => {
resolve(result.insightflowConfig || {
serverUrl: 'http://122.51.127.111:18000',
apiKey: '',
defaultProjectId: ''
});
});
});
}
// 保存配置
function saveConfig(config) {
return new Promise((resolve) => {
chrome.storage.sync.set({ insightflowConfig: config }, resolve);
});
}
// HTML 转义
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}