gm-import-export

Helper functions for importing and exporting stored values.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/487244/1326878/gm-import-export.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name               gm-import-export
// @description        Helper functions for importing and exporting stored values.
// @author             Jason Kwok
// @namespace          https://jasonhk.dev/
// @version            1.0.0
// @license            MIT
// ==/UserScript==

function GM_importValues(values, cleanImport = false)
{
    if (cleanImport)
    {
        for (const key of GM_listValues())
        {
            GM_deleteValue(key);
        }
    }

    for (const key of Object.keys(values))
    {
        GM_setValue(key, values[key]);
    }
}

function GM_exportValues()
{
    const values = {};
    for (const key of GM_listValues())
    {
        values[key] = GM_getValue(key);
    }

    return values;
}

GM.importValues = async function importValues(values, cleanImport = false)
{
    if (cleanImport)
    {
        const promises = [];
        for (const key of await GM.listValues())
        {
            promises.push(GM.deleteValue(key));
        }

        await Promise.all(promises);
    }

    const promises = [];
    for (const key of Object.keys(values))
    {
        promises.push(GM.setValue(key, values[key]));
    }

    await Promise.all(promises);
}

GM.exportValues = async function exportValues()
{
    const keys = await GM.listValues();

    const promises = [];
    for (const key of keys)
    {
        promises.push(GM.getValue(key));
    }

    const values = await Promise.all(promises);
    return Object.fromEntries(keys.map((key, i) => [key, values[i]]));
}