ios - 在 Safari Web Extension (iOS 15) 中将消息从应用程序 (popup.js) 发送到 JS (content.js)
问题描述
我在做一个简单的任务时遇到了麻烦。在 Xcode 13 中,您可以创建一个 Safari Extension App(适用于 iOS 15 的 Safari Web Extension)。
环顾四周,共享扩展有各种 js 资源:background.js、content.js 和 popup.js。
Popup.js 是您启动 Safari Web 扩展时呈现的模式。在 popup.html 中,您可以将元素添加到弹出 DOM(例如文本、按钮等)。在我的弹出窗口中,我有一个在 popup.js 中连接的按钮。当我按下这个按钮时,我想通知 content.js 这个事件。我该怎么做呢?
browser.runtime.sendMessage({ type: "start" });
我当前的代码向包含在 popup.js中的按钮添加了一个事件侦听器。
Xcode 的模板在 content.js 中包含以下内容:
browser.runtime.sendMessage({ greeting: "hello" }).then((response) => {
console.log("Received response: ", response);
});
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Received request: ", request);
});
据我所知,JS 在 popup.js 中运行,但我在 console.log 中没有看到 content.js 的任何内容。旁注:console.log 真的很难用于 iPhone Safari 应用程序。
我找到了这个文档,但它只讨论了将消息从 macOS 应用程序传递到 JS。我正在尝试将消息从 iOS 弹出窗口传递到 content.js。
解决方案
推荐阅读
- macos - SwiftUI 滑块和绑定提示?
- c# - 方法:如何检测数据不在文件中
- android - 如何使用 Retrofit 将使用 Z 和 T 格式化的日期发送到 API?
- docker - 错误“无法使用前端 dockerfile.v0 解决:构建 LLB 失败:执行程序运行失败”
- python - 如何中断我的功能以停止向我的角度服务器发送数据
- firebase - Firebase 函数 .pubsub.schedule 每月的第一个工作日
- angular - 离子应用程序:发生未处理的异常:找不到模块“@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/utils”
- mysql - SQL 插入外键(如果不存在)
- java - 无法下载包 xlsx 或 rjava R
- react-router - reduxForm :在几毫秒内显示旧数据,然后用新数据刷新