KeithHenry / chromeExtensionAsync

Promise wrapper for the Chrome extension API so that it can be used with async/await rather than callbacks
MIT License
228 stars 32 forks source link

e is not defined when use executeAsyncFunction #30

Open matamune94 opened 3 years ago

matamune94 commented 3 years ago

I make error :

Error: Error thrown in execution script: e is not defined.
Stack: ReferenceError: e is not defined
    at <anonymous>:4:44
    at <anonymous>:4:76
    at <anonymous>:20:3
    at Object.chrome.tabs.executeAsyncFunction

my code:

      try {
        const scriptToExecute = async function() {
          return 'hi'
        }
        const results = await chrome.tabs.executeAsyncFunction(window.tabs[0].id, scriptToExecute)
      } catch (err) {
        console.log(err)
      }

Any have idea ?

b5414 commented 3 years ago

@matamune94 try it:

(async()=>{
    const obj = {};

    obj.code = async()=>{
        await new Promise((r)=>setTimeout(r, 250)); // not important line
        return ['one', 'two'];
    };

    const w = await chrome.windows.create({url: 'https://google.com'});
    console.log(w);

    await new Promise((r)=>setTimeout(r, 1000)); // sometimes - important line
    try{
        const r = await chrome.tabs.executeAsyncFunction(w.tabs[0].id, obj);
        console.log(r);
    }catch(e){
        console.log(e);
    }
})();

also, make sure about permissions and the order of scripts:

"permissions": ["storage", "*://www.google.com/*", "tabs"],
"background": {"scripts": ["libs/cea.js", "libs/cea-eaf.js", "js/background.js"]},