Hardened background script against potential errors
Brian Vaughn committed
Apr 3, 2019 at 18:16 UTC
99f2e0da6f3aee53aa47b9d48574849f351b960b
1 file changed
+19
-11
shells/browser/shared/src/background.js
+19
-11
@@ -123,19 +123,27 @@ chrome.runtime.onMessage.addListener((request, sender) => {
123
124
if (request.captureScreenshot) {
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
+ }
140
});
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
+ }
147
}
148
}
149
});