mm
This commit is contained in:
16
packages/bookmark-sync/manifest.json
Normal file
16
packages/bookmark-sync/manifest.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "defgov-bookmark-sync",
|
||||
"version": "1.0",
|
||||
"manifest_version": 3,
|
||||
"permissions": ["bookmarks", "storage"],
|
||||
"host_permissions": ["<all_urls>"],
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
},
|
||||
"browser_specific_settings": {
|
||||
"gecko": {
|
||||
"id": "bookmark-sync@example.com",
|
||||
"strict_min_version": "109.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "defgov-bookmark-sync",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"typescript": "^6.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chrome": "^0.1.40",
|
||||
"@types/firefox-webext-browser": "^143.0.0",
|
||||
"@types/node": "^25.6.0",
|
||||
"@types/webextension-polyfill": "^0.12.5",
|
||||
"webextension-polyfill": "^0.12.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
export class Server {
|
||||
serverUrl: string;
|
||||
|
||||
constructor(serverUrl: string) {
|
||||
this.serverUrl = serverUrl;
|
||||
}
|
||||
|
||||
login(email: string, password: string) {
|
||||
const result = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
logout(email: string, password: string) {
|
||||
const result = false;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
73
packages/bookmark-sync/src/utils/background.ts
Normal file
73
packages/bookmark-sync/src/utils/background.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
// background.ts
|
||||
|
||||
// 引入 polyfill 以统一 browser 和 chrome 命名空间
|
||||
import browser from "webextension-polyfill";
|
||||
|
||||
// 定义书签节点的类型(可选,为了更严格的类型检查)
|
||||
type BookmarkNode = browser.Bookmarks.BookmarkTreeNode;
|
||||
|
||||
/**
|
||||
* 获取完整的书签树
|
||||
* 使用 async/await 处理异步操作
|
||||
*/
|
||||
async function getBookmarkTree(): Promise<BookmarkNode[]> {
|
||||
try {
|
||||
// 在 Firefox 中,browser.bookmarks 是原生的
|
||||
// 在 Chrome/Edge 中,polyfill 会自动将其映射为 Promise 版本的 chrome.bookmarks
|
||||
const tree = await browser.bookmarks.getTree();
|
||||
return tree;
|
||||
} catch (error) {
|
||||
console.error("获取书签树失败:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近添加的书签
|
||||
*/
|
||||
async function getRecentBookmarks(count: number = 10): Promise<BookmarkNode[]> {
|
||||
return await browser.bookmarks.getRecent(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将书签树转换为 JSON 字符串
|
||||
*/
|
||||
function serializeBookmarks(tree: BookmarkNode[]): string {
|
||||
return JSON.stringify(tree, null, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主同步函数
|
||||
*/
|
||||
async function syncBookmarks() {
|
||||
console.log("🚀 开始同步书签...");
|
||||
|
||||
try {
|
||||
// 1. 获取数据
|
||||
const tree = await getBookmarkTree();
|
||||
|
||||
// 2. 处理数据
|
||||
const jsonString = serializeBookmarks(tree);
|
||||
|
||||
// 3. 模拟上传
|
||||
console.log("✅ 书签数据准备就绪:", {
|
||||
size: jsonString.length,
|
||||
preview: jsonString.substring(0, 100) + "...",
|
||||
});
|
||||
|
||||
// 这里可以添加 fetch 请求发送到你的服务器
|
||||
// await fetch('YOUR_API_URL', { method: 'POST', body: jsonString ... })
|
||||
} catch (err) {
|
||||
console.error("❌ 同步过程发生错误:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// 监听书签变化事件
|
||||
browser.bookmarks.onChanged.addListener((id, changeInfo) => {
|
||||
console.log(`🔔 书签变更通知: ID=${id}, 变更类型=${changeInfo}`);
|
||||
// 防抖处理:实际项目中建议使用 lodash.debounce 避免频繁触发
|
||||
syncBookmarks();
|
||||
});
|
||||
|
||||
// 初始化执行
|
||||
syncBookmarks();
|
||||
17
packages/bookmark-sync/tsconfig.json
Normal file
17
packages/bookmark-sync/tsconfig.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"declaration": true,
|
||||
"types": ["chrome", "firefox-webext-browser"]
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user