首页 > 解决方案 > Firefox 扩展共享异步函数返回给 => 错误:访问属性“then”的权限被拒绝

问题描述

我正在尝试从网页调用我的扩展程序中定义的异步函数并打印此函数的返回值,但我得到"Error: Permission denied to access property "then""了(如果我使用非异步函数执行此操作,我仍然会收到 Permission denied 错误)。

我的扩展:

let ethereum = new window.Object(); 

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

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

window.wrappedJSObject.ethereum = ethereum;

我的网页:

const address = await ethereum.request();

console.log("request")作品。

我想我需要包装返回的变量或其他东西,但找不到什么......

提前致谢 !

标签: google-chrome-extensionfirefox-addonfirefox-addon-sdkfirefox-addon-webextensions

解决方案


exportFunction不起作用,因为您的 JS 函数在内部连接到内容脚本Promise(这是async幕后工作)及其原型(用于创建其返回的 实例Promise),它们不是自动导出的,因为它们不是一部分的功能。

  • 如果站点的 CSP 允许,则在页面上下文中使用eval或。new Functionunsafe-eval

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

    或者

    wrappedJSObject.test = new wrappedJSObject.Function(`return (${async () => {
      console.log("request");
      return ["0x00"];
    }})()`);
    
  • script如果站点的 CSP 允许,请使用元素,例如.


推荐阅读