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] How to Retrieve Storage Data in contents Script with world: "MAIN" #1150

Open
2 of 3 tasks
gopkg-dev opened this issue Dec 27, 2024 · 1 comment
Open
2 of 3 tasks
Labels
bug Something isn't working

Comments

@gopkg-dev
Copy link

What happened?

Description:

I have set some storage data in the OptionsIndex component, but I am unable to retrieve this data in the contents script that runs with world: "MAIN". I need assistance to achieve this.

Problem Details:

In the OptionsIndex component, I use the Storage class to set some key-value pairs. However, when I attempt to retrieve this storage data in the contents script, I am unable to get the stored values.

Steps to Reproduce:

  1. Load the OptionsIndex component in the application.
  2. Attempt to retrieve the storage data in the contents script.

Expected Behavior:
The storage data set in the OptionsIndex component should be successfully retrieved in the contents script.

Actual Behavior:
The storage data is not being retrieved in the contents script.

Code Snippets:

OptionsIndex Component:

import { useEffect } from "react";
import { Storage } from "@plasmohq/storage";

function OptionsIndex() {
    const storage = new Storage();

    const setOptions = async () => {
        await storage.set("test1", 1111);
        await storage.set("test2", 2222);
        await storage.set("test3", 3333);
    }

    useEffect(() => {
        setOptions();
    }, []);

    return (<></>);
}

export default OptionsIndex;

contents Script:

export const config: PlasmoCSConfig = {
    matches: [
        "https://www.plasmo.com/*",
    ],
    run_at: "document_start",
    world: "MAIN",
}

const storage = new Storage();

storage.getAll().then((data) => {
    console.log("storage data:", data);
});

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

  • 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.
@gopkg-dev gopkg-dev added the bug Something isn't working label Dec 27, 2024
@flexchar
Copy link

It's not a bug. It's how Chrome works. When you're using MAIN world, the Javascript thread that the webpage uses, you do NOT have access to ANY chrome.runtime.* methods. You must either use ISOLATED world (the default) or use Messaging API.

I propose to close the issue.

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