$Boolean

Handles persistent storage of boolean values.

Stan na 13-08-2022. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/449472/1081058/%24Boolean.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name        $Boolean
// @author      Callum Latham <[email protected]>
// @exclude     *
// @description Handles persistent storage of boolean values.
// ==/UserScript==

class $Boolean {
    constructor(KEY, DEFAULT = true) {
        // PRIVATE STATE

        let value;

        // PRIVATE FUNCTIONS

        const getError = (message, error) => {
            if (error) {
                console.error(error);
            }

            return new Error(`[$Toggle] ${message}`);
        };

        const set = (_value) => {
            value = _value;

            if (typeof GM.setValue !== 'function') {
                return Promise.reject(getError('The GM.setValue permission is required to store data.'));
            }

            return GM.setValue(KEY, value);
        };

        // PUBLIC FUNCTIONS

        this.init = () => {
            if (typeof GM.getValue !== 'function') {
                return Promise.reject(getError('The GM.getValue permission is required to retrieve data.'));
            }

            return GM.getValue(KEY, DEFAULT)
                .then(set);
        };

        this.toggle = () => set(!value);

        this.get = () => value;
    };
}