PlasmoHQ / plasmo

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

[BUG] executeScript with async func #1009

Open younes200 opened 2 weeks ago

younes200 commented 2 weeks ago

What happened?

Hi, I am trying to execute an asynchronous function in popup.tsx as shown by this simple example:


async function test () {
          return new Promise((resolve) =>
            resolve(["hello", "world"]),
          );
}

<button
  onClick={() => {
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
      const { id } = tabs[0];
      chrome.scripting.executeScript({
        target: { tabId: id },
        world: "MAIN",
        func: test
        args: [],
      });
    });
  }}>
  Async function call
</button>

Manifest permissions includes : activeTab, scripting.

The issue appears to be with using async with plasmo. It works fine when the function is not async, which implies that chrome.scripting.executeScript can handle asynchronous calls, but not when you use it with plasmo.

If anyone could provide some assistance with this issue, it would be greatly appreciated.

Version

Latest

What OS are you seeing the problem on?

MacOSX

What browsers are you seeing the problem on?

Chrome

Relevant log output

Exception : 

Uncaught ReferenceError: _func is not defined
    at func (<anonymous>:2:37)
    at <anonymous>:3:35

Generated coded :

(function func() {
    return _func.apply(this, arguments);
})()


### (OPTIONAL) Contribution

- [ ] I would like to fix this BUG via a PR

### Code of Conduct

- [X] I agree to follow this project's Code of Conduct
- [X] I checked the [current issues](https://github.com/PlasmoHQ/plasmo/issues?q=is%3Aopen+is%3Aissue+label%3Abug) for duplicate problems.