怎样提取 Chrome 书签的添加日期

January 27, 2024

引言

最近我在整理以前学过的东西,想同时查看浏览器书签和历史记录,这样能更高效地回顾当时查过和收藏过的资料。

Chrome 的历史记录会显示日期,也会按日期排列,书签却没有这样的显示方式。

寻找办法

扩展程序

我找到了一些扩展程序,可我实在有点多疑,不太信任它们。

直接查源文件

我了解到,用户配置目录中的 Bookmarks 文件带有一个叫作 date_added 的时间戳字段。

下一步是找到这个文件。 我目前用的是 Windows。 Chrome 用户数据文件夹的路径是 Users/[Username]/AppData/Local/Google/Chrome/User Data/Default。

  1. @taipou(2017 年 1 月 22 日):“打开个人配置目录中的 Bookmarks 文件,找到相应书签,查看 date_added,再把时间戳转换为日期和时间。” Reddit 评论。https://www.reddit.com/r/chrome/comments/5p94sr/is_there_a_way_to_see_what_date_i_added_a_bookmark/
  2. Vineeth Kumar Marupadige(2017 年 1 月 22 日)给出的恢复办法如下: 在 Windows 资源管理器中搜索 bookmarks.bak。 右键点击文件,选择“打开文件所在的位置”。打开的应该是 Chrome 用户数据目录,即 Users/[Username]/AppData/Local/Google/Chrome/User Data/Default。 用记事本打开书签备份,看看原来的书签是否还在。 如果 Chrome 正在同步书签,在当时版本的“设置 → 高级同步设置”(登录一栏)中关闭书签同步。 关闭 Chrome。 回到用户数据目录,找到另一个没有扩展名的 Bookmarks 文件,将它改名为 Bookmarks2.bak。 然后将 Bookmarks.bak 改名为 Bookmarks。Quora 回答。https://www.quora.com/How-can-I-find-my-bookmarks-if-they-have-disappeared-from-Chrome/answer/Vineeth-Kumar-Marupadige-%E0%B0%B5%E0%B0%BF%E0%B0%A8%E0%B1%80%E0%B0%A4%E0%B1%8D-%E0%B0%95%E0%B1%81%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D-%E0%B0%AE%E0%B0%B0%E0%B1%81%E0%B0%AA%E0%B0%A1%E0%B0%BF%E0%B0%97%E0%B1%86

看看文件内容

这是一个 JSON 文档。最上层结构如下:

{
   "checksum": "c2d3fb04563b236510147df664bcc150",
   "roots": {
        "bookmark_bar":{
            // ...
        },
        "others":{
            // ...
        }
        
   }
}

bookmark_bar 等节点在 Chrome 里都显示为文件夹。 再打开其中一个文件夹看看。 除了 date_added,还能看到 date_last_used。

{
   "checksum": "c2d3fb04563b236510147df664bcc150",
   "roots": {
        "bookmark_bar":{
            "children": [ {
                  "date_added": "13290325579000000",
                  "date_last_used": "0",
                  "guid": "8b486269-2f6f-4676-a5c0-dd42dc88c538",
                  "id": "31",
                  "name": "How to Create a PWA With Next.js - DEV Community",
                  "type": "url",
                  "url": "https://dev.to/byteslash/how-to-create-a-pwa-with-next-js-4dbm"
               }, {
                  "date_added": "13290811720000000",
                  "date_last_used": "0",
                  "guid": "681b9a2a-7c63-473f-a2e1-1095f766782c",
                  "id": "38",
                  "name": "HackerRank",
                  "type": "url",
                  "url": "https://www.hackerrank.com/"
               }, {
                  "date_added": "13290858633000000",
                  "date_last_used": "0",
                  "guid": "372baa03-132c-4853-a46b-1409aa70c127",
                  "id": "39",
                  "name": "How to Bundle a Simple Static Site Using Webpack - SitePoint",
                  "type": "url",
                  "url": "https://www.sitepoint.com/bundle-static-site-webpack/"
               }, {
                  "date_added": "13291416359000000",
                  "date_last_used": "0",
                  "guid": "258fbb34-8df3-41ee-8753-4276f978d8e5",
                  "id": "40",
                  "name": "seq command in Linux with Examples - GeeksforGeeks",
                  "type": "url",
                  "url": "https://www.geeksforgeeks.org/seq-command-in-linux-with-examples/"
               }, {
                  "date_added": "13291425628000000",
                  "date_last_used": "0",
                  "guid": "8e4c9491-8a6b-4e60-bc61-758aad1b88ce",
                  "id": "41",
                  "name": "What does \"esac\" mean at the end of a bash case statement? Is it required? - Unix & Linux Stack Exchange",
                  "type": "url",
                  "url": "https://unix.stackexchange.com/questions/256149/what-does-esac-mean-at-the-end-of-a-bash-case-statement-is-it-required"
               }, {
                  "date_added": "13291426538000000",
                  "date_last_used": "0",
                  "guid": "bb1275b3-f747-4a14-8956-e84bdedffe85",
                  "id": "42",
                  "name": "Functions and Fractals - Recursive Trees - Bash! Discussions | Linux Shell | HackerRank",
                  "type": "url",
                  "url": "https://www.hackerrank.com/challenges/fractal-trees-all/forum/comments/469102"
               }, {
                  "date_added": "13291569746000000",
                  "date_last_used": "0",
                  "guid": "e0a5874e-ced0-4899-bb41-df3bee8b9f02",
                  "id": "43",
                  "name": "Time Complexities of all Sorting Algorithms - GeeksforGeeks",
                  "type": "url",
                  "url": "https://www.geeksforgeeks.org/time-complexities-of-all-sorting-algorithms/"
               }, {
                  "date_added": "13291868121000000",
                  "date_last_used": "0",
                  "guid": "08b8233f-5ad3-4f35-9fdf-533e39917c7b",
                  "id": "49",
                  "name": "Unix / Linux - Regular Expressions with SED",
                  "type": "url",
                  "url": "https://www.tutorialspoint.com/unix/unix-regular-expressions.htm"
               }
               "children": [
                {
                    // can have nested child folder
                }
               ]
            ]
        },
        "others":{
            // ...
        }
        
   }
}

