@@ -123,19 +123,27 @@ chrome.runtime.onMessage.addListener((request, sender) => {
123123
124124 if ( request . captureScreenshot ) {
125125 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+ }
136140 } ) ;
137141 } ) ;
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+ }
139147 }
140148 }
141149} ) ;
0 commit comments