【實用教學】如何優雅地整理數千筆瀏覽器書籤!Python + JavaScript 完整解決方案
你是否也有瀏覽器書籤雜亂無章的困擾?資料夾一層又一層,想找個網站卻要翻找半天?本文將分享一個實用的書籤整理方法,讓你輕鬆管理數千筆書籤!
問題背景
在日常使用瀏覽器的過程中,我們常常會儲存各種有用的網頁。隨著時間推移,書籤數量暴增,資料夾結構也越來越複雜。就像我遇到的情況:
- 4,000+ 筆書籤
- 400+ 個資料夾
- 多層巢狀結構
- 找尋特定書籤耗時費力
解決方案概述
為了徹底解決這個問題,我設計了一個完整的書籤整理流程:
- 匯出所有書籤
- 使用 Python 程式將書籤結構扁平化
- 重新匯入瀏覽器
- 使用擴充功能找出重複書籤
- 透過 JavaScript 批次處理重複項目
詳細步驟說明
1. 匯出書籤
首先,需要將瀏覽器中的所有書籤匯出為 HTML 檔案。這個檔案包含了所有書籤的資訊,包括網址、標題、加入時間等。
2. Python 程式處理
使用 Python 的 BeautifulSoup 套件來處理 HTML 檔案,將所有書籤移到同一層級:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| from bs4 import BeautifulSoup import re
def process_bookmarks(html_content): soup = BeautifulSoup(html_content, 'html.parser') bookmarks = soup.find_all('a') new_html = """<!DOCTYPE NETSCAPE-Bookmark-file-1> <!-- This is an automatically generated file. It will be read and overwritten. DO NOT EDIT! --> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"> <TITLE>Bookmarks</TITLE> <H1>Bookmarks</H1> <DL><p> <DT><H3 ADD_DATE="1685050416" LAST_MODIFIED="1732586801" PERSONAL_TOOLBAR_FOLDER="true">書籤列</H3> <DL><p>""" for bookmark in bookmarks: bookmark_html = str(bookmark) if bookmark_html.strip(): new_html += f" <DT>{bookmark_html}</DT>\n" new_html += """ </DL><p> </DL><p>""" return new_html
with open('D:/paste.txt', 'r', encoding='utf-8') as f: content = f.read() print("讀取到的內容長度:", len(content)) print("內容的前100個字元:", content[:100])
processed_html = process_bookmarks(content) with open('bookmarks_flattened.html', 'w', encoding='utf-8') as f: f.write(processed_html) print("已處理完整內容並保存。")
|
3. 重新匯入瀏覽器
將處理後的 bookmarks_flattened.html 檔案重新匯入瀏覽器。這時候所有書籤都會在同一層級,方便後續整理。
4. 使用 Bookmarks clean up 尋找重複
安裝 Bookmarks clean up 擴充功能後,可以自動找出重複的書籤。但是這個工具沒有批次處理功能,如果重複項目很多(像是超過 2,000 個),一個個點選會非常耗時。
5. JavaScript 批次處理
使用以下 JavaScript 程式碼來自動選取要刪除的重複書籤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| async function checkDuplicateBookmarks() { const folderUrls = document.querySelectorAll('.folder-url'); let count = 0; const targetUrls = Array.from(folderUrls).filter(url => url.textContent.includes('//書籤列/已匯入') ); console.log(`找到 ${targetUrls.length} 個項目需要處理`); for (const url of targetUrls) { const listItem = url.closest('.list-group-item'); const checkbox = listItem.querySelector('input[type="checkbox"]'); if (checkbox) { const clickEvent = new MouseEvent('click', { bubbles: true, cancelable: true, view: window }); checkbox.dispatchEvent(clickEvent); count++; console.log(`已處理 ${count}/${targetUrls.length} 個項目`); await new Promise(resolve => setTimeout(resolve, 100)); } } console.log('處理完成!'); }
checkDuplicateBookmarks().catch(console.error);
|
實用擴充推薦
書籤管理必備擴充功能
Bookmarks clean up
- 功能:自動找出重複書籤、尋找有問題連結、刪除空資料夾、合併資料夾
- 特色:支援多種比對模式
- 使用時機:大規模書籤整理
Sprucemarks
- 功能:自動排序書籤
- 特色:
- 依照名稱、日期、網域等條件排序
- 支援資料夾內部排序
- 一鍵完成所有排序
- 使用時機:整理完書籤後,讓它們保持有序
TAB SORTER
- 功能:分頁智慧排序
- 特色:
- 依照網域、標題、使用時間等條件排序
- 支援自動分組
- 快速尋找重複分頁
- 使用時機:開啟大量分頁時,讓瀏覽更有條理
實用建議
定期整理:建議每 3-6 個月進行一次書籤整理,避免堆積過多。
資料夾結構:
- 建立主要分類(如:工作、學習、娛樂)
- 控制資料夾層級在 2-3 層內
- 善用標籤功能代替過多資料夾
管理策略:
- 使用 Sprucemarks 定期排序書籤
- 搭配 TAB SORTER 管理開啟的分頁
- 定期使用 Bookmarks clean up 清理重複項目
結語
透過這套整理流程,再配合推薦的擴充功能,我們可以將雜亂的書籤變得井井有條。這不僅節省了尋找資料的時間,也讓書籤真正發揮它的價值。特別是 Sprucemarks 和 TAB SORTER 這樣的工具,能讓我們的瀏覽器使用體驗更加順暢。希望這篇文章能幫助到同樣有書籤管理困擾的朋友!