PlasmoHQ / plasmo

🧩 The Browser Extension Framework
https://www.plasmo.com
MIT License
10.03k stars 346 forks source link

[BUG] Background does not appear to persist to local storage #896

Closed drewclauson closed 6 months ago

drewclauson commented 6 months ago

What happened?

I am newer to the world of extensions, so I may not be doing this right, but when trying to save data to local storage in a content script, I'm sending a message to the background and then using the with-storage example. It appears to work when looking at the console, but when looking through dev tools, I can't find where the data is actually stored in local storage. Am I thinking about this incorrectly or is there an issue I'm missing? Background message code is below, I pulled it from the with-storage example.

import type {PlasmoMessaging} from "@plasmohq/messaging"
import {Storage} from "@plasmohq/storage";
import {SecureStorage} from "@plasmohq/storage/dist/secure";
import assert from "assert"

const PASSWORD = "password"
const TEST_KEY = "ship"
const TEST_DATA = "1701"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
await testSecureStorage()
}

async function testSecureStorage() {
const storage = new SecureStorage({area: "local"})
await storage.clear()

await storage.setPassword(PASSWORD)
// Must set password then watch, otherwise the namespace key will mismatch
storage.watch({
[TEST_KEY]: (c) => {
console.log(TEST_KEY, c)
}
})

await storage.set(TEST_KEY, TEST_DATA)

const foo = await storage.get(TEST_KEY)

assert(foo === TEST_DATA, "ENCRYPTION FAILED")

console.log(await storage.getAll())

await storage.set(TEST_KEY, TEST_DATA + "2")

await storage.clear()
}

export default handler

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

(OPTIONAL) Contribution

Code of Conduct

drewclauson commented 6 months ago

I think I figured it out - it's saving to chrome.storage.local, which is not viewable in dev tools without an additional extension.