Movatterモバイル変換


[0]ホーム

URL:


  1. Mozilla
  2. Add-ons
  3. Browser extensions
  4. JavaScript APIs
  5. sessions
  6. sessions.getTabValue()

sessions.getTabValue()

Retrieves a value previously stored by a call tosessions.setTabValue.

You can retrieve a value from a tab even over a close/restore cycle: meaning that if you set a value, then the user closes the tab, then restores the tab using the browser's "restore tab" feature (for example, by pressing Control+Shift+T), then you will be able to retrieve the value from the restored tab. Note, though, that a restored tab does not get the same ID as the original, so the ID you pass intogetTabValue() will be different from the ID you passed intosetTabValue(), even though they both refer to the same tab.

This is an asynchronous function that returns aPromise.

Syntax

js
let retrieving = browser.sessions.getTabValue(  tabId,    // integer  key       // string)

Parameters

tabId

integer. ID of the tab whose data you are trying to retrieve. Error is thrown if ID is invalid.

key

string. Key identifying the particular value to retrieve. This needs to match the key previously given insessions.setTabValue.

Return value

APromise that will be resolved with the value if it exists, orundefined if it does not exist. If the call failed (for example, because the tab ID could not be found) then the promise will be rejected with an error message.

Examples

Log the value of "my-key" for all newly created tabs (this will include any tabs that have been restored):

js
function onGetResolved(r) {  console.log(`success: ${r}`);}function onGetRejected(e) {  console.log(`error: ${e}`);}browser.tabs.onCreated.addListener((tab) => {  browser.sessions    .getTabValue(tab.id, "my-key")    .then(onGetResolved, onGetRejected);});

Example extensions

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp