google-chrome-extension - 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")
作品。
我想我需要包装返回的变量或其他东西,但找不到什么......
提前致谢 !
解决方案
exportFunction
不起作用,因为您的 JS 函数在内部连接到内容脚本Promise
(这是async
幕后工作)及其原型(用于创建其返回的 实例Promise
),它们不是自动导出的,因为它们不是一部分的功能。
如果站点的 CSP 允许,则在页面上下文中使用
eval
或。new Function
unsafe-eval
wrappedJSObject.eval(`test = ${async () => { console.log("request"); return ["0x00"]; }}`);
或者
wrappedJSObject.test = new wrappedJSObject.Function(`return (${async () => { console.log("request"); return ["0x00"]; }})()`);
script
如果站点的 CSP 允许,请使用元素,例如.
推荐阅读
- apache-flink - Flink taskmanager out of memory 和内存配置
- python - Windows 10 上的 pip install torchvision 错误
- r - 定义四分位数及其名称
- kubernetes - storageClass kubernetes.io/no-provisioner 是否适用于多节点集群?
- vba - Excel VBA 向 SumIf 添加新行
- jakarta-ee - 消息驱动的 bean 没有收到任何消息
- postgresql - 如何使用 Hibernate Envers 查询图表数据
- prestashop - 如何在 prestashop 中使用模板视图?
- python - Python 3:使用 Rscript 从子进程捕获矩阵返回
- stack - Stack of Plates:破解编码面试