构建Chrome书签分类管理插件:智能文件夹与分类编辑
本文将指导你开发一个Chrome扩展,实现书签的智能分类管理。核心功能包括:通过弹出窗口选择分类并自动创建缺失的文件夹,以及提供可编辑分类的选项页面。以下逐步分解实现细节。
1. 清单文件配置 (manifest.json)
定义扩展权限和基础结构,需要声明 bookmarks、storage、activeTab 和 scripting 权限。
{
"manifest_version": 3,
"name": "书签分类管理器",
"version": "1.0",
"permissions": ["bookmarks", "activeTab", "storage", "scripting"],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/icon16.png",
"48": "images/icon48.png",
"128": "images/icon128.png"
}
},
"options_page": "options.html",
"background": {
"service_worker": "background.js"
},
"icons": {
"16": "images/icon16.png",
"48": "images/icon48.png",
"128": "images/icon128.png"
}
}
2. 后台服务脚本 (background.js)
当前版本核心逻辑位于popup页面,后台脚本可留空作为扩展入口。
// 此文件主要用于扩展生命周期管理,当前逻辑暂不涉及后台处理
3. 选项页面 (options.html + options.js)
提供分类列表编辑界面,支持逗号分隔输入并持久化存储。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>分类管理</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
textarea { width: 350px; height: 180px; font-size: 18px; padding: 8px; }
.container { max-width: 500px; margin: 0 auto; }
button { margin-top: 15px; padding: 8px 20px; cursor: pointer; }
</style>
</head>
<body>
<div class="container">
<h2>编辑分类(逗号分隔)</h2>
<textarea id="categoryInput" placeholder="例如:AI工具,开发,学习,工作"></textarea>
<br>
<button id="saveBtn">保存设置</button>
</div>
<script src="options.js"></script>
</body>
</html>
// 默认分类列表
const DEFAULT_CATEGORIES = ['AI应用', '机器学习', '前端开发', '后端架构', '设计资源'];
document.addEventListener('DOMContentLoaded', () => {
// 加载已有配置,若无则使用默认
chrome.storage.sync.get('categories', ({ categories }) => {
const list = categories || DEFAULT_CATEGORIES;
document.getElementById('categoryInput').value = list.join(', ');
});
// 保存逻辑
document.getElementById('saveBtn').addEventListener('click', () => {
const raw = document.getElementById('categoryInput').value;
const items = raw.split(',').map(s => s.trim()).filter(Boolean);
chrome.storage.sync.set({ categories: items }, () => {
alert('分类已更新');
});
});
});
4. 弹出窗口 (popup.html + popup.js)
实现分类选择、自动创建文件夹、收藏当前标签页的功能。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>书签收藏</title>
<style>
body { width: 220px; padding: 15px; font-family: Arial, sans-serif; }
select { width: 100%; padding: 6px; margin: 10px 0; }
button { width: 100%; padding: 8px; background: #4CAF50; color: white; border: none; cursor: pointer; }
button:hover { background: #45a049; }
</style>
</head>
<body>
<h3>收藏到分类</h3>
<select id="catSelect"></select>
<button id="addBtn">添加书签</button>
<script src="popup.js"></script>
</body>
</html>
document.addEventListener('DOMContentLoaded', () => {
// 从存储中获取分类,若无则使用默认
chrome.storage.sync.get('categories', ({ categories }) => {
const list = categories || ['AI应用', '机器学习', '前端开发', '后端架构', '设计资源'];
const selector = document.getElementById('catSelect');
list.forEach(cat => {
const opt = document.createElement('option');
opt.value = cat;
opt.textContent = cat;
selector.appendChild(opt);
});
});
// 添加书签逻辑
document.getElementById('addBtn').addEventListener('click', () => {
const selectedCategory = document.getElementById('catSelect').value;
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
if (!tab) return;
// 检查根目录下是否已有同名文件夹
chrome.bookmarks.getTree(([root]) => {
const bookmarksBar = root.children[0]; // 书签栏
let targetFolder = null;
// 递归查找分类文件夹
function findFolder(nodes) {
for (const node of nodes) {
if (node.title === selectedCategory && node.children !== undefined) {
targetFolder = node;
return true;
}
if (node.children && findFolder(node.children)) return true;
}
return false;
}
findFolder(bookmarksBar.children);
// 若未找到,则在书签栏创建新文件夹
if (!targetFolder) {
chrome.bookmarks.create({
parentId: bookmarksBar.id,
title: selectedCategory
}, (newFolder) => {
createBookmark(newFolder.id, tab);
});
} else {
createBookmark(targetFolder.id, tab);
}
});
});
});
// 在指定文件夹中创建书签
function createBookmark(folderId, tab) {
chrome.bookmarks.create({
parentId: folderId,
title: tab.title,
url: tab.url
}, () => {
console.log(`已将 "${tab.title}" 收藏至分类 "${selectedCategory}"`);
window.close(); // 完成后关闭弹出窗
});
}
});
5. 样式补充
可根据需求创建 popup.css 和 options.css 文件进一步美化,上述代码已内联基础样式。
此扩展演示了Chrome书签API的典型用法,实际部署前建议完善错误处理和边缘情况(如网络异常、标签页无URL等)。