-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
47 lines (39 loc) · 1.78 KB
/
Copy pathstorage.js
File metadata and controls
47 lines (39 loc) · 1.78 KB
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
import { browser } from "./browser.js"
// storage.local key holding the user's configured self-hosted instances, each
// an { type, hostname } object. Kept here so the key string lives in one place.
const INSTANCES_KEY = "selfHostedInstances"
// load the user's configured self-hosted instances ([] when none configured)
export async function loadInstances() {
const stored = await browser.storage.local.get(INSTANCES_KEY)
return stored[INSTANCES_KEY] || []
}
// persist the self-hosted instances list
export async function saveInstances(instances) {
await browser.storage.local.set({ [INSTANCES_KEY]: instances })
}
// storage.session key holding the popup result for a tab. Per-tab keys let
// concurrent tabs store results without clobbering each other, and the session
// area clears itself when the browser closes.
function tabResultKey(tabId) {
return `prs:${tabId}`
}
// save a tab's popup result: { prs } on success, { error } on failure
export async function saveTabResult(tabId, result) {
await browser.storage.session.set({ [tabResultKey(tabId)]: result })
}
// load a tab's popup result (undefined while the background is still fetching)
export async function loadTabResult(tabId) {
const stored = await browser.storage.session.get(tabResultKey(tabId))
return stored[tabResultKey(tabId)]
}
// forget a tab's popup result (tab closed, or a new page load under way)
export async function clearTabResult(tabId) {
await browser.storage.session.remove(tabResultKey(tabId))
}
// call back with a tab's popup result whenever the background stores one
export function onTabResult(tabId, callback) {
browser.storage.session.onChanged.addListener(changes => {
const change = changes[tabResultKey(tabId)]
if (change?.newValue) callback(change.newValue)
})
}