Search code examples
google-chrome-extensionfirefox-addonfirefox-addon-sdkfirefox-addon-webextensions

Firefox extension share async function with return gives => Error: Permission denied to access property "then"


i'm trying to call an async function defined in my extension from a webpage and print the returned value of this function, but i get "Error: Permission denied to access property "then"" (if i do it with a non async function i still get a Permission denied error).

My extension :

let ethereum = new window.Object(); 

let request = async function () {
    console.log("request");
    return ["0x00"]; }

exportFunction(request, ethereum, {
    defineAs: "request"   });    

window.wrappedJSObject.ethereum = ethereum;

My webpage :

const address = await ethereum.request();

The console.log("request") works.

I think I need to Wrap the returned variable or something but can't find what....

Thanks in advance !


Solution

  • exportFunction won't work because your JS function is internally wired to the content script's Promise (this is what async does under the hood) and its prototypes (used to create its returned instance of Promise), which aren't auto-exported as they are not a part of the function.

    • Use eval or new Function in the context of the page if the site's CSP allows unsafe-eval.

      wrappedJSObject.eval(`test = ${async () => {
        console.log("request");
        return ["0x00"];
      }}`);
      

      or

      wrappedJSObject.test = new wrappedJSObject.Function(`return (${async () => {
        console.log("request");
        return ["0x00"];
      }})()`);
      
    • Use a script element if the site's CSP allows, example.