Chrome Extensions Development Series eight: data storage for chrome expansion

Source: Internet
Author: User
Tags set cookie

Google Chrome extensions can use any of the following storage mechanisms:

    • HTML5 Local storage implemented by the Localstorage API (here is a bit)
    • Browser storage implemented by Google's chrome.storage.* API
    • Cookie storage implemented by Google's chrome.cookies.* API

1) browser storage implemented by Chrome.storage API

Chrome extensions can access data or listen to changes in data through the chrome.storage.* API.

In the Manifest.json file, register storage as follows:

{
"Permissions": [
"Storage"
],
}

The Chrome.storage.* API provides Chrome.storage.sync, chrome.storage.local, and chrome.storage.managed three types of storage space. The Chrome.storage.local method is only able to store data locally on the currently logged on device.

The Chrome.storage.sync method realizes automatic data synchronization, the same user regardless of the physical device used, as long as the same account login can access the stored data. The data is stored locally when the device is offline, and the data is synchronized once the device is online. If the user disables data synchronization, the Chrome.storage.local method is used.

The Chrome.storage.managed method is read-only storage, where only domain administrators can store data, and the chrome extension can only read the data.

The chrome.storage mechanism uses a series of storage lattices (tubes) to store data with limited storage space. Data can only be stored in one write operation, and the concurrency performance is not high.

The following is an example of how to use Chrome.storage.sync:

    • Store one or more data

Chrome.storage.sync.set (object items, function () {...})

The Items object contains a number of "key-value pairs" mappings, and a key-value pair is a stored data item.

    • Gets the data item for the specified key

Chrome.storage.sync.get (string or array of string or object keys, function (object items) {...})

The keys, if NULL, return all stored data items, or null object {} if "" or [].

The items object in the callback function is the obtained data item, which contains a "key-value pair" mapping.

    • Deletes one or more data items for the specified key

Chrome.storage.sync.remove (string or array of string keys, function () {...})

    • Empty all stored data items

Chrome.storage.sync.clear (function () {...})

    • Gets the number of storage spaces that are currently in use (in bytes)

Chrome.storage.sync.getBytesInUse (string or array of string keys, function (integer bytesinuse) {...})

The Keys property is the key of the data item, and if NULL represents the space to use for all data items, "" or [] returns 0

In addition, for some sensitive data changes, you can listen through the onchanged event. Any changes in the storage lattice will trigger the event, as shown in the following example:

Chrome.storage.onChanged.addListener (function (changes, namespace) {

For (key in changes) {

var storagechange = Changes[key];

Console.log (' Storage key '%s ' in namespace '%s ' changed. ' +

' Old value was '%s ' and new value is '%s '. ',

Key,//index of the Data key

namespace,//data storage space type, enumeration value, "sync" "local""managed"

storagechange.oldvalue,//the value before the change

Storagechange.newvalue); The changed value

}

});

The changes object in the callback function contains the key (string type) of all changed data and the value before and after the change (Storeagechange type), with the following data structure:

changes:{

Key1:obj

Key2:obj2

...

Keyn:objn

}

2) cookie storage implemented by chrome.cookies.* API

The Chrome extension uses the chrome.cookies.* API to obtain or modify cookies and to monitor changes in cookies.

Declare the cookie permissions in the Manifest.json file and the domain to be accessed as follows:

{

"Permissions": [

"Cookies",

"*://*.google.com"

],

}

The properties of the Chrome.cookies.Cookie object are as follows:

Property name

Type

must-choose/ options available

Comments

Name

String

Must-Choose

The name of the cookie object

Value

String

Must-Choose

Value of the Cookie object

Domain

String

Must-Choose

The domain to which the cookie object applies

Path

String

Must-Choose

URL path to which the cookie object applies

Hostonly

Boolean

Must-Choose

Whether the cookie object responds only to a request from a specified host, the host that accesses the cookie object must be within the specified range

Secure

Boolean

