aboutsummaryrefslogtreecommitdiff
path: root/files/ja/mozilla/add-ons/webextensions/api/storage/storagearea/set/index.html
blob: 609fee944a6454f074d3eff034d49abead2f2104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
title: StorageArea.set()
slug: Mozilla/Add-ons/WebExtensions/API/storage/StorageArea/set
tags:
  - API
  - Add-ons
  - Extensions
  - Method
  - Non-standard
  - Reference
  - Storage
  - StorageArea
  - WebExtensions
  - set
translation_of: Mozilla/Add-ons/WebExtensions/API/storage/StorageArea/set
---
<div>{{AddonSidebar()}}</div>

<p>1つ以上のアイテムをストレージ領域に保存または上書きします。</p>

<p>この API を使用して保存や上書きをする場合、{{WebExtAPIRef("storage.onChanged")}} イベントが発火します。</p>

<p>この関数は <code style="font-size: 16px !important; line-height: 24px !important;"><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise" style="font-size: 16px !important; line-height: 24px !important;">Promise</a></code> を返す非同期関数です。</p>

<h2 id="構文">構文</h2>

<pre class="syntaxbox brush:js">let settingItem = browser.storage.&lt;storageType&gt;.set(
  keys             // オブジェクト
)
</pre>

<p><code style="font-size: 16px !important; line-height: 24px !important;">&lt;storageType&gt;</code><a href="/ja/docs/Mozilla/Add-ons/WebExtensions/API/storage/sync" style="font-size: 16px !important; line-height: 24px !important;" title="sync ストレージ領域を指します。 sync ストレージ内のアイテムはブラウザーによって同期され、ログイン(Firefox sync や Google アカウントなど)しているブラウザー・デバイスの全てのインスタンスで利用できます。"><code style="font-size: 16px !important; line-height: 24px !important;">storage.sync</code></a> または <a class="new" href="/ja/docs/Mozilla/Add-ons/WebExtensions/API/storage/local" rel="nofollow" style="font-size: 16px !important; line-height: 24px !important;" title="この項目についての文書はまだ書かれていません。書いてみませんか?"><code style="font-size: 16px !important; line-height: 24px !important;">storage.local</code></a> の書き込み可能なストレージタイプです。</p>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>keys</code></dt>
 <dd>
 <p>保存したい1つ以上のキー/値ペアを持つオブジェクトを指定します。アイテムが既に存在する場合、値は上書きされます。</p>

 <p>値は <a href="/ja/docs/Glossary/Primitive">primitive 型</a> (整数型・ブール型・文字列) または<code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Array">配列</a></code> を指定でできます。</p>

 <p>通常は他の型 (<code>Function</code>, <code>Date</code>, <code>RegExp</code>, <code>Set</code>, <code>Map</code>, <code>ArrayBuffer</code>  など)は格納できません。これらのサポートされていない型の中には空のオブジェクトとして復元されたり、 <code>set()</code> がエラーをスローする場合があります。この場合の挙動はブラウザに依存します。</p>
 </dd>
</dl>

<h3 id="返り値">返り値</h3>

<p>成功時は引数の無い <code style="font-size: 16px !important; line-height: 24px !important;"><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise" style="font-size: 16px !important; line-height: 24px !important;">Promise</a></code> を返します。 失敗した場合 promise はエラーメッセージと共にリジェクトされます。</p>

<h2 id="ブラウザ実装状況">ブラウザ実装状況</h2>

<p> </p>

<p>{{Compat("webextensions.api.storage.StorageArea.set")}}</p>

<p> </p>

<h2 id="例"></h2>

<pre class="brush: js">function setItem() {
  console.log("OK");
}

function gotKitten(item) {
  console.log(`${item.kitten.name} has ${item.kitten.eyeCount} eyes`);
}

function gotMonster(item) {
  console.log(`${item.monster.name} has ${item.monster.eyeCount} eyes`);
}

function onError(error) {
  console.log(error)
}

// オブジェクトを2つ定義
var monster = {
  name: "Kraken",
  tentacles: true,
  eyeCount: 10
}

var kitten = {
  name: "Moggy",
  tentacles: false,
  eyeCount: 2
}

// オブジェクト2つを格納
browser.storage.local.set({kitten, monster})
  .then(setItem, onError);

browser.storage.local.get("kitten")
  .then(gotKitten, onError);
browser.storage.local.get("monster")
  .then(gotMonster, onError);
</pre>

<p>{{WebExtExamples}}</p>

<div class="note"><strong>謝辞</strong>

<p>この API は Chromium の <a class="external external-icon" href="https://developer.chrome.com/extensions/storage" rel="noopener" style="font-size: 18px !important; line-height: 27px !important;"><code style="font-size: 16px !important; line-height: 24px !important;">chrome.storage</code></a> APIに基づいています。また、このドキュメントは <a class="external external-icon" href="https://chromium.googlesource.com/chromium/src/+/master/extensions/common/api/storage.json" rel="noopener" style="font-size: 18px !important; line-height: 27px !important;"><code style="font-size: 16px !important; line-height: 24px !important;">storage.json</code></a> における Chromium のコードに基づいています。</p>
</div>