Skip to content

Commit

Permalink
fix: correctly returns xhr errors when type is blob
Browse files Browse the repository at this point in the history
porting commit fa2641b from pr quoid#558 fix issue quoid#555
  • Loading branch information
ACTCD committed Oct 15, 2023
1 parent 3470b84 commit ba8d8b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
27 changes: 15 additions & 12 deletions src/ext/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,21 @@ function handleMessage(request, sender, sendResponse) {
if (["", "text"].indexOf(xhr.responseType) !== -1) {
x.responseText = xhr.responseText;
}
// need to convert arraybuffer data to postMessage
if (xhr.responseType === "arraybuffer") {
const arr = Array.from(new Uint8Array(xhr.response));
x.response = arr;
}
// need to blob arraybuffer data to postMessage
if (xhr.responseType === "blob") {
const base64data = await readAsDataURL(xhr.response);
x.response = {
data: base64data,
type: xhr.responseType
};
// only process when xhr is complete and data exist
if (xhr.readyState === 4 && xhr.response !== null) {
// need to convert arraybuffer data to postMessage
if (xhr.responseType === "arraybuffer") {
const arr = Array.from(new Uint8Array(xhr.response));
x.response = arr;
}
// need to convert blob data to postMessage
if (xhr.responseType === "blob") {
const base64data = await readAsDataURL(xhr.response);
x.response = {
data: base64data,
type: xhr.responseType
};
}
}
port.postMessage({name: e, event, response: x});
};
Expand Down
31 changes: 17 additions & 14 deletions src/ext/content-scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,24 @@ const apis = {
) {
// process xhr response
const r = msg.response;
if (r.responseType === "arraybuffer") {
// arraybuffer responses had their data converted in background
// convert it back to arraybuffer
try {
const buffer = new Uint8Array(r.response).buffer;
r.response = buffer;
} catch (err) {
console.error("error parsing xhr arraybuffer", err);
// only process when xhr is complete and data exist
if (r.readyState === 4 && r.response !== null) {
if (r.responseType === "arraybuffer") {
// arraybuffer responses had their data converted in background
// convert it back to arraybuffer
try {
const buffer = new Uint8Array(r.response).buffer;
r.response = buffer;
} catch (err) {
console.error("error parsing xhr arraybuffer", err);
}
} else if (r.responseType === "blob" && r.response.data) {
// blob responses had their data converted in background
// convert it back to blob
const resp = await fetch(r.response.data);
const b = await resp.blob();
r.response = b;
}
} else if (r.responseType === "blob" && r.response.data) {
// blob responses had their data converted in background
// convert it back to blob
const resp = await fetch(r.response.data);
const b = await resp.blob();
r.response = b;
}
// call userscript method
details[msg.name](msg.response);
Expand Down

0 comments on commit ba8d8b6

Please sign in to comment.