@@ -7,15 +7,23 @@ library messaging;
7
7
8
8
import 'dart:async' ;
9
9
import 'dart:convert' ;
10
+ import 'dart:js_util' ;
10
11
11
12
import 'package:js/js.dart' ;
12
13
13
14
import 'chrome_api.dart' ;
14
15
import 'data_serializers.dart' ;
15
16
import 'logger.dart' ;
16
17
18
+ // A default response for the sendResponse callback.
19
+ //
20
+ // Prevents the message port from closing. See:
21
+ // https://developer.chrome.com/docs/extensions/mv3/messaging/#simple
22
+ final defaultResponse = jsify ({'response' : 'received' });
23
+
17
24
enum Script {
18
25
background,
26
+ copier,
19
27
debuggerPanel,
20
28
detector;
21
29
@@ -27,6 +35,7 @@ enum Script {
27
35
enum MessageType {
28
36
isAuthenticated,
29
37
connectFailure,
38
+ appId,
30
39
debugInfo,
31
40
debugStateChange,
32
41
devToolsUrl,
@@ -104,34 +113,77 @@ void interceptMessage<T>({
104
113
}
105
114
}
106
115
116
+ /// Send a message using the chrome.runtime.sendMessage API.
107
117
Future <bool > sendRuntimeMessage ({
108
118
required MessageType type,
109
119
required String body,
110
120
required Script sender,
111
121
required Script recipient,
122
+ }) =>
123
+ _sendMessage (
124
+ type: type,
125
+ body: body,
126
+ sender: sender,
127
+ recipient: recipient,
128
+ );
129
+
130
+ /// Send a message using the chrome.tabs.sendMessage API.
131
+ Future <bool > sendTabsMessage ({
132
+ required int tabId,
133
+ required MessageType type,
134
+ required String body,
135
+ required Script sender,
136
+ required Script recipient,
137
+ }) =>
138
+ _sendMessage (
139
+ tabId: tabId,
140
+ type: type,
141
+ body: body,
142
+ sender: sender,
143
+ recipient: recipient,
144
+ );
145
+
146
+ Future <bool > _sendMessage ({
147
+ required MessageType type,
148
+ required String body,
149
+ required Script sender,
150
+ required Script recipient,
151
+ int ? tabId,
112
152
}) {
113
153
final message = Message (
114
154
to: recipient,
115
155
from: sender,
116
156
type: type,
117
157
body: body,
118
- );
158
+ ). toJSON () ;
119
159
final completer = Completer <bool >();
120
- chrome.runtime.sendMessage (
121
- // id
122
- null ,
123
- message.toJSON (),
124
- // options
125
- null ,
126
- allowInterop (() {
127
- final error = chrome.runtime.lastError;
128
- if (error != null ) {
129
- debugError (
130
- 'Error sending $type to $recipient from $sender : ${error .message }' ,
131
- );
132
- }
133
- completer.complete (error != null );
134
- }),
135
- );
160
+ void responseHandler ([dynamic _]) {
161
+ final error = chrome.runtime.lastError;
162
+ if (error != null ) {
163
+ debugError (
164
+ 'Error sending $type to $recipient from $sender : ${error .message }' ,
165
+ );
166
+ }
167
+ completer.complete (error != null );
168
+ }
169
+
170
+ if (tabId != null ) {
171
+ chrome.tabs.sendMessage (
172
+ tabId,
173
+ message,
174
+ // options
175
+ null ,
176
+ allowInterop (responseHandler),
177
+ );
178
+ } else {
179
+ chrome.runtime.sendMessage (
180
+ // id
181
+ null ,
182
+ message,
183
+ // options
184
+ null ,
185
+ allowInterop (responseHandler),
186
+ );
187
+ }
136
188
return completer.future;
137
189
}
0 commit comments