sessions.getWindowValue()

Retrieves a value previously stored by a call to sessions.setWindowValue.

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

This is an asynchronous function that returns a Promise.

Syntax

js
let retrieving = browser.sessions.getWindowValue(
  windowId,    // integer
  key          // string
)

Parameters

windowId

integer. ID of the window 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 in sessions.setWindowValue.

Return value

A Promise that will be resolved with the value if it exists, or undefined if it does not exist. If the call failed (for example, because the window 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 windows (this will include any windows that have been restored):

js
function onGetResolved(r) {
  console.log(`success: ${r}`);
}

function onGetRejected(e) {
  console.log(`error: ${e}`);
}

browser.windows.onCreated.addListener((window) => {
  browser.sessions
    .getWindowValue(window.id, "my-key")
    .then(onGetResolved, onGetRejected);
});

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
getWindowValue
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – Full support
Firefox 57 (Release date: 2017-11-14)
footnote Full support
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote No support
Firefox for Android – No support
Firefox for Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support