@@ -123,19 +123,27 @@ chrome.runtime.onMessage.addListener((request, sender) => {
123
123
124
124
if ( request . captureScreenshot ) {
125
125
const { commitIndex } = request ;
126
- chrome . tabs . captureVisibleTab ( undefined , undefined , dataURL => {
127
- // TODO For some reason, sending a response using the third param (sendResponse) doesn't work,
128
- // so we have to use the chrome.tabs API for this instead.
129
- chrome . tabs . query ( { active : true , currentWindow : true } , tabs => {
130
- chrome . tabs . sendMessage ( tabs [ 0 ] . id , {
131
- event : 'screenshotCaptured' ,
132
- payload : {
133
- commitIndex,
134
- dataURL,
135
- } ,
126
+ try {
127
+ chrome . tabs . captureVisibleTab ( undefined , undefined , dataURL => {
128
+ // TODO For some reason, sending a response using the third param (sendResponse) doesn't work,
129
+ // so we have to use the chrome.tabs API for this instead.
130
+ chrome . tabs . query ( { active : true , currentWindow : true } , tabs => {
131
+ if ( tabs . length > 0 ) {
132
+ chrome . tabs . sendMessage ( tabs [ 0 ] . id , {
133
+ event : 'screenshotCaptured' ,
134
+ payload : {
135
+ commitIndex,
136
+ dataURL,
137
+ } ,
138
+ } ) ;
139
+ }
136
140
} ) ;
137
141
} ) ;
138
- } ) ;
142
+ } catch ( error ) {
143
+ // Screen captures may not always be allowed.
144
+ // DevTools is robust enough to handle missing images in this case.
145
+ // See https://stackoverflow.com/questions/55504938
146
+ }
139
147
}
140
148
}
141
149
} ) ;
0 commit comments