index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { guid } from '@/uilts/help'
  2. // iframe 通信中心
  3. class ChromeMessageCenter {
  4. constructor() {
  5. // 缓存事件队列
  6. this.messageCallbackMap = new Map();
  7. this.messageFailbackMap = new Map();
  8. this.timer = null
  9. }
  10. sendToContent(sender, { info = {}, data = {}, callback, overTime, failback }) {
  11. if (!info.messageId) {
  12. info.messageId = `${info.actionType}-${guid()}` // 唯一的ID,用于标记回调函数
  13. }
  14. try {
  15. chrome.tabs.sendMessage(sender.tab.id, {
  16. info,
  17. data
  18. })
  19. if (info.messageId && callback) {
  20. // 带回调callback 的message, 要求携带messageId,callback,failback等
  21. this.listen(info.messageId, callback)
  22. }
  23. if (failback) {
  24. this.addFailback(info.messageId, overTime, failback)
  25. }
  26. } catch (error) {
  27. failback({
  28. error: 2,
  29. msg: String(error)
  30. })
  31. }
  32. }
  33. sendToSW({ info = {}, data = {}, callback, overTime, failback }) {
  34. if (!info.messageId) {
  35. info.messageId = `${info.actionType}-${guid()}` // 唯一的ID,用于标记回调函数
  36. }
  37. try {
  38. chrome.runtime.sendMessage({
  39. info,
  40. data
  41. })
  42. if (info.messageId && callback) {
  43. // 带回调callback 的message, 要求携带messageId,callback,failback等
  44. this.listen(info.messageId, callback)
  45. }
  46. if (failback) {
  47. this.addFailback(info.messageId, overTime, failback)
  48. }
  49. } catch (error) {
  50. failback({
  51. error: 2,
  52. msg: String(error)
  53. })
  54. }
  55. }
  56. listen(messageId, callback) {
  57. // 序列添加失败回调
  58. this.messageCallbackMap.set(messageId, { callback })
  59. }
  60. addFailback(messageId, overTime = 2000, failback) {
  61. // 序列添加失败回调
  62. this.messageFailbackMap.set(messageId, {
  63. time: new Date().getTime(),
  64. overTime,
  65. failback
  66. })
  67. this.checkTimer()
  68. }
  69. init(req = {}) {
  70. const { info, data } = req;
  71. if (!info || !data) {
  72. return
  73. }
  74. // 序列删除 失败回调
  75. this.messageFailbackMap.delete(info.messageId);
  76. // 执行成功回调
  77. const _item = this.messageCallbackMap.get(info.messageId);
  78. if (_item) {
  79. const callback = _item.callback
  80. callback(data)
  81. // 序列删除 成功回调
  82. this.messageCallbackMap.delete(info.messageId)
  83. }
  84. this.checkTimer()
  85. }
  86. checkTimer() {
  87. if (this.timer) {
  88. return
  89. }
  90. let key, value, now_time
  91. this.timer = setInterval(() => {
  92. if (this.messageFailbackMap.size == 0) {
  93. clearInterval(this.timer)
  94. this.timer = null
  95. }
  96. // 轮询查看有无超期的message信息
  97. now_time = new Date().getTime();
  98. for (let item of this.messageFailbackMap.entries()) {
  99. key = item[0] || ''
  100. value = item[1] || {}
  101. if (now_time - value.time > value.overTime) {
  102. const callback = value.failback
  103. callback && callback({
  104. error: 1,
  105. msg: "message 超时错误"
  106. })
  107. this.messageFailbackMap.delete(key)
  108. }
  109. }
  110. }, 1000)
  111. }
  112. }
  113. // messageCenter在页面内实例化一次
  114. const chromeMessageCenter = new ChromeMessageCenter();
  115. export default chromeMessageCenter;