Gahing's blog Gahing's blog
首页
知识体系
  • 前端基础
  • 应用框架
  • 工程能力
  • 应用基础
  • 专业领域
  • 业务场景
  • 前端晋升 (opens new window)
  • Git
  • 网络基础
  • 算法
  • 数据结构
  • 编程范式
  • 编解码
  • Linux
  • AIGC
  • 其他领域

    • 客户端
    • 服务端
    • 产品设计
软素质
  • 面试经验
  • 人生总结
  • 个人简历
  • 知识卡片
  • 灵感记录
  • 实用技巧
  • 知识科普
  • 友情链接
  • 美食推荐 (opens new window)
  • 收藏夹

    • 优质前端信息源 (opens new window)
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Gahing / francecil

To be best
首页
知识体系
  • 前端基础
  • 应用框架
  • 工程能力
  • 应用基础
  • 专业领域
  • 业务场景
  • 前端晋升 (opens new window)
  • Git
  • 网络基础
  • 算法
  • 数据结构
  • 编程范式
  • 编解码
  • Linux
  • AIGC
  • 其他领域

    • 客户端
    • 服务端
    • 产品设计
软素质
  • 面试经验
  • 人生总结
  • 个人简历
  • 知识卡片
  • 灵感记录
  • 实用技巧
  • 知识科普
  • 友情链接
  • 美食推荐 (opens new window)
  • 收藏夹

    • 优质前端信息源 (opens new window)
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 前端基础

    • 编程语言

    • 开发工具

    • 前端调试

    • 浏览器原理

    • 浏览器生态

      • Devtools

      • Extension

        • Chrome 拓展开发笔记
          • options background 间通信
          • 短连接 sendMessage callback 回调注意事项
            • 1. 异步回调处理
            • 2. sendMessage callback 回调执行时机
        • chrome.storage 的表升级和索引
  • 应用框架

  • 工程能力

  • 应用基础

  • 专业领域

  • 业务场景

  • 大前端
  • 前端基础
  • 浏览器生态
  • Extension
gahing
2022-05-06
目录

Chrome 拓展开发笔记草稿

content-scripts, background 代码更新,需要到拓展详情页刷新下

content-scripts 代码的输出,在页面的 console 面板下查看,可以理解为注入到页面中的代码

# options background 间通信

chrome.runtime.onMessage.addListener chrome.runtime.sendMessage

# 短连接 sendMessage callback 回调注意事项

# 1. 异步回调处理

background 处理消息

// background
function handleMessage1(request, sender, sendResponse) {
  console.log(`content script sent a message: ${request.content}`);
  setTimeout(() => {
    sendResponse("handleMessage1");
  }, 100);
}
function handleMessage2(request, sender, sendResponse) {
  console.log(`content script sent a message: ${request.content}`);
  setTimeout(() => {
    sendResponse("handleMessage2");
  }, 1000);
  return true;
}

async function handleMessage3(request, sender, sendResponse) {
  console.log(`content script sent a message: ${request.content}`);
  await new Promise((resolve)=>{
      setTimeout(resolve, 1000)
  })

  sendResponse("handleMessage3");
  return true;
}
async function handleMessage4(request, sender, sendResponse) {
  console.log(`content script sent a message: ${request.content}`);
  await Promise.resolve(1)
  sendResponse("handleMessage4");
  return true;
}

async function handleMessage5(request, sender, sendResponse) {
  console.log(`content script sent a message: ${request.content}`);
  await new Promise((resolve)=>{
      chrome.storage.local.get("a",()=>{
          // 会在后面的 event loop 中调用
          resolve()
      })
  })
  sendResponse("handleMessage5");
  return true;
}

chrome.runtime.onMessage.addListener(handleMessage1);
// ...test
chrome.runtime.onMessage.removeListener(handleMessage1)
chrome.runtime.onMessage.addListener(handleMessage2);
chrome.runtime.onMessage.addListener(handleMessage3);
chrome.runtime.onMessage.addListener(handleMessage4);
chrome.runtime.onMessage.addListener(handleMessage5);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

content-script 发送事件

// 期望能打印消息接收者 sendResponse 传入的数据
chrome.runtime.sendMessage(msg, console.log)
1
2

对监听器进行单一或组合设置,可以得到以下结果:

  1. 未设置监听器,报错 Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist (内部输出错误信息,非异常抛出,下同)
  2. 仅设置第一个监听器,报错 Unchecked runtime.lastError: The message port closed before a response was received.
  3. 仅设置第二个监听器, 1s 后成功打印消息:"handleMessage2"
  4. 仅设置第三个监听器,报错 Unchecked runtime.lastError: The message port closed before a response was received.
  5. 仅设置第四个监听器,成功打印消息:"handleMessage4"
  6. 仅设置第五个监听器,报错 Unchecked runtime.lastError: The message port closed before a response was received.
  7. 设置1,2监听器, 100ms 后打印消息:"handleMessage1"

结论:

  1. 所有监听器的回调在同一个 event-loop 中执行
  2. sendResponse 若不在当前 event-loop 中调用,需要在当前 event loop 中的某个监听器中返回 true ,否则后续再调用,发送者回调将报错
  3. 多次执行 sendResponse ,仅处理第一次,即发送者回调仅收到第一次执行时传入的参数

# 2. sendMessage callback 回调执行时机

chrome.runtime.sendMessage(msg, (res)=>{
    console.log('res:', res) //res: undefined
})
1
2
3

回调必定执行

即使接收方未执行 sendResponse ,或者因为各种原因导致发送方没收到消息,发送方也会执行(其中的回调参数为 undefined)

因此如果想根据不同情况执行 sendMessage 回调,应该判断回调中参数取值,而不是在接收方中做 sendResponse 的执行控制

编辑 (opens new window)
上次更新: 2024/09/01, 23:56:56
定制 devtools-fe
chrome.storage 的表升级和索引

← 定制 devtools-fe chrome.storage 的表升级和索引→

最近更新
01
浅谈代码质量与量化指标
08-27
02
快速理解 JS 装饰器
08-26
03
Vue 项目中的 data-v-xxx 是怎么生成的
09-19
更多文章>
Theme by Vdoing | Copyright © 2016-2024 Gahing | 闽ICP备19024221号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式