Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Can't use @plasmohq/storage in executeScript function #1083

Open
2 of 3 tasks
chengfengfengwang opened this issue Sep 20, 2024 · 1 comment
Open
2 of 3 tasks
Labels
bug Something isn't working

Comments

@chengfengfengwang
Copy link

What happened?

Can't use @plasmohq/storage in executeScript function, get '_storage is not defined' error

// background/index.ts

import Browser from "webextension-polyfill"
import { Storage } from "@plasmohq/storage"
Browser.action.onClicked.addListener((tab) => {
  if (!tab.url.includes("chrome://")) {
    Browser.scripting.executeScript({
      target: { tabId: tab.id, allFrames: true },
      func: ()=>{
        const storage = new Storage()
        console.log(storage); // _storage is not defined
      },
    })
  }
})

export {}

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

Chrome

Relevant log output

'_storage is not defined'

(OPTIONAL) Contribution

  • I would like to fix this BUG via a PR

Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I checked the current issues for duplicate problems.
@chengfengfengwang chengfengfengwang added the bug Something isn't working label Sep 20, 2024
@tbrockman
Copy link
Contributor

Not a Plasmo bug, see the related MDN documentation to learn more.

TL;DR is that you'll lose any external references passed to func. If your function is simple, you could just use the regular storage APIs.

If you don't need access to other Javascript running in the page and don't really want to use func, it'd probably be easier to create a content.ts file that just listens for a message from the background script (triggering whatever code you want to run), and then have the background script send a message when the browser action is clicked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants