// ==UserScript== // @name 更好的书签 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 一个功能强大的书签管理工具,支持双栏显示、搜索等功能 // @author xjy666a // @match *://*/* // @match file://*/* // @include * // @grant GM_setValue // @grant GM_getValue // @grant GM_addValueChangeListener // @grant GM_registerMenuCommand // @run-at document-idle // @priority 1 // @license MIT // @icon https://youke1.picui.cn/s1/2025/11/08/690efd26c73bb.png // ==/UserScript== /* MIT License Copyright (c) 2025 xjy666a Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function() { 'use strict'; // 创建书签管理器 class BookmarkManager { constructor() { this.bookmarks = []; this.filteredBookmarks = []; this.currentView = 'grid'; // grid 或 list this.searchQuery = ''; this.isVisible = false; this.shadowRoot = null; this.selectedBookmarks = new Set(); // 选中的书签URL集合 this.batchMode = false; // 是否处于批量选择模式 this.storageListener = null; // 存储变化监听器 this.init(); } init() { // 创建主界面 this.createUI(); // 绑定事件 this.bindEvents(); // 从存储加载数据(使用 GM_getValue,可在所有网站间共享) this.loadFromStorage(); // 监听存储变化,实现跨标签页同步 this.setupStorageListener(); // 添加页面关闭前保存 this.setupAutoSave(); } createUI() { // 创建主容器 const container = document.createElement('div'); container.id = 'better-bookmarks-container'; // 创建 Shadow DOM 来隔离样式 this.shadowRoot = container.attachShadow({ mode: 'open' }); // 在 Shadow DOM 中创建内容 this.shadowRoot.innerHTML = `
还没有书签,请先导入书签文件、添加书签或将当前访问页面保存为书签
${this.searchQuery ? '没有找到匹配的书签' : '还没有书签,请先导入书签文件、添加书签或将当前访问页面保存为书签'}
${!this.searchQuery ? `` : ''}`; // 生成HTML(所有书签放在一个文件夹中) html += `
\n`; this.bookmarks.forEach(bookmark => { const date = Math.floor(bookmark.addDate / 1000); const url = this.escapeHtml(bookmark.url); const icon = this.escapeHtml(bookmark.icon || ''); const title = this.escapeHtml(bookmark.title); html += `
\n`; html += `
`; // 下载文件 const blob = new Blob([html], { type: 'text/html;charset=utf-8' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `bookmarks_${new Date().toISOString().split('T')[0]}.html`; a.click(); URL.revokeObjectURL(url); } } // 注册油猴菜单命令 - 显示快捷键提示 GM_registerMenuCommand('快捷键说明', () => { const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0; const ctrlKey = isMac ? 'Cmd' : 'Ctrl'; const shortcuts = [ `${ctrlKey} + B:打开/关闭书签界面`, `${ctrlKey} + Shift + A:添加当前页面到书签`, `Esc:关闭书签界面` ]; alert('快捷键说明:\n\n' + shortcuts.join('\n')); }); // 等待DOM加载完成后初始化 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => { new BookmarkManager(); }); } else { new BookmarkManager(); } })();