Must-Choose

Whether the cookie object is marked secure so that the cookie object can only be accessed over a secure channel (such as HTTPS)

HttpOnly

Boolean

Must-Choose

Whether the cookie object is marked as HttpOnly so that client script cannot access the cookie object

Session

Boolean

Must-Choose

is the session-level cookie object

ExpirationDate

Double

Options available

The expiration time of the cookie object, in units s

The session-level cookie object does not have this attribute because it expires at the end

StoreId

String

Must-Choose

ID of the Cookiestore that contains the cookie object

Among them, the Chrome.cookies.CookieStore object represents the cookie warehouse in the browser, common Cookiestore and stealth mode Cookiestore with normal mode.

Common methods in the Chrome.cookies API:

    • Gets a cookie object that returns a cookie object that has the longest path if more than one filter is met

Chrome.cookies.get (Object details, function (cookie cookie) {...})

The properties of the details object are as follows:

for the context that is currently executing

Property name

Type

Required/ optional

comments

URL

string

Required

URL of the request accessing the cookie object

name

String

Required

Cookie Object name

storeId

string

Optional

Contains the ID of the cookiestore of the cookie object, Default Cookiestore

    • Gets all the cookie objects in a Cookiestore

Chrome.cookies.getAll (Object details, function (array of cookie cookies) {...})

The properties of the details object are as follows:

Property name

Type

must-choose/ options available

Comments

Url

String

Options available

The URI of the request that accesses the cookie object, affecting domain and path

Name

String

Options available

The name of the cookie object

Value

String

Options available

Value of the Cookie object

Domain

String

Options available

The domain to which the cookie object applies

Path

String

Options available

URL path to which the cookie object applies

Secure

Boolean

Options available

Whether the cookie object is marked secure so that the cookie object can only be accessed over a secure channel (such as HTTPS)

Session

Boolean

Options available

is the session-level cookie object

StoreId

String

Options available

ID of the Cookiestore that contains the cookie object

    • Set Cookie Object

Chrome.cookies.set (Object details, function (cookie cookie) {...})

The properties of the details object are as follows:

Property name

Type

must-choose/ options available

Comments

Url

String

Must-Choose

The URI of the request that accesses the cookie object, affecting domain and path

Name

String

Options available

The name of the cookie object

Value

String

Options available

Value of the Cookie object

Domain

String

Options available

The domain to which the cookie object applies

Path

String

Options available

URL path to which the cookie object applies

Secure

Boolean

Options available

Whether the cookie object is marked secure so that the cookie object can only be accessed over a secure channel (such as HTTPS)

HttpOnly

Boolean

Options available

Whether the cookie object is marked as HttpOnly so that client script cannot access the cookie object

ExpirationDate

Double

Options available

The expiration time of the cookie object, in units s

The session-level cookie object does not have this attribute because it expires at the end

StoreId

String

Options available

ID of the Cookiestore that contains the cookie object

    • Delete Cookie object by name

Chrome.cookies.remove (Object details, function (object details) {...})

The properties of the details object are as follows:

Property name

Type

Comments

Url

String

The URL associated with the cookie object to be deleted

Name

String

The name of the cookie object to delete

StoreId

String

The ID of the cookiestore of the cookie object to delete

    • Get all the Cookie warehouse objects

Chrome.cookies.getAllCookieStores (function (array of Cookiestore cookiestores) {...})

    • Monitor changes in cookie objects

Chrome.cookies.onChanged.addListener (function (object Changeinfo) {...})

If the value of the cookie object is modified or deleted, the event is emitted. The properties of the Changeinfo object are as follows:

Property name

Type

Comments

Removed

Boolean

Whether the cookie object was deleted

Cookies

Chrome.cookies.Cookie

Cookie object that has changed

Cause

Chrome.cookies.OnChangedCause

Causes of changes in cookie objects

Chrome Extensions Development Series eight: data storage for chrome expansion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.