gm-import-export

Helper functions for importing and exporting stored values.

Цей скрипт не слід встановлювати безпосередньо. Це - бібліотека для інших скриптів для включення в мета директиву // @require https://update.greasyfork.org/scripts/487244/1326878/gm-import-export.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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               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]]));
}