Export Indexed DB

Export indexed db

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Export Indexed DB
// @namespace    rbits.export-indexeddb
// @version      1.0.0
// @description  Export indexed db
// @author       rbits
// @license      MIT
// @match        https://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_setClipboard
// ==/UserScript==

async function exportIndexedDb() {
    const databases = await window.indexedDB.databases();

    const outputObj = {};
    for (const database of databases) {
        const databaseObj = await exportDatabase(database.name);
        outputObj[database.name] = databaseObj;
    }

    const json = JSON.stringify(outputObj);
    GM_setClipboard(json, 'text', () => {
        alert('Copied to clipboard');
    });
}

const exportDatabase = (name) => new Promise((resolve, reject) => {
    const request = window.indexedDB.open(name);
    request.onsuccess = async (event) => {
        const db = event.target.result;
        const objectStoreNames = db.objectStoreNames;

        const outputObj = {};
        for (const objectStoreName of objectStoreNames) {
            const objectStore = await exportObjectStore(db, objectStoreName);
            outputObj[objectStoreName] = objectStore;
        }

        resolve(outputObj);
    };
});

const exportObjectStore = (db, objectStoreName) => new Promise((resolve, reject) => {
    const objectStore = db.transaction(objectStoreName).objectStore(objectStoreName);
    const request = objectStore.getAllKeys();
    request.onsuccess = async (event) => {
        const keys = event.target.result;

        const outputObj = {};
        for (const key of keys) {
            const value = await getFromObjectStore(objectStore, key);
            outputObj[key] = value;
        };

        resolve(outputObj);
    };
});


const getFromObjectStore = (objectStore, key) => new Promise((resolve, reject) => {
    const request = objectStore.get(key);
    request.onsuccess = (event) => {
        resolve(event.target.result);
    };
});


(function() {
    'use strict';

    GM_registerMenuCommand('Export', exportIndexedDb);
})();