Export Indexed DB

Export indexed db

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey to install this script.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
})();