以下是一个极简的Chrome浏览器插件实现方案,用于提取微信读书网页版的搜索结果:
核心功能实现// content_script.js(function() { // 监听来自后台的消息 chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.action === "extractBooks") { const books = []; const elements = document.querySelectorAll('div.wr_index_page_mini_bookInfo_content_title'); elements.forEach(element => { books.push(element.textContent.trim()); }); sendResponse({books: books}); } });})();完整插件结构- manifest.json (插件配置文件)
{ "manifest_version": 3, "name": "微信读书搜索结果提取器", "version": "1.0", "description": "提取微信读书网页版搜索结果", "permissions": ["activeTab"], "background": { "service_worker": "background.js" }, "content_scripts": [{ "matches": ["https://weread.qq.com/
*"], "js": ["content_script.js"] }], "action": { "default_popup": "popup.html", "default_icon": { "16": "icon.png", "48": "icon.png", "128": "icon.png" } }}- background.js (后台脚本)
chrome.action.onClicked.addListener((tab) => { chrome.scripting.executeScript({ target: {tabId: tab.id}, function: extractBooks });});function extractBooks() { chrome.runtime.sendMessage({action: "extractBooks"}, (response) => { const results = response.books.join('n'); navigator.clipboard.writeText(results).then(() => { alert(`已提取${response.books.length}条结果并复制到剪贴板`); }); });}- popup.html (插件弹窗界面)
<!DOCTYPE html><html><body style="width: 200px; padding: 10px;"> <button id="extractBtn" style="width: 100%; padding: 8px;">提取搜索结果</button> <script src="popup.js"></script></body></html>- popup.js (弹窗逻辑)
document.getElementById('extractBtn').addEventListener('click', () => { chrome.tabs.query({active: true, currentWindow: true}, (tabs) => { chrome.scripting.executeScript({ target: {tabId: tabs[0].id}, function: extractBooks }); });});使用说明安装插件:
在Chrome地址栏输入 chrome://extensions/
开启"开发者模式"
点击"加载已解压的扩展程序",选择包含上述文件的文件夹
使用方法:
在微信读书搜索页面点击插件图标
或右键点击插件图标选择"提取搜索结果"
结果会自动复制到剪贴板
功能特点- 自动化提取:无需手动操作控制台
- 一键复制:结果直接保存到剪贴板
- 轻量级设计:仅包含必要功能
- Manifest V3:符合最新Chrome扩展标准
扩展建议如需增强功能,可以考虑:
- 添加结果导出为CSV/JSON功能
- 支持自定义选择提取的书籍信息(如作者、评分等)
- 增加批量保存到本地文件功能
这个实现方案比直接使用控制台脚本更便捷,且具有更好的用户体验。用户只需点击插件图标即可完成提取和复制操作,无需手动执行脚本。