Skip to content

Commit fa2641b

Browse files
committed
fix: correctly returns xhr errors when type is blob
fix issue #555
1 parent fa0e9dc commit fa2641b

2 files changed

Lines changed: 32 additions & 26 deletions

File tree

xcode/Safari-Extension/Resources/background.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,18 +414,21 @@ function handleMessage(request, sender, sendResponse) {
414414
if (["", "text"].indexOf(xhr.responseType) !== -1) {
415415
x.responseText = xhr.responseText;
416416
}
417-
// need to convert arraybuffer data to postMessage
418-
if (xhr.responseType === "arraybuffer") {
419-
const arr = Array.from(new Uint8Array(xhr.response));
420-
x.response = arr;
421-
}
422-
// need to blob arraybuffer data to postMessage
423-
if (xhr.responseType === "blob") {
424-
const base64data = await readAsDataURL(xhr.response);
425-
x.response = {
426-
data: base64data,
427-
type: xhr.responseType
428-
};
417+
// only process when xhr is complete and data exist
418+
if (xhr.readyState === 4 && xhr.response !== null) {
419+
// need to convert arraybuffer data to postMessage
420+
if (xhr.responseType === "arraybuffer") {
421+
const arr = Array.from(new Uint8Array(xhr.response));
422+
x.response = arr;
423+
}
424+
// need to convert blob data to postMessage
425+
if (xhr.responseType === "blob") {
426+
const base64data = await readAsDataURL(xhr.response);
427+
x.response = {
428+
data: base64data,
429+
type: xhr.responseType
430+
};
431+
}
429432
}
430433
port.postMessage({name: e, event, response: x});
431434
};

xcode/Safari-Extension/Resources/content.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,24 @@ const apis = {
160160
) {
161161
// process xhr response
162162
const r = msg.response;
163-
if (r.responseType === "arraybuffer") {
164-
// arraybuffer responses had their data converted in background
165-
// convert it back to arraybuffer
166-
try {
167-
const buffer = new Uint8Array(r.response).buffer;
168-
r.response = buffer;
169-
} catch (err) {
170-
console.error("error parsing xhr arraybuffer", err);
163+
// only process when xhr is complete and data exist
164+
if (r.readyState === 4 && r.response !== null) {
165+
if (r.responseType === "arraybuffer") {
166+
// arraybuffer responses had their data converted in background
167+
// convert it back to arraybuffer
168+
try {
169+
const buffer = new Uint8Array(r.response).buffer;
170+
r.response = buffer;
171+
} catch (err) {
172+
console.error("error parsing xhr arraybuffer", err);
173+
}
174+
} else if (r.responseType === "blob" && r.response.data) {
175+
// blob responses had their data converted in background
176+
// convert it back to blob
177+
const resp = await fetch(r.response.data);
178+
const b = await resp.blob();
179+
r.response = b;
171180
}
172-
} else if (r.responseType === "blob" && r.response.data) {
173-
// blob responses had their data converted in background
174-
// convert it back to blob
175-
const resp = await fetch(r.response.data);
176-
const b = await resp.blob();
177-
r.response = b;
178181
}
179182
// call userscript method
180183
details[msg.name](msg.response);

0 commit comments

Comments
 (0)