Skip to content

Postman 历史记录导出的解决方案

Published: at 14:13

Postman 可以说是我在 CTF 中使用最多的工具了。它确实非常好用,但我并没有完全掌握它的使用之道,因此大量的历史请求堆在一起,显得环境无比混乱。

虽说是有想要改变的想法,但这些历史记录还是非常重要的,一时间难以割舍。于是便开始寻找导出的方案。

ToC

indexedDB

我们知道,Postman 是典型的 Electron 应用,而其数据则是存在了 indexedDB 中。通过开发者工具可以简单浏览一二:

保存文件

为了保存方便,我们定义 console.save 函数,以将字符串保存到文件[1]:

1
(function (console) {
2
console.save = function (data, filename) {
3
if (!data) {
4
console.error("Console.save: No data");
5
return;
6
}
7
8
if (!filename) filename = "console.json";
9
10
if (typeof data === "object") {
11
data = JSON.stringify(data, undefined, 4);
12
}
13
14
var blob = new Blob([data], { type: "text/json" }),
15
e = document.createEvent("MouseEvents"),
16
a = document.createElement("a");
17
18
a.download = filename;
19
a.href = window.URL.createObjectURL(blob);
20
a.dataset.downloadurl = ["text/json", a.download, a.href].join(":");
21
e.initMouseEvent(
22
"click",
23
true,
24
false,
25
window,
26
0,
27
0,
28
0,
29
0,
30
0,
31
false,
32
false,
33
false,
34
false,
35
0,
36
null
37
);
38
a.dispatchEvent(e);
39
};
40
})(console);

导出 db

这里我们使用的是 indexeddb-export-import[2] 中的函数:

1
/**
2
* Export all data from an IndexedDB database
3
* @param {IDBDatabase} idbDatabase - to export from
4
* @param {function(Object?, string?)} cb - callback with signature (error, jsonString)
5
*/
6
function exportToJsonString(idbDatabase, cb) {
7
const exportObject = {};
8
const objectStoreNamesSet = new Set(idbDatabase.objectStoreNames);
9
const size = objectStoreNamesSet.size;
10
if (size === 0) {
11
cb(null, JSON.stringify(exportObject));
12
} else {
13
const objectStoreNames = Array.from(objectStoreNamesSet);
14
const transaction = idbDatabase.transaction(objectStoreNames, "readonly");
15
transaction.onerror = event => cb(event, null);
16
17
objectStoreNames.forEach(storeName => {
18
const allObjects = [];
19
transaction.objectStore(storeName).openCursor().onsuccess = event => {
20
const cursor = event.target.result;
21
if (cursor) {
22
allObjects.push(cursor.value);
23
cursor.continue();
24
} else {
25
exportObject[storeName] = allObjects;
26
if (objectStoreNames.length === Object.keys(exportObject).length) {
27
cb(null, JSON.stringify(exportObject));
28
}
29
}
30
};
31
});
32
}
33
}

开始导出

最后用几行代码就可以导出了:

1
dbResp = indexedDB.open("postman-app");
2
dbResp.onsuccess = function () {
3
var db = dbResp.result;
4
exportToJsonString(db, (err, str) =>
5
err ? console.error(err) : console.save(str, "export.json")
6
);
7
};

保存后用 firefox 打开的效果如下图所示:

参考

  1. https://github.com/postmanlabs/postman-app-support/issues/1647#issuecomment-341270254
  2. https://github.com/Polarisation/indexeddb-export-import