diff options
Diffstat (limited to 'files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html')
-rw-r--r-- | files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html new file mode 100644 index 0000000000..7558105bcc --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html @@ -0,0 +1,118 @@ +--- +title: cookies.onChanged +slug: Mozilla/Add-ons/WebExtensions/API/cookies/onChanged +translation_of: Mozilla/Add-ons/WebExtensions/API/cookies/onChanged +--- +<div>{{AddonSidebar()}}</div> + +<p>{{WebExtAPIRef("cookies")}} API 的 <code>onChanged</code> 事件會在 cookie 設定或刪除時觸發。</p> + +<p>請注意,更新 cookie 的屬性要透過以下兩個步驟實做:</p> + +<ol> + <li>首先,要更新的 cookie 會先被刪掉,並產生一個 <code>overwrite</code> 的 {{WebExtAPIRef("cookies.OnChangedCause")}} 提醒。</li> + <li>接著,帶著更新數值的新 cookie 會被寫進去,並產生第二個 <code>explicit</code> 的 {{WebExtAPIRef("cookies.OnChangedCause")}} 提醒。</li> +</ol> + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox brush:js">browser.cookies.onChanged.addListener(listener) +browser.cookies.onChanged.removeListener(listener) +browser.cookies.onChanged.hasListener(listener) +</pre> + +<p>此 API 也能以 <code>browser.cookies.onChanged.*</code> 運行。</p> + +<p>此事件有以下函式:</p> + +<dl> + <dt><code>addListener(callback)</code></dt> + <dd>給此事件添加監聽器(listener)。</dd> + <dt><code>removeListener(listener)</code></dt> + <dd>停止監聽此事件。<code>listener</code> 參數是要移除的監聽器。</dd> + <dt><code>hasListener(listener)</code></dt> + <dd>檢查此事件的 <code>listener</code> 是否被監聽了。若有監聽,回傳 <code>true</code>,否則回傳 <code>false</code>。</dd> +</dl> + +<h2 id="addListener_語法">addListener 語法</h2> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>callback</code></dt> + <dd> + <p>能被呼叫的 callback 函式會在此事件發生的時候觸發。函式會 passed 以下參數:</p> + + <dl class="reference-values"> + <dt><code>changeInfo</code></dt> + <dd>一個含有觸發事件資訊的 <code>object</code>。它有兩個屬性:</dd> + <dd> + <dl class="reference-values"> + <dt><code>removed</code></dt> + <dd>一個 <code>boolean</code>。如果 cookie 被刪除則為 <code>true</code>,否則為 <code>false</code>。</dd> + <dt><code>cookie</code></dt> + <dd>一個 {{WebExtAPIRef('cookies.Cookie')}} 物件。含有被設定、或被刪除的 cookie 資訊。</dd> + <dt><code>cause</code></dt> + <dd>一個 {{WebExtAPIRef('cookies.OnChangedCause')}} 數值。含有 cookie被改變的潛在原因。</dd> + </dl> + </dd> + </dl> + </dd> +</dl> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + +<p class="hidden">本相容性表格由結構化資料產生。若要貢獻資料,請參考 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 並給一個 pull request。</p> + +<p>{{Compat("webextensions.api.cookies.onChanged")}}</p> + +<h2 id="示例">示例</h2> + +<p>本範例監聽 <code>onChanged</code> 事件並紀錄由 <code>changeInfo</code> 參數傳來的資訊:</p> + +<pre class="brush: js line-numbers language-js">browser.cookies.onChanged.addListener(function(changeInfo) { + console.log('Cookie changed: ' + + '\n * Cookie: ' + JSON.stringify(changeInfo.cookie) + + '\n * Cause: ' + changeInfo.cause + + '\n * Removed: ' + changeInfo.removed); +});</pre> + +<p>{{WebExtExamples}}</p> + +<div class="note"><strong>致謝</strong> + +<p>此 API 基於 Chromium 的 <a href="https://developer.chrome.com/extensions/cookies"><code>chrome.cookies</code></a> API 而來,文件改作自 Chromium 程式碼裡的 <a href="https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/api/cookies.json"><code>cookies.json</code></a>。</p> + +<p>Microsoft Edge 的相容資訊來自微軟公司,原文以創用 CC 姓名標示 3.0 美國版條款授權大眾使用。</p> +</div> + +<div class="hidden"> +<pre>// Copyright 2015 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +</pre> +</div> |