OpenClaw浏览器插件:Phi-3-mini网页信息提取神器

张开发
2026/4/10 0:48:09 15 分钟阅读

分享文章

OpenClaw浏览器插件:Phi-3-mini网页信息提取神器
OpenClaw浏览器插件Phi-3-mini网页信息提取神器1. 为什么需要浏览器插件与本地模型结合在日常工作中我经常需要从网页中提取关键信息、生成摘要或填写表单。传统做法是手动复制粘贴或者写爬虫脚本但前者效率低下后者维护成本高。直到发现OpenClaw可以结合本地部署的Phi-3-mini模型我才找到了一个平衡点——既保持数据隐私又能实现智能自动化。这个方案的独特价值在于数据不出本地所有网页内容处理和模型推理都在本机完成避免了敏感信息外泄响应速度快相比调用云端API本地模型减少了网络延迟可定制性强可以根据特定需求调整提示词和数据处理逻辑2. 环境准备与模型部署2.1 安装OpenClaw核心组件我选择从官方脚本开始安装这是最不容易出错的方式curl -fsSL https://openclaw.ai/install.sh | bash openclaw onboard --install-daemon安装完成后需要配置模型接入。这里我使用的是本地部署的Phi-3-mini-128k-instruct模型通过vLLM提供服务。2.2 配置本地模型连接修改OpenClaw的配置文件~/.openclaw/openclaw.json添加模型提供商{ models: { providers: { local-phi3: { baseUrl: http://localhost:8000/v1, apiKey: no-key-required, api: openai-completions, models: [ { id: phi-3-mini, name: Phi-3 Mini Local, contextWindow: 128000, maxTokens: 4096 } ] } } } }配置完成后重启网关服务openclaw gateway restart3. 开发Chrome扩展程序3.1 扩展程序基础结构我的插件目录结构如下phi3-web-extractor/ ├── manifest.json ├── background.js ├── content.js ├── popup/ │ ├── popup.html │ ├── popup.js │ └── styles.css └── icons/ ├── icon48.png └── icon128.png关键文件manifest.json配置{ manifest_version: 3, name: Phi-3 Web Extractor, version: 1.0, permissions: [activeTab, storage], background: { service_worker: background.js }, action: { default_popup: popup/popup.html, default_icon: { 48: icons/icon48.png, 128: icons/icon128.png } }, content_scripts: [ { matches: [all_urls], js: [content.js] } ] }3.2 与OpenClaw通信的核心逻辑在background.js中我实现了与本地OpenClaw服务的通信const OPENCLAW_ENDPOINT http://localhost:18789/api/v1/chat/completions; async function queryOpenClaw(prompt, webpageContent) { const fullPrompt ${prompt}\n\n网页内容:\n${webpageContent}; const response await fetch(OPENCLAW_ENDPOINT, { method: POST, headers: { Content-Type: application/json, }, body: JSON.stringify({ model: phi-3-mini, messages: [ { role: user, content: fullPrompt } ], temperature: 0.3 }) }); return await response.json(); } chrome.runtime.onMessage.addListener((request, sender, sendResponse) { if (request.action processContent) { queryOpenClaw(request.prompt, request.content) .then(result sendResponse(result)) .catch(error sendResponse({error: error.message})); return true; // 保持消息端口开放 } });4. 实现三大核心功能4.1 智能内容摘要在content.js中捕获网页内容并发送处理请求function extractMainContent() { // 简单的正文提取逻辑可根据实际需求优化 const article document.querySelector(article) || document.querySelector(.post-content) || document.body; return article.innerText.trim().substring(0, 50000); } chrome.runtime.sendMessage({ action: processContent, prompt: 请用中文为以下网页内容生成一段200字左右的摘要保留关键事实和数据:, content: extractMainContent() }, response { if (response.error) { console.error(处理失败:, response.error); } else { const summary response.choices[0].message.content; // 在页面右上角显示摘要 showSummaryPopup(summary); } });4.2 关键数据抓取针对特定类型网页如产品页可以定制数据提取模板function extractProductData() { const price document.querySelector(.price).innerText; const title document.querySelector(.product-title).innerText; const specs Array.from(document.querySelectorAll(.spec-item)) .map(item item.innerText).join(\n); return { price, title, specs }; } chrome.runtime.sendMessage({ action: processContent, prompt: 请从以下产品信息中提取结构化JSON数据包含price、title、keySpecs三个字段:, content: JSON.stringify(extractProductData()) }, response { // 处理返回的结构化数据 });4.3 自动填表功能通过内容脚本监听表单并自动填充function autoFillFormWithData(data) { document.querySelector(input[namename]).value data.name; document.querySelector(textarea[namecomments]).value data.summary; // 其他字段... } chrome.runtime.sendMessage({ action: processContent, prompt: 根据以下上下文生成适合填入联系表单的数据返回JSON格式:, content: extractMainContent() }, response { const formData JSON.parse(response.choices[0].message.content); autoFillFormWithData(formData); });5. 插件打包与安装指南5.1 开发模式加载打开Chrome浏览器访问chrome://extensions/开启开发者模式点击加载已解压的扩展程序选择插件目录5.2 生产环境打包使用官方打包工具生成.crx文件cd phi3-web-extractor zip -r ../phi3-web-extractor.zip *然后在Chrome开发者仪表板上传打包文件发布。5.3 权限配置要点在manifest.json中需要特别注意这些权限{ permissions: [ activeTab, storage, scripting ], host_permissions: [ http://localhost/* ] }特别是localhost权限这是与本地OpenClaw服务通信所必需的。6. 实际使用中的经验与调优在开发过程中我遇到了几个关键问题并找到了解决方案上下文长度限制Phi-3-mini虽然支持128k上下文但实际使用中发现超过50k tokens后质量下降。我的解决方法是先对网页内容进行预处理只保留核心区域。响应速度优化初始实现中每次请求都需要3-5秒响应。通过以下改进降到1秒内在OpenClaw配置中启用stream: false限制最大token数为512使用更简洁的提示词模板隐私保护机制为避免意外发送敏感数据我添加了内容过滤层function sanitizeContent(text) { const patterns [ /\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b/g, // 信用卡号 /\b\d{3}[- ]?\d{2}[- ]?\d{4}\b/g // SSN ]; return patterns.reduce((str, pattern) str.replace(pattern, [REDACTED]), text); }7. 效果展示与使用建议经过一个月的日常使用这个插件已经成为我的生产力利器。典型使用场景包括快速消化长篇技术文档摘要功能可以提取核心论点竞品分析从多个产品页自动提取规格参数对比日报生成收集多个网页信息后自动整理成日报框架对于想要尝试的开发者我的建议是先从简单功能开始如单页摘要逐步添加领域特定的处理逻辑针对常用网站开发定制提取器建立提示词库保存常用指令模板这个方案的独特优势在于完全自主可控不需要依赖任何第三方服务特别适合处理敏感或专有信息。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。

更多文章