Safari Developer Library

Developer

SafariExtensionSettings Class Reference

Options
Deployment Target:

On This Page

SafariExtensionSettings

Inheritance


Conforms To


Not Applicable

Import Statement


Not Applicable Not Applicable

Availability


Available in Safari 5.0 and later.

The SafariExtensionsSettings class provides a place for your extension to store settings that should persist between sessions.

You can access settings using the provided methods or using JavaScript’s built-in syntax, as follows:

  • // Get a value.
  • safari.extension.settings.someProperty
  • safari.extension.settings["someProperty"]
  • safari.extension.settings.getItem("someProperty")
  • // Set a value.
  • safari.extension.settings.someProperty = 42;
  • safari.extension.settings["someProperty"] = 42;
  • safari.extension.settings.setItem("someProperty", 42");
  • // Delete a value.
  • delete safari.extension.someProperty;
  • delete safari.extension["someProperty"];
  • safari.extension.removeItem("someProperty");

Note that there is a minor difference for properties that don’t exist: JavaScript’s build-in syntax returns undefined, but the getItem method returns null.

  • Returns the current value of a key.

    Declaration

    JavaScript

    any getItem (in DOMString key);

    Parameters

    key

    The key whose value is being returned.

    Return Value

    If the key exists, its current value; otherwise, null.

    Availability

    Available in Safari 5.0 and later.

  • Sets the value of a key.

    Declaration

    JavaScript

    void setItem (in DOMString key, in any value);

    Parameters

    key

    The key whose value is being set.

    value

    The value being set.

    Discussion

    The behavior of this method is undefined if value cannot be serialized to a JSON value.

    On success, this method dispatches a SafariExtensionSettingsChangeEvent event describing the change.

    Availability

    Available in Safari 5.0 and later.

  • Removes a key.

    Declaration

    JavaScript

    void removeItem (in DOMString key);

    Parameters

    key

    The key being removed.

    Discussion

    If there is no value with the given key, this method does nothing.

    On success, this method dispatches a SafariExtensionSettingsChangeEvent event describing the change.

    Availability

    Available in Safari 5.0 and later.

  • Removes all key-value pairs.

    Declaration

    JavaScript

    void clear ();

    Discussion

    If there is nothing to remove, this method does nothing.

    On success, dispatches a SafariExtensionSettingsChangeEvent event describing the change.

    Availability

    Available in Safari 5.0 and later.