|
89 | 89 | ### `onMessage` 监听器消息端口自动关闭
|
90 | 90 | > 错误信息:`Unchecked runtime.lastError: The message port closed before a response was received.`
|
91 | 91 |
|
92 |
| -* 可能错误原因一: `chrome.runtime.onMessage` 监听器在异步操作完成之前关闭了消息端口,导致无法发送响应。 |
| 92 | +* **可能错误原因一:** `chrome.runtime.onMessage` 监听器在异步操作完成之前关闭了消息端口,导致无法发送响应。 |
93 | 93 |
|
94 |
| -* 解决方案一: 保持消息端口打开,使用 return true 提前返回或者二次监听的方法 |
95 |
| -https://gitcode.csdn.net/65e95d911a836825ed790a29.html?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NTE3NDg1MSwiZXhwIjoxNzExMzc3NjQ5LCJpYXQiOjE3MTA3NzI4NDksInVzZXJuYW1lIjoidHVmZWlfemhhbmcifQ.TlQuK0xO3uZzlcLs313sgEpqQI_id7CngV4CWIqc5WQ |
| 94 | +* **解决方案一:** 保持消息端口打开,使用 `return true` 提前返回响应 或 二次监听 的方法来保持消息端口开放。 |
96 | 95 |
|
97 |
| -早期的issue: https://github.com/mozilla/webextension-polyfill/issues/130 |
| 96 | +``` |
| 97 | +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { |
| 98 | + if (request.action === 'sendRequest') { |
| 99 | + sendChromeRequest(request, sendResponse); |
| 100 | + } |
| 101 | + return true; // 防止消息端口关闭 |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +* 早期已经有国外的开发者发现此问题并提出了解决方案,传送门 [https://github.com/mozilla/webextension-polyfill/issues/130](https://github.com/mozilla/webextension-polyfill/issues/130) |
| 106 | + |
| 107 | +* **解决方案二:** 双监听器策略。 |
| 108 | + |
| 109 | +``` |
| 110 | +// 第一个监听器,返回 true 以保持消息端口开启 |
| 111 | +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { |
| 112 | + return true; |
| 113 | +}); |
| 114 | +
|
| 115 | +// 第二个监听器,处理实际的接口响应和其他异步操作 |
| 116 | +chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => { |
| 117 | + if (request.action === 'sendRequest') { |
| 118 | + sendChromeRequest(request, sendResponse); |
| 119 | + } |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +* 解决方案参考[这里](https://gitcode.csdn.net/65e95d911a836825ed790a29.html) |
98 | 124 |
|
99 |
| -* 可能错误原因二:`chrome.runtime.sendMessage` 未正确使用回调 |
| 125 | +* **可能错误原因二:** `chrome.runtime.sendMessage` 未正确使用回调函数 `sendResponse` |
| 126 | +* **解决方案:** |
100 | 127 | https://blog.csdn.net/m0_37729058/article/details/89186257
|
101 | 128 |
|
102 |
| -* 可能错误原因三:跨域权限 |
| 129 | +* **可能错误原因三:** 跨域权限 |
103 | 130 | `host_permissions` 权限配置
|
104 | 131 |
|
105 | 132 | ### 使用 `fetch` 发送数据后接收不到 `response`
|
|
0 commit comments