用 JavaScript 解析文件

展开嵌套内容

从 JSON 的结构可以看出,树中有子节点的节点会递归地包含 children 属性。因此可以写一个递归函数,把树展开成列表。

    "children": [ 
        "children":[
            "children":[
                {
                    //obj
                    "name": "Unix / Linux - Regular Expressions with SED",
                }
            ]
        ], 
        {
            //obj
            "name": "Time Complexities of all Sorting Algorithms - GeeksforGeeks",
        } 
    ]
// unfolding nested bookmarks to a list
function children_to_list(chd){
    var list = [];
    for(var i in chd){
        //terminal condition
        if(chd[i].children){
            //recursive call
            list = list.concat(children_to_list(chd[i].children));
        } else {
            list.push(chd[i]);
        }
    }
    return list;
}

来测试一下代码是否正确。

l = children_to_list(bookmarks.roots.other.children)
console.log(l[0],l[1],l[l.length-1], l.length)

for(let i of l){
    if(i.name){
        console.log(i.name)
    }else{
        console.log("wrong type")
        break
    }
}

输出应该类似这样:

{
  date_added: '0',
  date_last_used: '0',
  guid: 'b36223e0-8f0d-faf7-b97b-eef5ae01cf3e',
  id: '110',
  name: '114网站导航www.114.com.cn',
  type: 'url',
  url: 'http://www.114.com.cn/tindex.html'
} {
  date_added: '0',
  date_last_used: '0',
  guid: '7fb44715-7ae7-fbd6-aeba-4b04999d69d0',
  id: '111',
  name: 'DirectX10',
  type: 'url',
  url: 'http://www.lwgame.net/'
} {
  date_added: '13350832066016505',
  date_last_used: '0',
  guid: '3b456bc4-a639-4a00-a9f2-0b1ef4dc0ba3',
  id: '411',
  meta_info: { power_bookmark_meta: '' },
  name: 'markdown中插入图片怎么定义图片的大小或比例? - 知乎',
  type: 'url',
  url: 'https://www.zhihu.com/question/23378396'
} 237
114网站导航www.114.com.cn
DirectX10
MA
Microsoft 商店
Internet Explorer 8:主页
Marketplace
Microsoft 家庭安全

将 date_added 转换为可读日期

我试过 bkmk.date_added = new Date(bkmk.date_added);,却得到了无效日期。

原来 Google 的书签时间戳采用另一套起点和单位,我起初完全没想到。

下面的代码引自 Stack Overflow 的回答

Chrome 书签时间值是自 1601-01-01T00:00:00Z 起经过的微秒数。要转换为 Date:

先除以 1000,换算成毫秒。 把时间原点调整为 1970-01-01T00:00:00Z。 将所得数值传入 Date 构造函数。 例如:

var timeValue = '13170147422089597';
new Date(Date.UTC(1601,0,1) + timeValue / 1000); // 2018-05-07T06:17:02.089Z

将 Date.UTC(1601,0,1) 的值保存为常量(-11644473600000),再封装成函数:

function chromeTimeValueToDate(tv) {
  var epoch = -11644473600000;
  return new Date(epoch + tv / 1000);
}

// Example
['13170147422089597',
 '13150297844686316',
 '13115171381595644'].forEach( tv => {
   console.log(chromeTimeValueToDate(tv))
});

下面是我根据 RobG 的回答写的代码,可以按日期排序并输出书签。

//sort l by date_added
l.sort((a,b) => a.date_added - b.date_added)


// l[0].date_added = new Date(l[0].date_added)
function timestamp_to_readable(bkmk){
    bkmk.date_added = new Date(Date.UTC(1601,0,1) + bkmk.date_added / 1000); 
    return bkmk;
}

l = l.map(timestamp_to_readable)
console.log(l[0],l[1],l[l.length-1], l.length)

for(let i of l){
    if(i.name){
        console.log(i.date_added, i.name)
    }else{
        console.log("wrong type")
        break
    }
}

结果正确:

2024-01-24T09:40:00.168Z Using readable streams - Web APIs | MDN
2024-01-24T09:49:44.741Z javascript - html download attribute redirects to url instead of downloading - Stack Overflow
2024-01-24T09:49:48.176Z <a>: The Anchor element - HTML: HyperText Markup Language | MDN
2024-01-26T12:10:53.341Z ZIP格式分析 | Lordaeron_ESZ's blog
2024-01-27T10:24:02.656Z 19 Markdown 注释_Markdown 入门教程-慕课网
2024-01-27T10:57:46.250Z Visual Studio Code多行同时输入_visual studio 上下一起输入-CSDN博客
2024-01-27T12:14:51.998Z Promise.all() - JavaScript | MDN

最后编辑于 2024 年 3 月 2 日 02:12:26 +0000。

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2024-01-27). 怎样提取 Chrome 书签的添加日期. https://caiborui.com/blog/2024-01-27-en-chrome-bookmark-added-date-zhtrans

@misc{borui2024-5f01ba93,
  author = {borui},
  title = {{怎样提取 Chrome 书签的添加日期}},
  year = {2024},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2024-01-27-en-chrome-bookmark-added-date-zhtrans}
}